"use strict";

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

const vtable_1 = require("@visactor/vtable"), autoFillHandle_1 = require("./fillHandleUtils/autoFillHandle");

class WpsFillHandlePlugin {
    constructor(pluginOptions) {
        var _a;
        this.id = "wps-fill-handle", this.name = "WPS Fill Handle", this.runTime = [ vtable_1.TABLE_EVENT_TYPE.MOUSEDOWN_FILL_HANDLE, vtable_1.TABLE_EVENT_TYPE.DRAG_FILL_HANDLE_END ], 
        this.beforeDragMinCol = 0, this.beforeDragMinRow = 0, this.beforeDragMaxCol = 0, 
        this.beforeDragMaxRow = 0, this.id = null !== (_a = null == pluginOptions ? void 0 : pluginOptions.id) && void 0 !== _a ? _a : this.id, 
        this.pluginOptions = pluginOptions;
    }
    run(...args) {
        var _a, _b, _c, _d, _e;
        if (args[1] === vtable_1.TABLE_EVENT_TYPE.MOUSEDOWN_FILL_HANDLE) {
            args[0];
            const table = args[2];
            this.table = table;
            const startSelectCellRange = null === (_a = this.table) || void 0 === _a ? void 0 : _a.getSelectedCellRanges()[0];
            if (!startSelectCellRange) return;
            this.beforeDragMaxCol = Math.max(startSelectCellRange.start.col, startSelectCellRange.end.col), 
            this.beforeDragMinCol = Math.min(startSelectCellRange.start.col, startSelectCellRange.end.col), 
            this.beforeDragMaxRow = Math.max(startSelectCellRange.start.row, startSelectCellRange.end.row), 
            this.beforeDragMinRow = Math.min(startSelectCellRange.start.row, startSelectCellRange.end.row);
        } else if (args[1] === vtable_1.TABLE_EVENT_TYPE.DRAG_FILL_HANDLE_END) {
            const direction = args[0].direction;
            let endChangeCellCol, endChangeCellRow;
            const endSelectCellRange = null === (_b = this.table) || void 0 === _b ? void 0 : _b.getSelectedCellRanges()[0];
            if (!endSelectCellRange) return;
            "bottom" === direction ? (endChangeCellCol = this.beforeDragMaxCol, endChangeCellRow = endSelectCellRange.end.row) : "right" === direction ? (endChangeCellCol = endSelectCellRange.end.col, 
            endChangeCellRow = this.beforeDragMaxRow) : "top" === direction ? (endChangeCellCol = this.beforeDragMaxCol, 
            endChangeCellRow = endSelectCellRange.end.row) : "left" === direction && (endChangeCellCol = endSelectCellRange.end.col, 
            endChangeCellRow = this.beforeDragMaxRow);
            const rowDatas = null === (_c = this.table) || void 0 === _c ? void 0 : _c.records, tableColumns = (null === (_d = this.table) || void 0 === _d ? void 0 : _d.getAllColumnHeaderCells()) || [];
            let highestColumns = [];
            (null == tableColumns ? void 0 : tableColumns.length) && (highestColumns = tableColumns[tableColumns.length - 1]);
            const newDatas = (0, autoFillHandle_1.generateAutoFillData)(rowDatas, highestColumns, {
                startRow: this.beforeDragMinRow - tableColumns.length,
                startCol: this.beforeDragMinCol,
                endRow: this.beforeDragMaxRow - tableColumns.length,
                endCol: this.beforeDragMaxCol
            }, {
                row: endChangeCellRow - tableColumns.length,
                col: endChangeCellCol
            });
            null === (_e = this.table) || void 0 === _e || _e.setRecords(newDatas);
        }
    }
    handleKeyDown() {
        var _a;
        if (this.table.editorManager) {
            const startSelectCellRange = null === (_a = this.table) || void 0 === _a ? void 0 : _a.getSelectedCellRanges()[0];
            if (!startSelectCellRange) return;
            this.beforeDragMaxCol = Math.max(startSelectCellRange.start.col, startSelectCellRange.end.col), 
            this.beforeDragMinCol = Math.min(startSelectCellRange.start.col, startSelectCellRange.end.col), 
            this.beforeDragMaxRow = Math.max(startSelectCellRange.start.row, startSelectCellRange.end.row), 
            this.beforeDragMinRow = Math.min(startSelectCellRange.start.row, startSelectCellRange.end.row);
        }
    }
    release() {
        this.beforeDragMinCol = 0, this.beforeDragMinRow = 0, this.beforeDragMaxCol = 0, 
        this.beforeDragMaxRow = 0;
    }
}

function deleteSelectRange(selectCells, tableInstance) {
    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, "");
}

//# sourceMappingURL=wps-fill-handle.js.map
exports.WpsFillHandlePlugin = WpsFillHandlePlugin;