import { isValid } from "@visactor/vutils";

export const chartInstanceListColumnByColumnDirection = {};

export const chartInstanceListRowByRowDirection = {};

export function generateChartInstanceListByColumnDirection(col, dimensionValueOrXValue, positionValueOrYValue, canvasXY, table, hideTooltip = !1, isScatter = !1) {
    var _a;
    isValid(chartInstanceListColumnByColumnDirection[col]) || (chartInstanceListColumnByColumnDirection[col] = {});
    const {rowStart: rowStart} = table.getBodyVisibleRowRange();
    let rowEnd = table.getBodyVisibleRowRange().rowEnd;
    rowEnd = Math.min(table.rowCount - 1 - table.bottomFrozenRowCount, rowEnd);
    for (let i = rowStart; i <= rowEnd; i++) {
        const cellGroup = table.scenegraph.getCell(col, i), chartNode = null === (_a = null == cellGroup ? void 0 : cellGroup.getChildren()) || void 0 === _a ? void 0 : _a[0];
        chartNode.addUpdateShapeAndBoundsTag(), chartInstanceListColumnByColumnDirection[col][i] || isValid(chartNode) && (chartNode.activeChartInstance || chartNode.activate(table), 
        chartInstanceListColumnByColumnDirection[col][i] = chartNode.activeChartInstance), 
        table.stateManager.hover.cellPos.col === col && table.stateManager.hover.cellPos.row === i || setTimeout((() => {
            var _a, _b, _c, _d;
            if (null === (_a = chartInstanceListColumnByColumnDirection[col]) || void 0 === _a ? void 0 : _a[i]) {
                const absolutePosition = table.getCellRelativeRect(col, i), chartDimensionLinkage = table.options.chartDimensionLinkage;
                let isShowTooltip = !isScatter;
                if (!isScatter && "object" == typeof chartDimensionLinkage && (isShowTooltip = null === (_b = chartDimensionLinkage.showTooltip) || void 0 === _b || _b, 
                i === rowEnd && isShowTooltip)) {
                    const heightLimitToShowTooltipForLastRow = chartDimensionLinkage.heightLimitToShowTooltipForLastRow, {rowEnd: rowEnd1} = table.getBodyVisibleRowRange(-heightLimitToShowTooltipForLastRow);
                    if (rowEnd1 === rowEnd) isShowTooltip = !0; else {
                        const {rowEnd: rowEnd2} = table.getBodyVisibleRowRange(5);
                        isShowTooltip = rowEnd2 !== rowEnd;
                    }
                }
                isScatter ? null === (_d = (_c = chartInstanceListColumnByColumnDirection[col][i]).showCrosshair) || void 0 === _d || _d.call(_c, (axis => "left" === axis.layoutOrient ? positionValueOrYValue : dimensionValueOrXValue)) : hideTooltip ? (chartInstanceListColumnByColumnDirection[col][i].hideTooltip(), 
                chartInstanceListColumnByColumnDirection[col][i].setDimensionIndex(dimensionValueOrXValue, {
                    tooltip: !1,
                    showTooltipOption: {
                        x: canvasXY.x,
                        y: absolutePosition.top + 3,
                        activeType: "dimension"
                    }
                })) : chartInstanceListColumnByColumnDirection[col][i].setDimensionIndex(dimensionValueOrXValue, {
                    tooltip: isShowTooltip,
                    showTooltipOption: {
                        x: canvasXY.x,
                        y: absolutePosition.top + 3,
                        activeType: "dimension"
                    }
                });
            }
        }), 0), table.scenegraph.updateNextFrame();
    }
}

export function clearChartInstanceListByColumnDirection(col, excludedRow, table) {
    var _a;
    if (isValid(chartInstanceListColumnByColumnDirection[col])) {
        for (const i in chartInstanceListColumnByColumnDirection[col]) {
            if (isValid(excludedRow) && Number(i) === excludedRow) continue;
            const cellGroup = table.scenegraph.getCell(col, Number(i)), chartNode = null === (_a = null == cellGroup ? void 0 : cellGroup.getChildren()) || void 0 === _a ? void 0 : _a[0];
            chartNode.addUpdateShapeAndBoundsTag(), isValid(chartNode) && (chartNode.deactivate(table, {
                releaseChartInstance: !0,
                releaseColumnChartInstance: !1,
                releaseRowChartInstance: !1
            }), chartInstanceListColumnByColumnDirection[col][i] = null);
        }
        delete chartInstanceListColumnByColumnDirection[col];
    }
}

export function generateChartInstanceListByRowDirection(row, dimensionValueOrXValue, positionValueOrYValue, canvasXY, table, hideTooltip = !1, isScatter = !1) {
    var _a;
    isValid(chartInstanceListRowByRowDirection[row]) || (chartInstanceListRowByRowDirection[row] = {});
    const {colStart: colStart} = table.getBodyVisibleColRange();
    let colEnd = table.getBodyVisibleColRange().colEnd;
    colEnd = Math.min(table.colCount - 1 - table.rightFrozenColCount, colEnd);
    for (let i = colStart; i <= colEnd; i++) {
        const cellGroup = table.scenegraph.getCell(i, row), chartNode = null === (_a = null == cellGroup ? void 0 : cellGroup.getChildren()) || void 0 === _a ? void 0 : _a[0];
        chartNode.addUpdateShapeAndBoundsTag(), chartInstanceListRowByRowDirection[row][i] || isValid(chartNode) && (chartNode.activeChartInstance || chartNode.activate(table), 
        chartInstanceListRowByRowDirection[row][i] = chartNode.activeChartInstance), table.stateManager.hover.cellPos.col === i && table.stateManager.hover.cellPos.row === row || setTimeout((() => {
            var _a, _b, _c, _d;
            if (null === (_a = chartInstanceListRowByRowDirection[row]) || void 0 === _a ? void 0 : _a[i]) {
                const absolutePosition = table.getCellRelativeRect(i, row), chartDimensionLinkage = table.options.chartDimensionLinkage;
                let isShowTooltip = !isScatter;
                if (!isScatter && "object" == typeof chartDimensionLinkage && (isShowTooltip = null === (_b = chartDimensionLinkage.showTooltip) || void 0 === _b || _b, 
                i === colEnd && isShowTooltip)) {
                    const widthLimitToShowTooltipForLastColumn = chartDimensionLinkage.widthLimitToShowTooltipForLastColumn, {colEnd: colEnd1} = table.getBodyVisibleColRange(-widthLimitToShowTooltipForLastColumn);
                    if (colEnd1 === colEnd) isShowTooltip = !0; else {
                        const {colEnd: colEnd2} = table.getBodyVisibleColRange(5);
                        isShowTooltip = colEnd2 !== colEnd;
                    }
                }
                isScatter ? null === (_d = (_c = chartInstanceListRowByRowDirection[row][i]).showCrosshair) || void 0 === _d || _d.call(_c, (axis => "left" === axis.layoutOrient ? positionValueOrYValue : dimensionValueOrXValue)) : hideTooltip ? (chartInstanceListRowByRowDirection[row][i].hideTooltip(), 
                chartInstanceListRowByRowDirection[row][i].setDimensionIndex(dimensionValueOrXValue, {
                    tooltip: !1,
                    showTooltipOption: {
                        x: absolutePosition.left + 3,
                        y: canvasXY.y,
                        activeType: "dimension"
                    }
                })) : chartInstanceListRowByRowDirection[row][i].setDimensionIndex(dimensionValueOrXValue, {
                    tooltip: isShowTooltip,
                    showTooltipOption: {
                        x: absolutePosition.left + 3,
                        y: canvasXY.y,
                        activeType: "dimension"
                    }
                });
            }
        }), 0), table.scenegraph.updateNextFrame();
    }
}

export function clearChartInstanceListByRowDirection(row, excludedCol, table) {
    var _a;
    if (isValid(chartInstanceListRowByRowDirection[row])) for (const i in chartInstanceListRowByRowDirection[row]) {
        if (isValid(excludedCol) && Number(i) === excludedCol) continue;
        const cellGroup = table.scenegraph.getCell(Number(i), row), chartNode = null === (_a = null == cellGroup ? void 0 : cellGroup.getChildren()) || void 0 === _a ? void 0 : _a[0];
        chartNode.addUpdateShapeAndBoundsTag(), isValid(chartNode) && (chartNode.deactivate(table, {
            releaseChartInstance: !0,
            releaseColumnChartInstance: !1,
            releaseRowChartInstance: !1
        }), chartInstanceListRowByRowDirection[row][i] = null);
    }
    delete chartInstanceListRowByRowDirection[row];
}
//# sourceMappingURL=active-cell-chart-list.js.map
