"use strict";

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

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

class HighlightHeaderWhenSelectCellPlugin {
    constructor(pluginOptions) {
        var _a;
        this.id = "highlight-header-when-select-cell", this.name = "Highlight Header When Select Cell", 
        this.runTime = [ vtable_1.TABLE_EVENT_TYPE.INITIALIZED, vtable_1.TABLE_EVENT_TYPE.SELECTED_CLEAR, vtable_1.TABLE_EVENT_TYPE.SELECTED_CELL, vtable_1.TABLE_EVENT_TYPE.MOUSEMOVE_TABLE ], 
        this.colHeaderRanges = [], this.rowHeaderRanges = [], this.id = null !== (_a = null == pluginOptions ? void 0 : pluginOptions.id) && void 0 !== _a ? _a : this.id, 
        this.pluginOptions = pluginOptions;
    }
    run(...args) {
        const runTime = args[1], table = args[2];
        this.table = table, runTime === vtable_1.TABLE_EVENT_TYPE.SELECTED_CLEAR ? this.clearHighlight() : runTime === vtable_1.TABLE_EVENT_TYPE.SELECTED_CELL || runTime === vtable_1.TABLE_EVENT_TYPE.MOUSEMOVE_TABLE ? this.updateHighlight() : runTime === vtable_1.TABLE_EVENT_TYPE.INITIALIZED && this.registerStyle();
    }
    registerStyle() {
        var _a, _b, _c, _d, _e, _f, _g, _h;
        this.table.registerCustomCellStyle("col-highlight", {
            bgColor: null !== (_b = null === (_a = this.pluginOptions) || void 0 === _a ? void 0 : _a.colHighlightBGColor) && void 0 !== _b ? _b : "#82b2f5",
            color: null !== (_d = null === (_c = this.pluginOptions) || void 0 === _c ? void 0 : _c.colHighlightColor) && void 0 !== _d ? _d : "#FFF"
        }), this.table.registerCustomCellStyle("row-highlight", {
            bgColor: null !== (_f = null === (_e = this.pluginOptions) || void 0 === _e ? void 0 : _e.rowHighlightBGColor) && void 0 !== _f ? _f : "#82b2f5",
            color: null !== (_h = null === (_g = this.pluginOptions) || void 0 === _g ? void 0 : _g.rowHighlightColor) && void 0 !== _h ? _h : "yellow"
        });
    }
    clearHighlight() {
        this.colHeaderRanges && this.colHeaderRanges.forEach((range => {
            this.table.arrangeCustomCellStyle({
                range: range
            }, void 0);
        })), this.rowHeaderRanges && this.rowHeaderRanges.forEach((range => {
            this.table.arrangeCustomCellStyle({
                range: range
            }, void 0);
        })), this.colHeaderRanges = [], this.rowHeaderRanges = [];
    }
    updateHighlight() {
        var _a, _b, _c, _d;
        if (!1 === (null === (_a = this.pluginOptions) || void 0 === _a ? void 0 : _a.colHighlight) && !1 === (null === (_b = this.pluginOptions) || void 0 === _b ? void 0 : _b.rowHighlight)) return;
        const selectRanges = this.table.getSelectedCellRanges();
        selectRanges.length < 2 && this.clearHighlight();
        for (let i = 0; i < selectRanges.length; i++) {
            const selectRange = selectRanges[i], rowSelectRange = [ selectRange.start.row, selectRange.end.row ];
            rowSelectRange.sort(((a, b) => a - b));
            const colSelectRange = [ selectRange.start.col, selectRange.end.col ];
            let colHeaderRange, rowHeaderRange;
            colSelectRange.sort(((a, b) => a - b)), this.table.isPivotTable() ? (colHeaderRange = {
                start: {
                    col: colSelectRange[0],
                    row: 0
                },
                end: {
                    col: colSelectRange[1],
                    row: this.table.columnHeaderLevelCount - 1
                }
            }, rowHeaderRange = {
                start: {
                    col: 0,
                    row: rowSelectRange[0]
                },
                end: {
                    col: this.table.rowHeaderLevelCount - 1,
                    row: rowSelectRange[1]
                }
            }) : this.table.internalProps.transpose ? rowHeaderRange = {
                start: {
                    col: 0,
                    row: rowSelectRange[0]
                },
                end: {
                    col: this.table.rowHeaderLevelCount - 1,
                    row: rowSelectRange[1]
                }
            } : (colHeaderRange = {
                start: {
                    col: colSelectRange[0],
                    row: 0
                },
                end: {
                    col: colSelectRange[1],
                    row: this.table.columnHeaderLevelCount - 1
                }
            }, this.table.internalProps.rowSeriesNumber && (rowHeaderRange = {
                start: {
                    col: 0,
                    row: rowSelectRange[0]
                },
                end: {
                    col: 0,
                    row: rowSelectRange[1]
                }
            })), !1 === (null === (_c = this.pluginOptions) || void 0 === _c ? void 0 : _c.colHighlight) || this.colHeaderRanges.find((range => isSameRange(range, colHeaderRange))) || (colHeaderRange && this.table.arrangeCustomCellStyle({
                range: colHeaderRange
            }, "col-highlight"), this.colHeaderRanges.push(colHeaderRange)), !1 === (null === (_d = this.pluginOptions) || void 0 === _d ? void 0 : _d.rowHighlight) || this.rowHeaderRanges.find((range => isSameRange(range, rowHeaderRange))) || (rowHeaderRange && this.table.arrangeCustomCellStyle({
                range: rowHeaderRange
            }, "row-highlight"), this.rowHeaderRanges.push(rowHeaderRange));
        }
    }
    update() {
        this.registerStyle(), this.updateHighlight();
    }
    release() {
        this.rowHeaderRanges = [], this.colHeaderRanges = [];
    }
}

function isSameRange(a, b) {
    return void 0 === a && void 0 === b || void 0 !== a && void 0 !== b && (a.start.col === b.start.col && a.start.row === b.start.row && a.end.col === b.end.col && a.end.row === b.end.row);
}

//# sourceMappingURL=highlight-header-when-select-cell.js.map
exports.HighlightHeaderWhenSelectCellPlugin = HighlightHeaderWhenSelectCellPlugin;