"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ShortcutModule = void 0;
const tslib_1 = require("tslib");
const AdaptableModuleBase_1 = require("./AdaptableModuleBase");
const ModuleConstants = tslib_1.__importStar(require("../Utilities/Constants/ModuleConstants"));
const ShortcutRedux = tslib_1.__importStar(require("../Redux/ActionsReducers/ShortcutRedux"));
const Enums_1 = require("../AdaptableState/Common/Enums");
const ArrayExtensions_1 = require("../Utilities/Extensions/ArrayExtensions");
const StringExtensions_1 = tslib_1.__importDefault(require("../Utilities/Extensions/StringExtensions"));
const ShortcutWizard_1 = require("../View/Shortcut/Wizard/ShortcutWizard");
const getShortcutSettingsViewItems_1 = require("./Utilities/Shortcut/getShortcutSettingsViewItems");
const getObjectTagsViewItems_1 = require("../Utilities/getObjectTagsViewItems");
const getScopeViewItems_1 = require("../Utilities/getScopeViewItems");
class ShortcutModule extends AdaptableModuleBase_1.AdaptableModuleBase {
    constructor(api) {
        super(ModuleConstants.ShortcutModuleId, ModuleConstants.ShortcutFriendlyName, 'link', 'ShortcutPopup', 'Set up rules so cells update mathematically when keys are pressed in numeric cells', api);
        this.shouldHandleKeyDown = false;
        this.adaptable = api.internalApi.getAdaptableInstance();
    }
    getModuleAdaptableObjects(config) {
        return this.api.shortcutApi.getShortcuts(config);
    }
    hasNamedQueryReferences() {
        return false;
    }
    getTeamSharingAction() {
        return {
            ModuleEntities: this.api.shortcutApi.getShortcuts(),
            AddAction: ShortcutRedux.ShortcutAdd,
            EditAction: ShortcutRedux.ShortcutEdit,
        };
    }
    onAdaptableReady() {
        this.checkListenToKeyDown();
        if (this.shouldHandleKeyDown) {
            this.setupKeyDownListener();
        }
    }
    setupKeyDownListener() {
        this.adaptable._on('CellEditorKeyDown', (cellEditorKeyDownEvent) => {
            this.handleKeyDown(cellEditorKeyDownEvent);
        });
    }
    checkListenToKeyDown() {
        const shortcuts = this.api.shortcutApi.getShortcuts();
        this.shouldHandleKeyDown = ArrayExtensions_1.ArrayExtensions.IsNotNullOrEmpty(shortcuts);
        if (this.shouldHandleKeyDown) {
            this.setupKeyDownListener();
        }
    }
    handleKeyDown(cellEditorKeyDownEvent) {
        if (!this.shouldHandleKeyDown ||
            StringExtensions_1.default.IsNullOrEmpty(cellEditorKeyDownEvent.cellValue)) {
            return;
        }
        const { keyDownEvent, cellValue, columnId, updateValueCallback } = cellEditorKeyDownEvent;
        const matchingShortcut = this.api.shortcutApi
            .getActiveShortcuts()
            .find((x) => keyDownEvent.key.toLowerCase() === x.ShortcutKey.toLowerCase() &&
            this.api.columnScopeApi.isColumnInScope(this.api.columnApi.getColumnWithColumnId(columnId), x.Scope));
        if (!matchingShortcut || !matchingShortcut.ShortcutValue) {
            return;
        }
        const newValue = this.calculateShortcut(cellValue, matchingShortcut.ShortcutValue, matchingShortcut.ShortcutOperation);
        // update the editor value
        updateValueCallback(newValue);
    }
    calculateShortcut(first, second, shortcutOperation) {
        let firstNumber = Number(first);
        let secondNumber = Number(second);
        switch (shortcutOperation) {
            case Enums_1.MathOperation.Add:
                return firstNumber + secondNumber;
            case Enums_1.MathOperation.Subtract:
                return firstNumber - secondNumber;
            case Enums_1.MathOperation.Multiply:
                return firstNumber * secondNumber;
            case Enums_1.MathOperation.Divide:
                return firstNumber / secondNumber;
        }
    }
    toView(shortcut) {
        return {
            abObject: shortcut,
            items: [
                (0, getScopeViewItems_1.getScopeViewItems)(shortcut.Scope, this.api),
                (0, getShortcutSettingsViewItems_1.getShortcutSettingsViewItems)(shortcut),
                (0, getObjectTagsViewItems_1.getObjectTagsViewItems)(shortcut, this.api),
            ],
        };
    }
    toViewAll() {
        return this.getModuleAdaptableObjects({
            includeLayoutNotExtendedObjects: this.showLayoutNonExtendedObjects(),
        }).map((shortcut) => this.toView(shortcut));
    }
    getViewProperties() {
        return {
            getEditAction: ShortcutRedux.ShortcutEdit,
            getDeleteAction: ShortcutRedux.ShortcutDelete,
            getSuspendAction: ShortcutRedux.ShortcutSuspend,
            getUnSuspendAction: ShortcutRedux.ShortcutUnSuspend,
            getSuspendAllAction: ShortcutRedux.ShortcutSuspendAll,
            getUnSuspendAllAction: ShortcutRedux.ShortcutUnSuspendAll,
            getEditWizard() {
                return ShortcutWizard_1.ShortcutWizard;
            },
        };
    }
    containsLayoutExtensions() {
        return true;
    }
}
exports.ShortcutModule = ShortcutModule;
