/**
 * notion-enhancer: tray
 * (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 (api, db, __exports, __eval) {
  const electron = require('electron'),
    urlHelpers = api.electron.notionRequire('helpers/urlHelpers'),
    runInBackground = await db.get(['run_in_background']);
  if (!runInBackground) return;

  let appQuit = false;
  electron.app.once('before-quit', () => {
    appQuit = true;
  });

  const notionCreateWindow = __exports.createWindow;
  __exports.createWindow = (relativeUrl = '', args) => {
    const windows = api.electron.getNotionWindows();
    if (windows.length) windows.forEach((win) => win.show());

    if (relativeUrl || !windows.length) {
      // hijack close event to hide instead
      const window = notionCreateWindow(relativeUrl, args);
      window.prependListener('close', (e) => {
        const isLastWindow = electron.BrowserWindow.getAllWindows().length === 1;
        if (!appQuit && isLastWindow) {
          window.hide();
          e.preventDefault();
          throw new Error('<fake error>: prevent window close');
        }
      });

      // no other windows yet + opened at startup = hide
      const wasOpenedAtStartup =
        process.argv.includes('--startup') ||
        app.getLoginItemSettings({ args: ['--startup'] }).wasOpenedAtLogin;
      if (!windows.length && wasOpenedAtStartup) {
        window.once('ready-to-show', () => window.hide());
      }

      return window;
    } else {
      const window = api.electron.getFocusedNotionWindow() || windows[0];
      // prevents duplicate windows on dock/taskbar click
      window.focus();
      if (relativeUrl) {
        // handle requests passed via the notion:// protocol
        // or ctrl+click
        window.loadURL(urlHelpers.getIndexUrl(relativeUrl));
      }
      return window;
    }
  };
};
