'use strict';

var _electron = require('electron');

var _path = require('path');

var _launch = require('./launch');

var _menu = require('./menu');

const log = require('electron-log');

const version = require('../../package.json').version;

const argv = require('yargs').version(version).parse(process.argv.slice(2));

const notebooks = argv._;

_electron.app.on('window-all-closed', () => {
  // On OS X, we want to keep the app and menu bar active
  if (process.platform !== 'darwin') {
    _electron.app.quit();
  }
});

_electron.app.on('open-file', (event, path) => {
  event.preventDefault();
  (0, _launch.launch)((0, _path.resolve)(path));
});

_electron.ipcMain.on('new-kernel', (event, newKernel) => {
  (0, _launch.launchNewNotebook)(newKernel);
});

_electron.ipcMain.on('open-notebook', (event, filename) => {
  (0, _launch.launch)((0, _path.resolve)(filename));
});

_electron.app.on('ready', () => {
  log.info('app in ready state');

  // Get the default menu first
  _electron.Menu.setApplicationMenu(_menu.defaultMenu);
  // Let the kernels/languages come in after
  (0, _menu.loadFullMenu)().then(menu => _electron.Menu.setApplicationMenu(menu));
  if (notebooks.length <= 0) {
    log.info('launching an empty notebook by default');
    (0, _launch.launchNewNotebook)('python3');
  } else {
    notebooks.filter(Boolean).filter(x => x !== '.') // Ignore the `electron .`
    // TODO: Consider opening something for directories
    .forEach(f => (0, _launch.launch)((0, _path.resolve)(f)));
  }
});