import { __esDecorate, __runInitializers } from "tslib";
import { ioc, keyType, ShortcutChord, ShortcutHandler, System } from '@journeyapps-labs/reactor-mod';
import { MonacoShortcut } from './MonacoShortcut';
import { MonacoMapInverted } from './MonacoShortcutMap';
import { action } from 'mobx';
let MonacoShortcutHandler = (() => {
    var _a;
    let _classSuper = ShortcutHandler;
    let _instanceExtraInitializers = [];
    let _deserialize_decorators;
    return _a = class MonacoShortcutHandler extends _classSuper {
            constructor(options2) {
                super({
                    key: 'monaco-shortcuts',
                    type: `${ioc.get(System).ideName} Editor`,
                    serializeID: 'v2.0.0'
                });
                this.options2 = (__runInitializers(this, _instanceExtraInitializers), options2);
            }
            get store() {
                return this.options2.keybindingStore;
            }
            async deserialize(data) {
                await this.store.waitForReady();
                await super.deserialize(data);
                // unbind defaults which have been user-removed (this code is only necessary on initial deserialization)
                this.getPossibleActions().forEach((action) => {
                    const existing = this.getShortcutsForAction(action);
                    action.defaultKeybindings.forEach((k) => {
                        if (!existing.find((shortcut) => shortcut.chord.uuid === k.uuid)) {
                            const s = this.generateShortcut(action);
                            s.setChord(k, false);
                            s.dispose();
                        }
                    });
                });
            }
            getMetaForAction(id) {
                const action = this.store.getActionFromID(id);
                if (!action) {
                    this.logger.info(`could not find action ${id}`);
                    return null;
                }
                return {
                    handler: this,
                    label: action.label,
                    id: id,
                    supportsMultipleShortcuts: action.bindings.length < 2,
                    defaultKeybindings: action.bindings.map((binding) => new ShortcutChord(this.getShortcutKeys(binding, true), this.getShortcutKeys(binding, false)))
                };
            }
            getPossibleActions() {
                // some actions should not be shown to the user
                const HIDDEN = [`Command palette`];
                return this.store.actions
                    .filter((action) => HIDDEN.indexOf(action.label) === -1)
                    .map((action) => {
                    return this.getMetaForAction(action.id);
                });
            }
            getShortcutKeys(item, primary) {
                const items = [];
                const entry = item.chords[primary ? 0 : 1];
                if (!entry) {
                    return [];
                }
                if (entry.altKey) {
                    items.push({
                        key: '',
                        type: keyType.ALT
                    });
                }
                if (entry.ctrlKey) {
                    items.push({
                        key: '',
                        type: keyType.CTRL
                    });
                }
                if (entry.metaKey) {
                    items.push({
                        key: '',
                        type: keyType.META
                    });
                }
                if (entry.shiftKey) {
                    items.push({
                        key: '',
                        type: keyType.SHIFT
                    });
                }
                if (entry.keyCode) {
                    const code = MonacoMapInverted[`${entry.keyCode}`];
                    if (!code) {
                        console.log('unknown code', entry.keyCode);
                    }
                    else {
                        items.push({
                            key: code,
                            type: keyType.STANDARD
                        });
                    }
                }
                return items;
            }
            generateShortcut(data) {
                return new MonacoShortcut(data);
            }
        },
        (() => {
            var _b;
            const _metadata = typeof Symbol === "function" && Symbol.metadata ? Object.create((_b = _classSuper[Symbol.metadata]) !== null && _b !== void 0 ? _b : null) : void 0;
            _deserialize_decorators = [action];
            __esDecorate(_a, null, _deserialize_decorators, { kind: "method", name: "deserialize", static: false, private: false, access: { has: obj => "deserialize" in obj, get: obj => obj.deserialize }, metadata: _metadata }, null, _instanceExtraInitializers);
            if (_metadata) Object.defineProperty(_a, Symbol.metadata, { enumerable: true, configurable: true, writable: true, value: _metadata });
        })(),
        _a;
})();
export { MonacoShortcutHandler };
//# sourceMappingURL=MonacoShortcutHandler.js.map