/**
 * notion-enhancer: tabs
 * (c) 2021 dragonwocky <thedragonring.bod@gmail.com> (https://dragonwocky.me/)
 * (https://notion-enhancer.github.io/) under the MIT license
 */

'use strict';

module.exports = async function ({}, db, __exports, __eval) {
  const electron = require('electron');
  electron.ipcMain.on('notion-enhancer:close-tab', (event, { window, id }) => {
    electron.webContents.fromId(window).send('notion-enhancer:close-tab', id);
  });

  __eval(`
    const notionHandleActivate = handleActivate;
    handleActivate = (relativeUrl) => {
      const api = require('notion-enhancer/api/index.cjs'),
        { BrowserWindow } = require('electron'),
        windows = api.electron.getNotionWindows(),
        electronWindows = BrowserWindow.getAllWindows();
      if (relativeUrl && windows.length) {
        const win = api.electron.getFocusedNotionWindow() || windows[0];
        win.webContents.send('notion-enhancer:open-tab', {
          notionUrl: \`notion://www.notion.so\$\{relativeUrl\}\`,
        });
        win.show();
        win.focus();
      } else if (relativeUrl && electronWindows.length && !windows.length) {
        // enhancer menu is open: prevent override
        const { createWindow } = api.electron.notionRequire('main/createWindow'),
          win = createWindow(relativeUrl);
        win.focus();
      } else notionHandleActivate(relativeUrl);
    };
  `);
};
