'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.deferURL = deferURL;
exports.launch = launch;
exports.launchNewNotebook = launchNewNotebook;

var _path = require('path');

var _path2 = _interopRequireDefault(_path);

var _electron = require('electron');

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function deferURL(event, url) {
  event.preventDefault();
  _electron.shell.openExternal(url);
}

const iconPath = _path2.default.join(__dirname, '../../static/icon.png');

function launch(filename) {
  let win = new _electron.BrowserWindow({
    width: 800,
    height: 1000,
    icon: iconPath,
    title: 'nteract'
  });

  const index = _path2.default.join(__dirname, '..', '..', 'static', 'index.html');
  win.loadURL(`file://${ index }`);

  win.webContents.on('did-finish-load', () => {
    if (filename) {
      win.webContents.send('main:load', filename);
    }
    // TODO: else, we assume it's an empty notebook
    //       assumption right now is that launchNewNotebook will handle the follow on
  });

  win.webContents.on('will-navigate', deferURL);

  // Emitted when the window is closed.
  win.on('closed', () => {
    win = null;
  });
  return win;
}

function launchNewNotebook(kernelSpecName) {
  const win = launch();
  win.webContents.on('did-finish-load', () => {
    win.webContents.send('main:new', kernelSpecName);
  });
  return win;
}