"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.GridGroup = void 0;
const tslib_1 = require("tslib");
const g_1 = require("@antv/g");
const lodash_1 = require("lodash");
const constant_1 = require("../common/constant");
const g_renders_1 = require("../utils/g-renders");
class GridGroup extends g_1.Group {
    constructor(cfg) {
        const { name, s2 } = cfg, rest = tslib_1.__rest(cfg, ["name", "s2"]);
        super({
            name,
            style: rest,
        });
        this.gridInfo = {
            cols: [],
            rows: [],
        };
        this.updateGrid = (gridInfo, id = constant_1.KEY_GROUP_GRID_GROUP) => {
            var _a, _b, _c, _d;
            if (!this.gridGroup || !this.getElementById(id)) {
                this.gridGroup = this.appendChild(new g_1.Group({
                    id,
                    style: {
                        zIndex: constant_1.PANEL_GRID_GROUP_Z_INDEX,
                    },
                }));
            }
            const width = (_a = (0, lodash_1.last)(gridInfo.cols)) !== null && _a !== void 0 ? _a : 0;
            const height = (_b = (0, lodash_1.last)(gridInfo.rows)) !== null && _b !== void 0 ? _b : 0;
            const { theme } = this.s2;
            const style = theme.dataCell.cell;
            const verticalBorderWidth = (_c = style === null || style === void 0 ? void 0 : style.verticalBorderWidth) !== null && _c !== void 0 ? _c : 1;
            const halfVerticalBorderWidth = verticalBorderWidth / 2;
            const verticalBorderStyle = {
                stroke: style.verticalBorderColor,
                strokeOpacity: style.verticalBorderColorOpacity,
                lineWidth: verticalBorderWidth,
            };
            const horizontalBorderWidth = (_d = style === null || style === void 0 ? void 0 : style.horizontalBorderWidth) !== null && _d !== void 0 ? _d : 1;
            const halfHorizontalBorderWidth = horizontalBorderWidth / 2;
            const horizontalBorderStyle = {
                stroke: style.horizontalBorderColor,
                strokeOpacity: style.horizontalBorderColorOpacity,
                lineWidth: horizontalBorderWidth,
            };
            const children = this.gridGroup.children;
            let childIndex = 0;
            // 复用或新增垂直线
            gridInfo.cols.forEach((x) => {
                const attrs = Object.assign({ x1: x - halfVerticalBorderWidth, x2: x - halfVerticalBorderWidth, y1: 0, y2: height }, verticalBorderStyle);
                if (childIndex < children.length) {
                    // 复用已有的 Line 对象
                    (0, g_renders_1.batchSetStyle)(children[childIndex], attrs);
                }
                else {
                    // 创建新的 Line 对象
                    (0, g_renders_1.renderLine)(this.gridGroup, attrs);
                }
                childIndex++;
            });
            // 复用或新增水平线
            gridInfo.rows.forEach((y) => {
                const attrs = Object.assign({ x1: 0, x2: width, y1: y - halfHorizontalBorderWidth, y2: y - halfHorizontalBorderWidth }, horizontalBorderStyle);
                if (childIndex < children.length) {
                    // 复用已有的 Line 对象
                    (0, g_renders_1.batchSetStyle)(children[childIndex], attrs);
                }
                else {
                    // 创建新的 Line 对象
                    (0, g_renders_1.renderLine)(this.gridGroup, attrs);
                }
                childIndex++;
            });
            // 移除多余的 Line 对象
            const requiredCount = childIndex;
            const currentCount = children.length;
            if (requiredCount < currentCount) {
                // 从后往前删除，避免在循环中改变数组长度导致索引错乱
                for (let i = currentCount - 1; i >= requiredCount; i--) {
                    children[i].destroy();
                }
            }
            // 更新 gridInfo 供下次使用
            this.gridInfo = gridInfo;
        };
        this.s2 = s2;
    }
}
exports.GridGroup = GridGroup;
//# sourceMappingURL=grid-group.js.map