"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var path = require("path");
var electron_1 = require("electron");
var windowState = require("electron-window-state");
var config_1 = require("./config");
var menu = require("./menu");
var watcher_1 = require("./watcher");
if (process.argv.indexOf('--version') !== -1) {
    var versions = process.versions;
    console.log("Shiba v" + electron_1.app.getVersion() + "\n\nEnvironment:\n  OS:       " + process.platform + "-" + process.arch + "\n  Electron: " + versions.electron + "\n  Chromium: " + versions.chrome + "\n  Node.js:  " + versions.node + "\n");
    electron_1.app.exit();
}
function createWindow(config, icon_path) {
    var display_size = electron_1.screen.getPrimaryDisplay().workAreaSize;
    function getConfigLength(key) {
        var len = config[key];
        var default_len = config_1.default_config[key];
        switch (typeof len) {
            case 'string': {
                if (len === 'max') {
                    return display_size[key];
                }
                return default_len;
            }
            case 'number': {
                return len;
            }
            default: {
                return default_len;
            }
        }
    }
    var config_width = getConfigLength('width');
    var config_height = getConfigLength('height');
    var win_state = windowState({
        defaultWidth: config_width,
        defaultHeight: config_height,
    });
    var options;
    if (config.restore_window_state) {
        options = {
            x: win_state.x,
            y: win_state.y,
            width: win_state.width,
            height: win_state.height,
        };
    }
    else {
        options = {
            width: config_width,
            height: config_height,
        };
    }
    options.icon = icon_path;
    options.autoHideMenuBar = config.hide_menu_bar;
    options.show = false;
    if (config.hide_title_bar) {
        options.titleBarStyle = 'hiddenInset';
    }
    var win = new electron_1.BrowserWindow(options);
    win.once('ready-to-show', function () {
        win.show();
    });
    if (config.restore_window_state) {
        if (win_state.isFullScreen) {
            win.setFullScreen(true);
        }
        else if (win_state.isMaximized) {
            win.maximize();
        }
        win_state.manage(win);
    }
    return win;
}
var loading = config_1.default().then(function (config) { return [config, new watcher_1.default(config)]; });
electron_1.app.once('window-all-closed', function () { electron_1.app.quit(); });
electron_1.app.once('ready', function () {
    loading.then(function (loaded) {
        var config = loaded[0], dog = loaded[1];
        global.config = config;
        var icon_path = path.join(__dirname, '..', '..', 'images', 'shibainu.png');
        var win = createWindow(config, icon_path);
        win.once('closed', function () {
            win = null;
        });
        var html = 'file://' + path.join(__dirname, '..', '..', 'static', 'index.html');
        win.loadURL(html);
        dog.wakeup(win.webContents);
        win.webContents.on('will-navigate', function (e, url) {
            e.preventDefault();
            electron_1.shell.openExternal(url);
        });
        menu.build(win);
        if (process.argv[0].endsWith('Electron') && process.platform === 'darwin') {
            electron_1.app.dock.setIcon(icon_path);
        }
        if (process.env.NODE_ENV === 'development') {
            win.webContents.once('devtools-opened', function () { return setImmediate(function () { return win.focus(); }); });
            win.webContents.openDevTools({ mode: 'detach' });
        }
    }).catch(function (e) {
        console.error('Unknown error: ', e);
        electron_1.app.quit();
    });
});
//# sourceMappingURL=mainu.js.map