"use strict";

Object.defineProperty(exports, "__esModule", {
    value: !0
}), exports.ExcelEditCellKeyboardPlugin = exports.ExcelEditCellKeyboardResponse = void 0;

const vtable_1 = require("@visactor/vtable");

var ExcelEditCellKeyboardResponse;

!function(ExcelEditCellKeyboardResponse) {
    ExcelEditCellKeyboardResponse.ENTER = "enter", ExcelEditCellKeyboardResponse.TAB = "tab", 
    ExcelEditCellKeyboardResponse.ARROW_LEFT = "arrowLeft", ExcelEditCellKeyboardResponse.ARROW_RIGHT = "arrowRight", 
    ExcelEditCellKeyboardResponse.ARROW_DOWN = "arrowDown", ExcelEditCellKeyboardResponse.ARROW_UP = "arrowUp", 
    ExcelEditCellKeyboardResponse.DELETE = "delete", ExcelEditCellKeyboardResponse.BACKSPACE = "backspace";
}(ExcelEditCellKeyboardResponse = exports.ExcelEditCellKeyboardResponse || (exports.ExcelEditCellKeyboardResponse = {}));

class ExcelEditCellKeyboardPlugin {
    constructor(pluginOptions) {
        var _a, _b;
        this.id = "excel-edit-cell-keyboard", this.name = "Excel Edit Cell Keyboard", this.runTime = [ vtable_1.TABLE_EVENT_TYPE.INITIALIZED ], 
        this.id = null !== (_a = null == pluginOptions ? void 0 : pluginOptions.id) && void 0 !== _a ? _a : this.id, 
        this.pluginOptions = pluginOptions, this.responseKeyboard = null !== (_b = null == pluginOptions ? void 0 : pluginOptions.responseKeyboard) && void 0 !== _b ? _b : [ ExcelEditCellKeyboardResponse.ENTER, ExcelEditCellKeyboardResponse.TAB, ExcelEditCellKeyboardResponse.ARROW_LEFT, ExcelEditCellKeyboardResponse.ARROW_RIGHT, ExcelEditCellKeyboardResponse.ARROW_DOWN, ExcelEditCellKeyboardResponse.ARROW_UP, ExcelEditCellKeyboardResponse.DELETE, ExcelEditCellKeyboardResponse.BACKSPACE ], 
        this.bindEvent();
    }
    run(...args) {
        const table = args[2];
        this.table = table;
    }
    bindEvent() {
        document.addEventListener("keydown", this.handleKeyDown.bind(this), !0);
    }
    handleKeyDown(event) {
        var _a, _b, _c;
        if ((null === (_a = this.table) || void 0 === _a ? void 0 : _a.editorManager) && this.isExcelShortcutKey(event)) {
            const eventKey = event.key.toLowerCase();
            if (this.table.editorManager.editingEditor && "keydown" === this.table.editorManager.beginTriggerEditCellMode) {
                const {col: col, row: row} = this.table.editorManager.editCell;
                if (eventKey === ExcelEditCellKeyboardResponse.BACKSPACE || eventKey === ExcelEditCellKeyboardResponse.DELETE) return;
                this.table.editorManager.completeEdit(), this.table.getElement().focus(), event.shiftKey || event.ctrlKey || event.metaKey || (eventKey === ExcelEditCellKeyboardResponse.ENTER ? this.table.selectCell(col, row + 1) : eventKey === ExcelEditCellKeyboardResponse.TAB ? this.table.selectCell(col + 1, row) : eventKey === ExcelEditCellKeyboardResponse.ARROW_LEFT ? this.table.selectCell(col - 1, row) : eventKey === ExcelEditCellKeyboardResponse.ARROW_RIGHT ? this.table.selectCell(col + 1, row) : eventKey === ExcelEditCellKeyboardResponse.ARROW_DOWN ? this.table.selectCell(col, row + 1) : eventKey === ExcelEditCellKeyboardResponse.ARROW_UP && this.table.selectCell(col, row - 1), 
                event.stopPropagation(), event.preventDefault());
            } else {
                const {col: col, row: row} = this.table.stateManager.select.cellPos;
                if (!this.table.editorManager.editingEditor || eventKey !== ExcelEditCellKeyboardResponse.ENTER && eventKey !== ExcelEditCellKeyboardResponse.TAB) {
                    if (!this.table.editorManager.editingEditor && (eventKey === ExcelEditCellKeyboardResponse.DELETE || eventKey === ExcelEditCellKeyboardResponse.BACKSPACE)) {
                        const selectCells = this.table.getSelectedCellInfos();
                        (null == selectCells ? void 0 : selectCells.length) > 0 && (document.activeElement === this.table.getElement() || Object.values(this.table.editorManager.cacheLastSelectedCellEditor || {}).some((editor => {
                            var _a;
                            return (null === (_a = editor.getInputElement) || void 0 === _a ? void 0 : _a.call(editor)) === document.activeElement;
                        }))) && (deleteSelectRange(selectCells, this.table, null === (_c = null === (_b = this.pluginOptions) || void 0 === _b ? void 0 : _b.deleteWorkOnEditableCell) || void 0 === _c || _c), 
                        event.stopPropagation(), event.preventDefault());
                    }
                } else this.table.editorManager.completeEdit(), this.table.getElement().focus(), 
                eventKey === ExcelEditCellKeyboardResponse.ENTER ? this.table.selectCell(col, row + 1) : eventKey === ExcelEditCellKeyboardResponse.TAB && this.table.selectCell(col + 1, row), 
                event.stopPropagation(), event.preventDefault();
            }
        }
    }
    isExcelShortcutKey(event) {
        return this.responseKeyboard.includes(event.key.toLowerCase());
    }
    setResponseKeyboard(responseKeyboard) {
        this.responseKeyboard = responseKeyboard;
    }
    deleteResponseKeyboard(responseKeyboard) {
        this.responseKeyboard = this.responseKeyboard.filter((key => !responseKeyboard.includes(key)));
    }
    addResponseKeyboard(responseKeyboard) {
        this.responseKeyboard = [ ...this.responseKeyboard, ...responseKeyboard ];
    }
    release() {
        document.removeEventListener("keydown", this.handleKeyDown, !0);
    }
}

function deleteSelectRange(selectCells, tableInstance, workOnEditableCell = !1) {
    for (let i = 0; i < selectCells.length; i++) for (let j = 0; j < selectCells[i].length; j++) tableInstance.changeCellValue(selectCells[i][j].col, selectCells[i][j].row, "", workOnEditableCell);
}

//# sourceMappingURL=excel-edit-cell-keyboard.js.map
exports.ExcelEditCellKeyboardPlugin = ExcelEditCellKeyboardPlugin;