"use strict";

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

const vrender_1 = require("@visactor/vtable/es/vrender"), cell_range_1 = require("@visactor/vtable/es/tools/cell-range"), helper_1 = require("@visactor/vtable/es/tools/helper"), vtable_1 = require("@visactor/vtable");

class FocusHighlightPlugin {
    constructor(options = {
        fill: "#000",
        opacity: .5,
        highlightRange: void 0
    }) {
        var _a;
        this.id = "focus-highlight", this.name = "Focus Highlight", this.runTime = [ vtable_1.TABLE_EVENT_TYPE.INITIALIZED, vtable_1.TABLE_EVENT_TYPE.SELECTED_CELL, vtable_1.TABLE_EVENT_TYPE.SELECTED_CLEAR ], 
        this.id = null !== (_a = options.id) && void 0 !== _a ? _a : this.id, this.pluginOptions = Object.assign({
            fill: "#000",
            opacity: .5
        }, options);
    }
    run(...args) {
        if (this.table || (this.table = args[2]), args[1] === vtable_1.TABLE_EVENT_TYPE.INITIALIZED) this.pluginOptions.highlightRange && this.setFocusHighlightRange(this.pluginOptions.highlightRange); else if (args[1] === vtable_1.TABLE_EVENT_TYPE.SELECTED_CELL) {
            const posCell = this.table.stateManager.select.cellPos;
            if (this.table.isHeader(posCell.col, posCell.row)) this.setFocusHighlightRange(void 0); else {
                const ranges = this.table.stateManager.select.ranges, min_col = 0, max_col = this.table.colCount - 1, min_row = Math.min(ranges[0].start.row, ranges[0].end.row), max_row = Math.max(ranges[0].start.row, ranges[0].end.row);
                this.setFocusHighlightRange({
                    start: {
                        col: min_col,
                        row: min_row
                    },
                    end: {
                        col: max_col,
                        row: max_row
                    }
                });
            }
        } else args[1] === vtable_1.TABLE_EVENT_TYPE.SELECTED_CLEAR && this.setFocusHighlightRange(void 0);
    }
    setFocusHighlightRange(range, forceUpdate = !1) {
        let cellRange;
        range && "start" in range && "end" in range ? cellRange = range : range && (cellRange = {
            start: range,
            end: range
        }), (0, cell_range_1.isSameRange)(this.range, cellRange) && !forceUpdate || (this.range = cellRange, 
        range ? this.updateCellGroupShadow() : this.deleteAllCellGroupShadow(), this.table.scenegraph.updateNextFrame());
    }
    deleteAllCellGroupShadow() {
        this.table.isPivotTable() || (this.updateCellGroupShadowInContainer(this.table.scenegraph.rowHeaderGroup), 
        this.updateCellGroupShadowInContainer(this.table.scenegraph.leftBottomCornerGroup)), 
        this.updateCellGroupShadowInContainer(this.table.scenegraph.bodyGroup), this.updateCellGroupShadowInContainer(this.table.scenegraph.rightFrozenGroup), 
        this.updateCellGroupShadowInContainer(this.table.scenegraph.bottomFrozenGroup), 
        this.updateCellGroupShadowInContainer(this.table.scenegraph.rightBottomCornerGroup);
    }
    updateCellGroupShadow() {
        this.table.isPivotTable() || (this.updateCellGroupShadowInContainer(this.table.scenegraph.rowHeaderGroup, this.range), 
        this.updateCellGroupShadowInContainer(this.table.scenegraph.leftBottomCornerGroup, this.range)), 
        this.updateCellGroupShadowInContainer(this.table.scenegraph.bodyGroup, this.range), 
        this.updateCellGroupShadowInContainer(this.table.scenegraph.rightFrozenGroup, this.range), 
        this.updateCellGroupShadowInContainer(this.table.scenegraph.bottomFrozenGroup), 
        this.range, this.updateCellGroupShadowInContainer(this.table.scenegraph.rightBottomCornerGroup, this.range);
    }
    updateCellGroupShadowInContainer(container, range) {
        let cellRange;
        range && "start" in range && "end" in range ? cellRange = range : range && (cellRange = {
            start: range,
            end: range
        }), container.forEachChildrenSkipChild((item => {
            const column = item;
            "column" === column.role && column.forEachChildrenSkipChild((item => {
                const cell = item;
                if ("cell" !== cell.role) return;
                cell.attachShadow(cell.shadowRoot);
                const shadowGroup = cell.shadowRoot;
                if (cellRange) {
                    if ((0, helper_1.cellInRange)(cellRange, cell.col, cell.row)) shadowGroup.removeAllChild(); else if (!shadowGroup.firstChild) {
                        const shadowRect = (0, vrender_1.createRect)({
                            x: 0,
                            y: 0,
                            width: cell.attribute.width,
                            height: cell.attribute.height,
                            fill: this.pluginOptions.fill,
                            opacity: this.pluginOptions.opacity
                        });
                        shadowRect.name = "shadow-rect", shadowGroup.appendChild(shadowRect);
                    }
                } else shadowGroup.removeAllChild();
            }));
        }));
    }
    update() {
        this.table && this.setFocusHighlightRange(this.range, !0);
    }
}

exports.FocusHighlightPlugin = FocusHighlightPlugin;
//# sourceMappingURL=focus-highlight.js.map