"use strict";
var __values = (this && this.__values) || function (o) {
    var m = typeof Symbol === "function" && o[Symbol.iterator], i = 0;
    if (m) return m.call(o);
    return {
        next: function () {
            if (o && i >= o.length) o = void 0;
            return { value: o && o[i++], done: !o };
        }
    };
};
Object.defineProperty(exports, "__esModule", { value: true });
var electron_1 = require("electron");
var Menu = electron_1.remote.Menu;
var webContents = electron_1.remote.getCurrentWebContents();
var config = require("../js/config");
var eventType_1 = require("../lib/eventType");
var locale = require("../locale/locale");
var ConfigurationPersistence_1 = require("../settings/ConfigurationPersistence");
var SettingsType_1 = require("../settings/SettingsType");
var textMenu;
var copyContext = '';
var defaultMenu = Menu.buildFromTemplate([
    {
        click: function () { return electron_1.clipboard.writeText(copyContext); },
        label: locale.getText('menuCopy'),
    },
]);
var selection = {
    isMisspelled: false,
    suggestions: [],
};
var textMenuTemplate = [
    {
        label: locale.getText('menuCut'),
        role: 'cut',
    },
    {
        label: locale.getText('menuCopy'),
        role: 'copy',
    },
    {
        label: locale.getText('menuPaste'),
        role: 'paste',
    },
    {
        type: 'separator',
    },
    {
        label: locale.getText('menuSelectAll'),
        role: 'selectall',
    },
];
var createTextMenu = function () {
    var e_1, _a;
    var template = textMenuTemplate.slice();
    if (selection.isMisspelled) {
        template.unshift({ type: 'separator' });
        if (selection.suggestions.length > 0) {
            try {
                for (var _b = __values(selection.suggestions.reverse()), _c = _b.next(); !_c.done; _c = _b.next()) {
                    var suggestion = _c.value;
                    template.unshift({
                        click: function (menuItem) { return webContents.replaceMisspelling(menuItem.label); },
                        label: suggestion,
                    });
                }
            }
            catch (e_1_1) { e_1 = { error: e_1_1 }; }
            finally {
                try {
                    if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
                }
                finally { if (e_1) throw e_1.error; }
            }
        }
        else {
            template.unshift({
                enabled: false,
                label: locale.getText('menuNoSuggestions'),
            });
        }
    }
    textMenu = Menu.buildFromTemplate(template);
};
var imageMenu = Menu.buildFromTemplate([
    {
        click: function () { return savePicture(imageMenu.file || '', imageMenu.image || ''); },
        label: locale.getText('menuSavePictureAs'),
    },
]);
window.addEventListener('contextmenu', function (event) {
    var element = event.target;
    copyContext = '';
    if (element.nodeName === 'TEXTAREA' || element.nodeName === 'INPUT') {
        event.preventDefault();
        createTextMenu();
        textMenu.popup({ window: electron_1.remote.getCurrentWindow() });
    }
    else if (element.classList.contains('image-element') || element.classList.contains('detail-view-image')) {
        event.preventDefault();
        var elementSource = element.src;
        imageMenu.image = elementSource;
        imageMenu.popup({ window: electron_1.remote.getCurrentWindow() });
    }
    else if (element.nodeName === 'A') {
        event.preventDefault();
        var elementHref = element.href;
        copyContext = elementHref.replace(/^mailto:/, '');
        defaultMenu.popup({ window: electron_1.remote.getCurrentWindow() });
    }
    else if (element.classList.contains('text')) {
        event.preventDefault();
        copyContext = window.getSelection().toString() || element.innerText.trim();
        defaultMenu.popup({ window: electron_1.remote.getCurrentWindow() });
    }
    else {
        var parentNode = element.parentNode;
        while (parentNode && parentNode !== document && !parentNode.classList.contains('text')) {
            parentNode = parentNode.parentNode;
        }
        if (parentNode !== document) {
            event.preventDefault();
            copyContext = window.getSelection().toString() || parentNode.innerText.trim();
            defaultMenu.popup({ window: electron_1.remote.getCurrentWindow() });
        }
    }
}, false);
var savePicture = function (fileName, url) {
    return fetch(url, {
        headers: {
            'User-Agent': config.USER_AGENT,
        },
    })
        .then(function (response) { return response.arrayBuffer(); })
        .then(function (arrayBuffer) { return electron_1.ipcRenderer.send(eventType_1.EVENT_TYPE.ACTION.SAVE_PICTURE, fileName, new Uint8Array(arrayBuffer)); });
};
var isSpellCheckSupported = config.SPELLCHECK.SUPPORTED_LANGUAGES.includes(locale.getCurrent());
if (isSpellCheckSupported) {
    var spellchecker_1 = require('spellchecker');
    electron_1.webFrame.setSpellCheckProvider(locale.getCurrent(), false, {
        spellCheck: function (text) {
            var isSpellCheckEnabled = ConfigurationPersistence_1.settings.restore(SettingsType_1.SettingsType.SPELL_CHECK, false);
            if (!isSpellCheckEnabled) {
                return true;
            }
            selection.isMisspelled = spellchecker_1.isMisspelled(text);
            selection.suggestions = [];
            if (selection.isMisspelled) {
                selection.suggestions = spellchecker_1.getCorrectionsForMisspelling(text).slice(0, config.SPELLCHECK.SUGGESTIONS);
            }
            return !selection.isMisspelled;
        },
    });
}
//# sourceMappingURL=context.js.map