"use strict";

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

class MenuHandler {
    constructor() {}
    handleCopy(table) {
        table.eventManager.handleCopy(new KeyboardEvent("copy"));
    }
    handleCut(table) {
        table.eventManager.handleCut(new KeyboardEvent("cut"));
    }
    handlePaste(table) {
        table.eventManager.handlePaste(new KeyboardEvent("paste"));
    }
    handleInsertRowAbove(table, rowIndex, count = 1) {
        if (void 0 !== rowIndex && "function" == typeof table.addRecord) {
            const records = Array.from({
                length: count
            }, ((_, i) => []));
            table.addRecords(records, rowIndex - 1 - table.columnHeaderLevelCount + 1);
        }
    }
    handleInsertRowBelow(table, rowIndex, count = 1) {
        if (void 0 !== rowIndex && "function" == typeof table.addRecord) {
            const records = Array.from({
                length: count
            }, ((_, i) => []));
            table.addRecords(records, rowIndex - table.columnHeaderLevelCount + 1);
        }
    }
    handleInsertColumnLeft(table, colIndex, count = 1) {
        if (void 0 !== colIndex && "function" == typeof table.addColumns) {
            const toAddColumns = [];
            for (let i = 0; i < count; i++) toAddColumns.push({
                field: colIndex - i
            });
            table.addColumns(toAddColumns, colIndex, !0);
        }
    }
    handleInsertColumnRight(table, colIndex, count = 1) {
        if (void 0 !== colIndex && "function" == typeof table.addColumns) {
            const toAddColumns = [];
            for (let i = 0; i < count; i++) toAddColumns.push({
                field: colIndex + 1
            });
            table.addColumns(toAddColumns, colIndex + 1, !0);
        }
    }
    handleDeleteRow(table) {
        if ("function" == typeof table.deleteRecords) {
            const selectRanges = table.stateManager.select.ranges, deleteRowIndexs = [];
            for (let i = 0; i < selectRanges.length; i++) {
                const range = selectRanges[i];
                for (let j = range.start.row; j <= range.end.row; j++) deleteRowIndexs.includes(j) || deleteRowIndexs.push(j);
            }
            deleteRowIndexs.sort(((a, b) => b - a));
            const delRecordIndexs = [];
            for (let i = 0; i < deleteRowIndexs.length; i++) {
                const recordIndex = table.getRecordIndexByCell(0, deleteRowIndexs[i]);
                delRecordIndexs.push(recordIndex);
            }
            table.deleteRecords(delRecordIndexs);
        }
    }
    handleDeleteColumn(table, colIndex) {
        if (void 0 !== colIndex && "function" == typeof table.deleteColumns) {
            const selectRanges = table.stateManager.select.ranges, deleteColIndexs = [];
            for (let i = 0; i < selectRanges.length; i++) {
                const range = selectRanges[i];
                for (let j = range.start.col; j <= range.end.col; j++) deleteColIndexs.includes(j) || deleteColIndexs.push(j);
            }
            table.deleteColumns(deleteColIndexs, !0);
        }
    }
    handleHideColumn(table, colIndex) {
        void 0 !== colIndex && "function" == typeof table.hideColumns && table.hideColumns([ colIndex ]);
    }
    handleSort(table, colIndex) {
        if (void 0 !== colIndex && "function" == typeof table.sort) {
            const currentSortState = table.getSortState ? table.getSortState() : null;
            let direction = "asc";
            currentSortState && currentSortState.field === colIndex && (direction = "asc" === currentSortState.order ? "desc" : "normal"), 
            table.sort({
                field: colIndex,
                order: direction
            });
        }
    }
    handleMergeCells(table) {
        var _a, _b;
        if (1 === (null !== (_b = null === (_a = table.stateManager.select.ranges) || void 0 === _a ? void 0 : _a.length) && void 0 !== _b ? _b : 0) && "function" == typeof table.mergeCells) {
            const {row: startRow, col: startCol} = table.stateManager.select.ranges[0].start, {row: endRow, col: endCol} = table.stateManager.select.ranges[0].end;
            if (startCol === endCol && startRow === endRow) return;
            table.mergeCells(Math.min(startCol, endCol), Math.min(startRow, endRow), Math.max(startCol, endCol), Math.max(startRow, endRow));
        }
    }
    handleUnmergeCells(table) {
        var _a, _b;
        if (1 === (null !== (_b = null === (_a = table.stateManager.select.ranges) || void 0 === _a ? void 0 : _a.length) && void 0 !== _b ? _b : 0) && "function" == typeof table.unmergeCells) {
            const {row: startRow, col: startCol} = table.stateManager.select.ranges[0].start, {row: endRow, col: endCol} = table.stateManager.select.ranges[0].end;
            if (startCol === endCol && startRow === endRow) return;
            table.unmergeCells(Math.min(startCol, endCol), Math.min(startRow, endRow), Math.max(startCol, endCol), Math.max(startRow, endRow));
        }
    }
    handleSetProtection(table) {}
    handleFreezeToRow(table, rowIndex) {
        void 0 !== rowIndex && (table.frozenRowCount = rowIndex + 1);
    }
    handleFreezeToColumn(table, colIndex) {
        void 0 !== colIndex && (table.frozenColCount = colIndex + 1);
    }
    handleFreezeToRowAndColumn(table, rowIndex, colIndex) {
        void 0 !== rowIndex && void 0 !== colIndex && (table.frozenRowCount = rowIndex + 1, 
        table.frozenColCount = colIndex + 1);
    }
    handleUnfreeze(table) {
        table.frozenRowCount = 0, table.frozenColCount = 0;
    }
    release() {}
}

exports.MenuHandler = MenuHandler;
//# sourceMappingURL=handle-menu-helper.js.map