"use strict";

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

const vtable_1 = require("@visactor/vtable"), vrender_1 = require("@visactor/vtable/es/vrender");

class TableSeriesNumber {
    constructor(pluginOptions) {
        this.id = "table-series-number", this.name = "Table Series Number", this.runTime = [ vtable_1.TABLE_EVENT_TYPE.INITIALIZED, vtable_1.TABLE_EVENT_TYPE.BEFORE_INIT, vtable_1.TABLE_EVENT_TYPE.BEFORE_UPDATE_OPTION, vtable_1.TABLE_EVENT_TYPE.UPDATED ], 
        this.lastCurrentRow = 0, this.lastCurrentCol = 0, this.startRowIndex = 0, this.handleScroll = () => {
            this.scrollTimer && (clearTimeout(this.scrollTimer), this.scrollTimer = null), this.scrollTimer = setTimeout((() => {
                this.scrollTimer = null, this.syncScrollToComponent();
            }), 10);
        }, this.handleAfterSort = () => {
            this.syncScrollToComponent();
        }, this.handleSelectedChanged = e => {
            this.seriesNumberComponent.removeAllSelectedIndexs();
            const selectRange = this.table.stateManager.select.ranges, rowSelectedIndexs = [], colSelectedIndexs = [];
            for (const range of selectRange) {
                const {row: rowStart, col: colStart} = range.start, {row: rowEnd, col: colEnd} = range.end;
                rowSelectedIndexs.push({
                    startIndex: Math.min(rowStart, rowEnd),
                    endIndex: Math.max(rowStart, rowEnd)
                }), colSelectedIndexs.push({
                    startIndex: Math.min(colStart, colEnd),
                    endIndex: Math.max(colStart, colEnd)
                });
            }
            this.seriesNumberComponent.addRowSelectedRanges(rowSelectedIndexs), this.seriesNumberComponent.addColSelectedRanges(colSelectedIndexs), 
            this.table.stateManager.select.isSelectAll && this.seriesNumberComponent.addCornderSelected(), 
            this.seriesNumberComponent.renderSelectedIndexsState();
        }, this.handleResizeColumnEnd = e => {
            this.seriesNumberComponent.setAttribute("hover", !0);
        }, this.handleResizeRowEnd = e => {
            this.seriesNumberComponent.setAttribute("hover", !0);
        }, this.handleChangeHeaderPosition = e => {
            this.syncColWidthToComponent();
        }, this.handleSeriesNumberCellRightClick = e => {
            const {seriesNumberCell: seriesNumberCell, event: event, isDragSelect: isDragSelect} = e.detail, isRow = seriesNumberCell.name.includes("row"), isCol = seriesNumberCell.name.includes("col");
            if (isRow) {
                const rowIndex = seriesNumberCell.id;
                this.table.stateManager.select.ranges.some((range => range.start.row <= rowIndex && rowIndex <= range.end.row && 0 === range.start.col && range.end.col === this.table.colCount - 1)) || this.table.selectCells([ {
                    start: {
                        row: rowIndex,
                        col: 0
                    },
                    end: {
                        row: rowIndex,
                        col: this.table.colCount - 1
                    }
                } ]), this.table.fireListeners(vtable_1.TABLE_EVENT_TYPE.PLUGIN_EVENT, {
                    plugin: this,
                    event: event,
                    pluginEventInfo: {
                        eventType: "rightclick",
                        rowIndex: rowIndex
                    }
                });
            } else if (isCol) {
                const colIndex = seriesNumberCell.id;
                this.table.stateManager.select.ranges.some((range => range.start.col <= colIndex && colIndex <= range.end.col && 0 === range.start.row && range.end.row === this.table.rowCount - 1)) || this.table.selectCells([ {
                    start: {
                        row: 0,
                        col: colIndex
                    },
                    end: {
                        row: this.table.rowCount - 1,
                        col: colIndex
                    }
                } ]), this.table.fireListeners(vtable_1.TABLE_EVENT_TYPE.PLUGIN_EVENT, {
                    plugin: this,
                    event: event,
                    pluginEventInfo: {
                        eventType: "rightclick",
                        colIndex: colIndex
                    }
                });
            } else this.table.selectCells([ {
                start: {
                    row: 0,
                    col: 0
                },
                end: {
                    row: this.table.rowCount - 1,
                    col: this.table.colCount - 1
                }
            } ]), this.table.fireListeners(vtable_1.TABLE_EVENT_TYPE.PLUGIN_EVENT, {
                plugin: this,
                event: event,
                pluginEventInfo: {
                    eventType: "rightclick",
                    isCorner: !0
                }
            });
        }, this.handleSeriesNumberCellHover = e => {
            this.table.scenegraph.renderSceneGraph();
        }, this.handleSeriesNumberCellUnHover = e => {
            this.table.scenegraph.renderSceneGraph();
        }, this.handleSeriesNumberCellClick = e => {
            var _a, _b, _c, _d;
            const {seriesNumberCell: seriesNumberCell, event: event, isDragSelect: isDragSelect} = e.detail, ctrlMultiSelect = null === (_b = null === (_a = this.table.options.keyboardOptions) || void 0 === _a ? void 0 : _a.ctrlMultiSelect) || void 0 === _b || _b, shiftMultiSelect = null === (_d = null === (_c = this.table.options.keyboardOptions) || void 0 === _c ? void 0 : _c.shiftMultiSelect) || void 0 === _d || _d, enableCtrlSelectMode = (event.nativeEvent.ctrlKey || event.nativeEvent.metaKey) && ctrlMultiSelect, enableShiftSelectMode = event.nativeEvent.shiftKey && shiftMultiSelect, isRow = seriesNumberCell.name.includes("row"), isCol = seriesNumberCell.name.includes("col");
            if (isRow) {
                this.table.stateManager.setSelectInline("row");
                const rowIndex = seriesNumberCell.id;
                isDragSelect ? this.table.dragSelectRow(rowIndex, enableCtrlSelectMode) : this.table.startDragSelectRow(rowIndex, enableCtrlSelectMode, enableShiftSelectMode);
            } else if (isCol) {
                this.table.stateManager.setSelectInline("col");
                const colIndex = seriesNumberCell.id;
                isDragSelect ? this.table.dragSelectCol(colIndex, enableCtrlSelectMode) : this.table.startDragSelectCol(colIndex, enableCtrlSelectMode, enableShiftSelectMode);
            } else this.table.eventManager.deelTableSelectAll();
        }, this.handleSeriesNumberCellClickUp = e => {
            this.table.stateManager.setSelectInline(!1), this.table.endDragSelect();
        }, this.handleRowSeriesNumberWidthChange = e => {
            const rowSeriesNumberColWidth = this.seriesNumberComponent.rowSeriesNumberWidth, colSeriesNumberRowHeight = this.seriesNumberComponent.colSeriesNumberHeight;
            this.table.setContentInsetXY(rowSeriesNumberColWidth, colSeriesNumberRowHeight);
        }, this.handleResizeColWidthStart = e => {
            this.seriesNumberComponent.setAttribute("hover", !1);
            const {colIndex: colIndex, event: event} = e.detail;
            this.table.stateManager.updateInteractionState(vtable_1.TYPES.InteractionState.grabing), 
            this.table.stateManager.startResizeCol(colIndex, event.viewport.x, event.viewport.y);
        }, this.handleResizeRowHeightStart = e => {
            this.seriesNumberComponent.setAttribute("hover", !1);
            const {rowIndex: rowIndex, event: event} = e.detail;
            this.table.stateManager.updateInteractionState(vtable_1.TYPES.InteractionState.grabing), 
            this.table.stateManager.startResizeRow(rowIndex, event.viewport.x, event.viewport.y);
        }, this.handleDragColumOrderStart = e => {
            const {colIndex: colIndex, event: event} = e.detail;
            this.table.stateManager.updateInteractionState(vtable_1.TYPES.InteractionState.grabing), 
            this.table.stateManager.startMoveCol(colIndex, 0, event.viewport.x, event.viewport.y, event.nativeEvent, "column");
        }, this.handleDragRowOrderStart = e => {
            const {rowIndex: rowIndex, event: event} = e.detail;
            this.table.stateManager.updateInteractionState(vtable_1.TYPES.InteractionState.grabing), 
            this.table.stateManager.startMoveCol(0, rowIndex, event.viewport.x, event.viewport.y, event.nativeEvent, "row");
        }, this.pluginOptions = pluginOptions, this.seriesNumberComponent = new vrender_1.TableSeriesNumber({
            rowCount: pluginOptions.rowCount,
            colCount: pluginOptions.colCount,
            rowSeriesNumberWidth: pluginOptions.rowSeriesNumberWidth,
            colSeriesNumberHeight: pluginOptions.colSeriesNumberHeight,
            rowSeriesNumberGenerate: pluginOptions.rowSeriesNumberGenerate,
            colSeriesNumberGenerate: pluginOptions.colSeriesNumberGenerate,
            rowSeriesNumberCellStyle: Object.assign({
                text: {
                    fontSize: 14,
                    fill: "#7A7A7A",
                    pickable: !1,
                    textAlign: "left",
                    textBaseline: "middle",
                    padding: [ 2, 4, 2, 4 ]
                },
                borderLine: {
                    stroke: "#D9D9D9",
                    lineWidth: 1,
                    pickable: !1
                },
                bgColor: "#F9F9F9",
                states: {
                    hover: {
                        fill: "#c8daf6",
                        opacity: .7
                    },
                    select: {
                        fill: "#c8daf6",
                        opacity: 1
                    }
                }
            }, pluginOptions.rowSeriesNumberCellStyle),
            colSeriesNumberCellStyle: Object.assign({
                text: {
                    fontSize: 14,
                    fill: "#7A7A7A",
                    pickable: !1,
                    textAlign: "left",
                    textBaseline: "middle",
                    padding: [ 2, 4, 2, 4 ]
                },
                borderLine: {
                    stroke: "#D9D9D9",
                    lineWidth: 1,
                    pickable: !1
                },
                bgColor: "#F9F9F9",
                states: {
                    hover: {
                        fill: "#c8daf6",
                        opacity: .7
                    },
                    select: {
                        fill: "#c8daf6",
                        opacity: 1
                    }
                }
            }, pluginOptions.colSeriesNumberCellStyle),
            cornerCellStyle: Object.assign({
                borderLine: {
                    stroke: "#D9D9D9",
                    lineWidth: 1,
                    pickable: !1
                },
                bgColor: "#F9F9F9",
                states: {
                    hover: {
                        fill: "#98C8A5",
                        opacity: .7
                    },
                    select: {
                        fill: "#98C8A5",
                        opacity: 1
                    }
                }
            }, pluginOptions.cornerCellStyle),
            checkMoveColumnOrder: colIndex => {
                var _a;
                return !!(null === (_a = this.pluginOptions.dragOrder) || void 0 === _a ? void 0 : _a.enableDragColumnOrder) && this.table.isColumnSelected(colIndex);
            },
            checkMoveRowOrder: rowIndex => {
                var _a;
                return !!(null === (_a = this.pluginOptions.dragOrder) || void 0 === _a ? void 0 : _a.enableDragRowOrder) && this.table.isRowSelected(rowIndex);
            }
        }), this.listenComponentEvents();
    }
    run(...args) {
        var _a;
        const runTime = args[1];
        if (runTime === vtable_1.TABLE_EVENT_TYPE.BEFORE_INIT || runTime === vtable_1.TABLE_EVENT_TYPE.BEFORE_UPDATE_OPTION) {
            const eventArgs = args[0], table = args[2];
            this.table = table;
            const options = eventArgs.options, records = null !== (_a = options.records) && void 0 !== _a ? _a : [];
            records.length < this.pluginOptions.rowCount && (records.length = this.pluginOptions.rowCount), 
            options.records = records, options.columns || (options.columns = []);
            for (let i = options.columns.length; i < this.pluginOptions.colCount; i++) {
                const columnFields = {
                    field: i,
                    key: i,
                    title: ""
                };
                options.columns.push(columnFields);
            }
            const rowSeriesNumberColWidth = this.seriesNumberComponent.rowSeriesNumberWidth, colSeriesNumberRowHeight = this.seriesNumberComponent.colSeriesNumberHeight;
            options.contentOffsetX = rowSeriesNumberColWidth, options.contentOffsetY = colSeriesNumberRowHeight;
        } else if (runTime === vtable_1.TABLE_EVENT_TYPE.INITIALIZED || runTime === vtable_1.TABLE_EVENT_TYPE.UPDATED) {
            this.table = args[2], this.table.options.customConfig || (this.table.options.customConfig = {}), 
            this.table.options.customConfig.cancelSelectCellHook = e => !e.target.isDescendantsOf(this.seriesNumberComponent), 
            this.seriesNumberComponent.setAttributes({
                rowCount: this.table.rowCount,
                colCount: this.table.colCount,
                frozenRowCount: this.table.frozenRowCount,
                frozenColCount: this.table.frozenColCount,
                rightFrozenColCount: this.table.rightFrozenColCount,
                bottomFrozenRowCount: this.table.bottomFrozenRowCount
            });
            const newLayer = this.table.scenegraph.stage.createLayer();
            newLayer.appendChild(this.seriesNumberComponent), newLayer.setAttributes({
                pickable: !1
            }), this.componentLayoutLayer = newLayer, this.syncRowHeightToComponent(), this.syncColWidthToComponent(), 
            this.listenTableEvents();
        }
    }
    listenTableEvents() {
        this.table.on(vtable_1.TABLE_EVENT_TYPE.SCROLL, this.handleScroll), this.table.on(vtable_1.TABLE_EVENT_TYPE.AFTER_SORT, this.handleAfterSort), 
        this.table.on(vtable_1.TABLE_EVENT_TYPE.SELECTED_CHANGED, this.handleSelectedChanged), 
        this.table.on(vtable_1.TABLE_EVENT_TYPE.RESIZE_COLUMN_END, this.handleResizeColumnEnd), 
        this.table.on(vtable_1.TABLE_EVENT_TYPE.RESIZE_ROW_END, this.handleResizeRowEnd), 
        this.table.on(vtable_1.TABLE_EVENT_TYPE.CHANGE_HEADER_POSITION, this.handleChangeHeaderPosition);
    }
    listenComponentEvents() {
        var _a, _b;
        this.seriesNumberComponent.on(vrender_1.SeriesNumberEvent.seriesNumberCellRightClick, this.handleSeriesNumberCellRightClick), 
        this.seriesNumberComponent.on(vrender_1.SeriesNumberEvent.seriesNumberCellHover, this.handleSeriesNumberCellHover), 
        this.seriesNumberComponent.on(vrender_1.SeriesNumberEvent.seriesNumberCellUnHover, this.handleSeriesNumberCellUnHover), 
        this.seriesNumberComponent.on(vrender_1.SeriesNumberEvent.seriesNumberCellClick, this.handleSeriesNumberCellClick), 
        this.seriesNumberComponent.on(vrender_1.SeriesNumberEvent.seriesNumberCellClickUp, this.handleSeriesNumberCellClickUp), 
        this.seriesNumberComponent.on(vrender_1.SeriesNumberEvent.rowSeriesNumberWidthChange, this.handleRowSeriesNumberWidthChange), 
        this.seriesNumberComponent.on(vrender_1.SeriesNumberEvent.resizeColWidthStart, this.handleResizeColWidthStart), 
        this.seriesNumberComponent.on(vrender_1.SeriesNumberEvent.resizeRowHeightStart, this.handleResizeRowHeightStart), 
        (null === (_a = this.pluginOptions.dragOrder) || void 0 === _a ? void 0 : _a.enableDragColumnOrder) && this.seriesNumberComponent.on(vrender_1.SeriesNumberEvent.dragColumnOrderStart, this.handleDragColumOrderStart), 
        (null === (_b = this.pluginOptions.dragOrder) || void 0 === _b ? void 0 : _b.enableDragRowOrder) && this.seriesNumberComponent.on(vrender_1.SeriesNumberEvent.dragRowOrderStart, this.handleDragRowOrderStart);
    }
    release() {
        var _a, _b;
        this.componentLayoutLayer && (this.table.scenegraph.stage.removeLayer(this.componentLayoutLayer._uid), 
        this.componentLayoutLayer = null), this.scrollTimer && (clearTimeout(this.scrollTimer), 
        this.scrollTimer = null), this.table && (this.table.off(vtable_1.TABLE_EVENT_TYPE.SCROLL, this.handleScroll), 
        this.table.off(vtable_1.TABLE_EVENT_TYPE.AFTER_SORT, this.handleAfterSort), this.table.off(vtable_1.TABLE_EVENT_TYPE.SELECTED_CHANGED, this.handleSelectedChanged), 
        this.table.off(vtable_1.TABLE_EVENT_TYPE.RESIZE_COLUMN_END, this.handleResizeColumnEnd), 
        this.table.off(vtable_1.TABLE_EVENT_TYPE.RESIZE_ROW_END, this.handleResizeRowEnd)), 
        this.seriesNumberComponent && (this.seriesNumberComponent.off(vrender_1.SeriesNumberEvent.seriesNumberCellRightClick, this.handleSeriesNumberCellRightClick), 
        this.seriesNumberComponent.off(vrender_1.SeriesNumberEvent.seriesNumberCellHover, this.handleSeriesNumberCellHover), 
        this.seriesNumberComponent.off(vrender_1.SeriesNumberEvent.seriesNumberCellUnHover, this.handleSeriesNumberCellUnHover), 
        this.seriesNumberComponent.off(vrender_1.SeriesNumberEvent.seriesNumberCellClick, this.handleSeriesNumberCellClick), 
        this.seriesNumberComponent.off(vrender_1.SeriesNumberEvent.seriesNumberCellClickUp, this.handleSeriesNumberCellClickUp), 
        this.seriesNumberComponent.off(vrender_1.SeriesNumberEvent.rowSeriesNumberWidthChange, this.handleRowSeriesNumberWidthChange), 
        this.seriesNumberComponent.off(vrender_1.SeriesNumberEvent.resizeColWidthStart, this.handleResizeColWidthStart), 
        this.seriesNumberComponent.off(vrender_1.SeriesNumberEvent.resizeRowHeightStart, this.handleResizeRowHeightStart), 
        null === (_b = (_a = this.seriesNumberComponent).release) || void 0 === _b || _b.call(_a), 
        this.seriesNumberComponent = null);
    }
    syncRowHeightToComponent() {
        const {rowStart: rowStart, rowEnd: rowEnd} = this.table.getBodyVisibleRowRange(), adjustStartRowIndex = Math.max(rowStart - 2, this.table.frozenRowCount), adjustEndRowIndex = Math.min(rowEnd + 2, this.table.rowCount - 1);
        this.table.frozenRowCount !== this.seriesNumberComponent.getAttributes().frozenRowCount && this.seriesNumberComponent.setAttributes({
            frozenRowCount: this.table.frozenRowCount
        }), this.seriesNumberComponent.recreateCellsToRowSeriesNumberGroup(adjustStartRowIndex, adjustEndRowIndex);
        for (let i = 0; i < this.table.frozenRowCount; i++) {
            const cell_attrs = this.table.scenegraph.getCell(0, i).getAttributes();
            this.seriesNumberComponent.setRowSeriesNumberCellAttributes(i, {
                y: cell_attrs.y,
                height: cell_attrs.height
            });
        }
        for (let i = adjustStartRowIndex; i <= adjustEndRowIndex; i++) {
            const cell_attrs = this.table.scenegraph.getCell(0, i).getAttributes();
            this.seriesNumberComponent.setRowSeriesNumberCellAttributes(i, {
                y: cell_attrs.y,
                height: cell_attrs.height
            });
        }
    }
    syncColWidthToComponent() {
        const {colStart: colStart, colEnd: colEnd} = this.table.getBodyVisibleColRange(), adjustStartColIndex = colStart, adjustEndColIndex = Math.min(colEnd, this.table.scenegraph.proxy.colEnd);
        this.table.frozenColCount !== this.seriesNumberComponent.getAttributes().frozenColCount && this.seriesNumberComponent.setAttributes({
            frozenColCount: this.table.frozenColCount
        }), this.seriesNumberComponent.recreateCellsToColSeriesNumberGroup(adjustStartColIndex, adjustEndColIndex);
        for (let i = 0; i < this.table.frozenColCount; i++) {
            const cell_attrs = this.table.scenegraph.getColGroup(i).getAttributes();
            this.seriesNumberComponent.setColSeriesNumberCellAttributes(i, {
                x: cell_attrs.x,
                width: cell_attrs.width
            });
        }
        for (let i = adjustStartColIndex; i <= adjustEndColIndex; i++) {
            const col_attrs = this.table.scenegraph.getColGroup(i).getAttributes();
            this.seriesNumberComponent.setColSeriesNumberCellAttributes(i, {
                x: col_attrs.x,
                width: col_attrs.width
            });
        }
    }
    syncScrollToComponent() {
        this.syncRowHeightToComponent(), this.syncColWidthToComponent();
        const y = this.table.scenegraph.bodyGroup.getAttributes().y, x = this.table.scenegraph.bodyGroup.getAttributes().x;
        this.seriesNumberComponent.setRowSeriesNumberGroupAttributes({
            y: y + this.seriesNumberComponent.colSeriesNumberHeight
        }), this.seriesNumberComponent.setColSeriesNumberGroupAttributes({
            x: x + this.seriesNumberComponent.rowSeriesNumberWidth
        });
    }
}

exports.TableSeriesNumber = TableSeriesNumber;
//# sourceMappingURL=table-series-number.js.map