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

export function setBrushingChartInstance(chartInstance, col, row, scenegraph) {
    scenegraph.brushingChartInstance = chartInstance, scenegraph.brushingChartInstanceCellPos = {
        col: col,
        row: row
    };
}

export function clearBrushingChartInstance(scenegraph) {
    scenegraph.brushingChartInstance = void 0, scenegraph.brushingChartInstanceCellPos = {
        col: -1,
        row: -1
    };
}

export function clearAndReleaseBrushingChartInstance(scenegraph) {
    var _a, _b, _c;
    enableTooltipToAllChartInstances(scenegraph);
    const cellGroup = scenegraph.getCell(scenegraph.brushingChartInstanceCellPos.col, scenegraph.brushingChartInstanceCellPos.row);
    (null === (_a = null == cellGroup ? void 0 : cellGroup.firstChild) || void 0 === _a ? void 0 : _a.deactivate) && (null === (_c = null === (_b = null == cellGroup ? void 0 : cellGroup.firstChild) || void 0 === _b ? void 0 : _b.deactivate) || void 0 === _c || _c.call(_b, scenegraph.table, {
        forceRelease: !0,
        releaseChartInstance: !0,
        releaseColumnChartInstance: !1,
        releaseRowChartInstance: !1,
        releaseAllChartInstance: !1
    })), isValid(scenegraph.chartInstanceListColumnByColumnDirection[scenegraph.brushingChartInstanceCellPos.col]) && delete scenegraph.chartInstanceListColumnByColumnDirection[scenegraph.brushingChartInstanceCellPos.col][scenegraph.brushingChartInstanceCellPos.row], 
    isValid(scenegraph.chartInstanceListRowByRowDirection[scenegraph.brushingChartInstanceCellPos.row]) && delete scenegraph.chartInstanceListRowByRowDirection[scenegraph.brushingChartInstanceCellPos.row][scenegraph.brushingChartInstanceCellPos.col], 
    scenegraph.brushingChartInstance = void 0, scenegraph.brushingChartInstanceCellPos = {
        col: -1,
        row: -1
    };
}

export function getBrushingChartInstance(scenegraph) {
    return scenegraph.brushingChartInstance;
}

export function getBrushingChartInstanceCellPos(scenegraph) {
    return scenegraph.brushingChartInstanceCellPos;
}

export function generateChartInstanceListByColumnDirection(col, dimensionValueOrXValue, positionValueOrYValue, canvasXY, table, hideTooltip = !1, isScatter = !1) {
    var _a;
    const scenegraph = table.scenegraph;
    clearDelayRunDimensionHoverTimerForColumnDirection(scenegraph), isValid(scenegraph.chartInstanceListColumnByColumnDirection[col]) || (scenegraph.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];
        scenegraph.chartInstanceListColumnByColumnDirection[col][i] || isValid(chartNode) && (chartNode.addUpdateShapeAndBoundsTag(), 
        chartNode.activeChartInstance || chartNode.activate(table), scenegraph.chartInstanceListColumnByColumnDirection[col][i] = chartNode.activeChartInstance);
        const timer = setTimeout((() => {
            var _a, _b, _c, _d;
            if (null === (_a = scenegraph.chartInstanceListColumnByColumnDirection[col]) || void 0 === _a ? void 0 : _a[i]) {
                const chartDimensionLinkage = table.options.chartDimensionLinkage;
                let isShowTooltip = !isScatter;
                if (isScatter || "object" != typeof chartDimensionLinkage || (isShowTooltip = null === (_b = chartDimensionLinkage.showTooltip) || void 0 === _b || _b, 
                isShowTooltip = isShowTooltip && checkIsShowTooltipForEdgeRow(i, table)), isScatter) table.stateManager.hover.cellPos.col === col && table.stateManager.hover.cellPos.row === i || null === (_d = (_c = scenegraph.chartInstanceListColumnByColumnDirection[col][i]).showCrosshair) || void 0 === _d || _d.call(_c, (axis => "left" === axis.layoutOrient ? positionValueOrYValue : dimensionValueOrXValue)); else {
                    const cellBoundry = table.getCellRelativeRect(col, i), bodyBoundryTop = table.frozenRowCount ? table.getCellRelativeRect(col, table.frozenRowCount - 1).bottom : 0, absolutePositionTop = Math.max(bodyBoundryTop, table.getCellRelativeRect(col, i).top);
                    hideTooltip ? (table.stateManager.hover.cellPos.col === col && table.stateManager.hover.cellPos.row === i || scenegraph.chartInstanceListColumnByColumnDirection[col][i].hideTooltip(), 
                    scenegraph.chartInstanceListColumnByColumnDirection[col][i].setDimensionIndex(dimensionValueOrXValue, {
                        tooltip: !1,
                        showTooltipOption: {
                            x: canvasXY.x - cellBoundry.left,
                            y: absolutePositionTop - cellBoundry.top,
                            activeType: "dimension"
                        }
                    })) : scenegraph.chartInstanceListColumnByColumnDirection[col][i].setDimensionIndex(dimensionValueOrXValue, {
                        tooltip: isShowTooltip,
                        showTooltipOption: {
                            x: canvasXY.x - cellBoundry.left,
                            y: absolutePositionTop - cellBoundry.top,
                            activeType: "dimension"
                        }
                    });
                }
            }
        }), 0);
        scenegraph.delayRunDimensionHoverTimerForColumnDirection.push(timer), table.scenegraph.updateNextFrame();
    }
}

export function generateChartInstanceListByRowDirection(row, dimensionValueOrXValue, positionValueOrYValue, canvasXY, table, hideTooltip = !1, isScatter = !1) {
    var _a;
    const scenegraph = table.scenegraph;
    clearDelayRunDimensionHoverTimerForRowDirection(scenegraph), isValid(scenegraph.chartInstanceListRowByRowDirection[row]) || (scenegraph.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];
        scenegraph.chartInstanceListRowByRowDirection[row][i] || isValid(chartNode) && (chartNode.addUpdateShapeAndBoundsTag(), 
        chartNode.activeChartInstance || chartNode.activate(table), scenegraph.chartInstanceListRowByRowDirection[row][i] = chartNode.activeChartInstance);
        const timer = setTimeout((() => {
            var _a, _b, _c, _d;
            if (null === (_a = scenegraph.chartInstanceListRowByRowDirection[row]) || void 0 === _a ? void 0 : _a[i]) {
                const chartDimensionLinkage = table.options.chartDimensionLinkage;
                let isShowTooltip = !isScatter;
                if (isScatter || "object" != typeof chartDimensionLinkage || (isShowTooltip = null === (_b = chartDimensionLinkage.showTooltip) || void 0 === _b || _b, 
                isShowTooltip = isShowTooltip && checkIsShowTooltipForEdgeColumn(i, table)), isScatter) table.stateManager.hover.cellPos.col === i && table.stateManager.hover.cellPos.row === row || null === (_d = (_c = scenegraph.chartInstanceListRowByRowDirection[row][i]).showCrosshair) || void 0 === _d || _d.call(_c, (axis => "left" === axis.layoutOrient ? positionValueOrYValue : dimensionValueOrXValue)); else {
                    const cellBoundry = table.getCellRelativeRect(i, row), bodyBoundryLeft = table.frozenColCount ? table.getCellRelativeRect(table.frozenColCount - 1, row).right : 0, absolutePositionLeft = Math.max(bodyBoundryLeft, table.getCellRelativeRect(i, row).left);
                    hideTooltip ? (table.stateManager.hover.cellPos.col === i && table.stateManager.hover.cellPos.row === row || scenegraph.chartInstanceListRowByRowDirection[row][i].hideTooltip(), 
                    scenegraph.chartInstanceListRowByRowDirection[row][i].setDimensionIndex(dimensionValueOrXValue, {
                        tooltip: !1,
                        showTooltipOption: {
                            x: absolutePositionLeft - cellBoundry.left,
                            y: canvasXY.y - cellBoundry.top,
                            activeType: "dimension"
                        }
                    })) : scenegraph.chartInstanceListRowByRowDirection[row][i].setDimensionIndex(dimensionValueOrXValue, {
                        tooltip: isShowTooltip,
                        showTooltipOption: {
                            x: absolutePositionLeft - cellBoundry.left,
                            y: canvasXY.y - cellBoundry.top,
                            activeType: "dimension"
                        }
                    });
                }
            }
        }), 0);
        scenegraph.delayRunDimensionHoverTimerForRowDirection.push(timer), table.scenegraph.updateNextFrame();
    }
}

export function generateChartInstanceListByViewRange(datum, table, deactivate = !1) {
    var _a;
    const scenegraph = table.scenegraph;
    clearDelayRunDimensionHoverTimerForViewRange(scenegraph);
    const {rowStart: rowStart} = table.getBodyVisibleRowRange();
    let rowEnd = table.getBodyVisibleRowRange().rowEnd;
    rowEnd = Math.min(table.rowCount - 1 - table.bottomFrozenRowCount, rowEnd);
    const {colStart: colStart} = table.getBodyVisibleColRange();
    let colEnd = table.getBodyVisibleColRange().colEnd;
    colEnd = Math.min(table.colCount - 1 - table.rightFrozenColCount, colEnd);
    for (let col = colStart; col <= colEnd; col++) {
        isValid(scenegraph.chartInstanceListColumnByColumnDirection[col]) || (scenegraph.chartInstanceListColumnByColumnDirection[col] = {});
        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];
            scenegraph.chartInstanceListColumnByColumnDirection[col][i] || isValid(chartNode) && (chartNode.addUpdateShapeAndBoundsTag(), 
            chartNode.activeChartInstance ? scenegraph.chartInstanceListColumnByColumnDirection[col][i] = chartNode.activeChartInstance : "pie" === chartNode.attribute.spec.type && (chartNode.activate(table), 
            scenegraph.chartInstanceListColumnByColumnDirection[col][i] = chartNode.activeChartInstance));
            const timer = setTimeout((() => {
                var _a, _b;
                if (null === (_a = scenegraph.chartInstanceListColumnByColumnDirection[col]) || void 0 === _a ? void 0 : _a[i]) {
                    const chartDimensionLinkage = table.options.chartDimensionLinkage;
                    let isShowTooltip = !0;
                    "object" == typeof chartDimensionLinkage && (deactivate ? (scenegraph.chartInstanceListColumnByColumnDirection[col][i].setHovered(), 
                    scenegraph.chartInstanceListColumnByColumnDirection[col][i].hideTooltip()) : (isShowTooltip = null === (_b = chartDimensionLinkage.showTooltip) || void 0 === _b || _b, 
                    isShowTooltip = isShowTooltip && checkIsShowTooltipForEdgeRow(i, table), isShowTooltip = isShowTooltip && checkIsShowTooltipForEdgeColumn(col, table), 
                    scenegraph.chartInstanceListColumnByColumnDirection[col][i].setHovered(datum), isShowTooltip && scenegraph.chartInstanceListColumnByColumnDirection[col][i].showTooltip(datum, {
                        activeType: "mark"
                    })));
                }
            }), 0);
            scenegraph.delayRunDimensionHoverTimerForViewRange.push(timer), table.scenegraph.updateNextFrame();
        }
    }
}

function checkIsShowTooltipForEdgeRow(row, table) {
    var _a, _b;
    let isShowTooltip = !0;
    const {rowStart: rowStart} = table.getBodyVisibleRowRange();
    let rowEnd = table.getBodyVisibleRowRange().rowEnd;
    rowEnd = Math.min(table.rowCount - 1 - table.bottomFrozenRowCount, rowEnd);
    const chartDimensionLinkage = table.options.chartDimensionLinkage;
    if (row === rowEnd && isShowTooltip) {
        const heightLimitToShowTooltipForEdgeRow = null !== (_a = chartDimensionLinkage.heightLimitToShowTooltipForEdgeRow) && void 0 !== _a ? _a : 0, {rowEnd: rowEnd1} = table.getBodyVisibleRowRange(0, -heightLimitToShowTooltipForEdgeRow);
        if (rowEnd1 === rowEnd) isShowTooltip = !0; else {
            const {rowEnd: rowEnd2} = table.getBodyVisibleRowRange(0, 5);
            isShowTooltip = rowEnd2 !== rowEnd;
        }
    } else if (row === rowStart && isShowTooltip) {
        const heightLimitToShowTooltipForEdgeRow = null !== (_b = chartDimensionLinkage.heightLimitToShowTooltipForEdgeRow) && void 0 !== _b ? _b : 0, {rowStart: rowStart1} = table.getBodyVisibleRowRange(heightLimitToShowTooltipForEdgeRow, 0);
        if (rowStart1 === rowStart) isShowTooltip = !0; else {
            const {rowStart: rowStart2} = table.getBodyVisibleRowRange(0, -5);
            isShowTooltip = rowStart2 !== rowStart;
        }
    }
    return isShowTooltip;
}

function checkIsShowTooltipForEdgeColumn(col, table) {
    let isShowTooltip = !0;
    const {colStart: colStart} = table.getBodyVisibleColRange();
    let colEnd = table.getBodyVisibleColRange().colEnd;
    colEnd = Math.min(table.colCount - 1 - table.rightFrozenColCount, colEnd);
    const chartDimensionLinkage = table.options.chartDimensionLinkage;
    if (col === colEnd && isShowTooltip) {
        const widthLimitToShowTooltipForEdgeColumn = chartDimensionLinkage.widthLimitToShowTooltipForEdgeColumn, {colEnd: colEnd1} = table.getBodyVisibleColRange(0, -widthLimitToShowTooltipForEdgeColumn);
        if (colEnd1 === colEnd) isShowTooltip = !0; else {
            const {colEnd: colEnd2} = table.getBodyVisibleColRange(0, 5);
            isShowTooltip = colEnd2 !== colEnd;
        }
    } else if (col === colStart && isShowTooltip) {
        const widthLimitToShowTooltipForEdgeColumn = chartDimensionLinkage.widthLimitToShowTooltipForEdgeColumn, {colStart: colStart1} = table.getBodyVisibleColRange(widthLimitToShowTooltipForEdgeColumn, 0);
        if (colStart1 === colStart) isShowTooltip = !0; else {
            const {colStart: colStart2} = table.getBodyVisibleColRange(0, -5);
            isShowTooltip = colStart2 !== colStart;
        }
    }
    return isShowTooltip;
}

export function clearChartInstanceListByColumnDirection(col, excludedRow, table, forceRelease = !1) {
    var _a;
    const scenegraph = table.scenegraph;
    if (isValid(scenegraph.chartInstanceListColumnByColumnDirection[col])) {
        for (const i in scenegraph.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];
            isValid(chartNode) && (chartNode.addUpdateShapeAndBoundsTag(), chartNode.deactivate(table, {
                forceRelease: forceRelease,
                releaseChartInstance: !0,
                releaseColumnChartInstance: !1,
                releaseRowChartInstance: !1
            }), scenegraph.chartInstanceListColumnByColumnDirection[col][i] = null);
        }
        delete scenegraph.chartInstanceListColumnByColumnDirection[col];
    }
}

export function clearChartInstanceListByRowDirection(row, excludedCol, table, forceRelease = !1) {
    var _a;
    const scenegraph = table.scenegraph;
    if (isValid(scenegraph.chartInstanceListRowByRowDirection[row])) for (const i in scenegraph.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];
        isValid(chartNode) && (chartNode.addUpdateShapeAndBoundsTag(), chartNode.deactivate(table, {
            forceRelease: forceRelease,
            releaseChartInstance: !0,
            releaseColumnChartInstance: !1,
            releaseRowChartInstance: !1
        }), scenegraph.chartInstanceListRowByRowDirection[row][i] = null);
    }
    delete scenegraph.chartInstanceListRowByRowDirection[row];
}

export function clearDelayRunDimensionHoverTimerForColumnDirection(scenegraph) {
    for (const timer of scenegraph.delayRunDimensionHoverTimerForColumnDirection) clearTimeout(timer);
    scenegraph.delayRunDimensionHoverTimerForColumnDirection.length = 0;
}

export function clearDelayRunDimensionHoverTimerForRowDirection(scenegraph) {
    for (const timer of scenegraph.delayRunDimensionHoverTimerForRowDirection) clearTimeout(timer);
    scenegraph.delayRunDimensionHoverTimerForRowDirection.length = 0;
}

export function clearDelayRunDimensionHoverTimerForViewRange(scenegraph) {
    for (const timer of scenegraph.delayRunDimensionHoverTimerForViewRange) clearTimeout(timer);
    scenegraph.delayRunDimensionHoverTimerForViewRange.length = 0;
}

export function clearDelayRunDimensionHoverTimers(scenegraph) {
    for (const timer of scenegraph.delayRunDimensionHoverTimerForColumnDirection) clearTimeout(timer);
    scenegraph.delayRunDimensionHoverTimerForColumnDirection.length = 0;
    for (const timer of scenegraph.delayRunDimensionHoverTimerForRowDirection) clearTimeout(timer);
    scenegraph.delayRunDimensionHoverTimerForRowDirection.length = 0;
    for (const timer of scenegraph.delayRunDimensionHoverTimerForViewRange) clearTimeout(timer);
    scenegraph.delayRunDimensionHoverTimerForViewRange.length = 0;
}

export function clearAllChartInstanceList(table, forceRelease = !1) {
    const scenegraph = table.scenegraph;
    clearDelayRunDimensionHoverTimers(scenegraph);
    for (const col in scenegraph.chartInstanceListColumnByColumnDirection) clearChartInstanceListByColumnDirection(Number(col), void 0, table, forceRelease);
    for (const row in scenegraph.chartInstanceListRowByRowDirection) clearChartInstanceListByRowDirection(Number(row), void 0, table, forceRelease);
}

export function isDisabledTooltipToAllChartInstances(scenegraph) {
    return scenegraph.disabledTooltipToAllChartInstances;
}

export function disableTooltipToAllChartInstances(scenegraph) {
    scenegraph.disabledTooltipToAllChartInstances = !0, clearDelayRunDimensionHoverTimers(scenegraph);
    for (const col in scenegraph.chartInstanceListColumnByColumnDirection) for (const row in scenegraph.chartInstanceListColumnByColumnDirection[col]) scenegraph.chartInstanceListColumnByColumnDirection[col][row].disableTooltip(!0), 
    scenegraph.chartInstanceListColumnByColumnDirection[col][row].hideTooltip();
    for (const row in scenegraph.chartInstanceListRowByRowDirection) for (const col in scenegraph.chartInstanceListRowByRowDirection[row]) scenegraph.chartInstanceListRowByRowDirection[row][col].disableTooltip(!0), 
    scenegraph.chartInstanceListRowByRowDirection[row][col].hideTooltip();
}

export function enableTooltipToAllChartInstances(scenegraph) {
    scenegraph.disabledTooltipToAllChartInstances = !1;
    for (const col in scenegraph.chartInstanceListColumnByColumnDirection) for (const row in scenegraph.chartInstanceListColumnByColumnDirection[col]) scenegraph.chartInstanceListColumnByColumnDirection[col][row].disableTooltip(!1);
    for (const row in scenegraph.chartInstanceListRowByRowDirection) for (const col in scenegraph.chartInstanceListRowByRowDirection[row]) scenegraph.chartInstanceListRowByRowDirection[row][col].disableTooltip(!1);
}
//# sourceMappingURL=active-cell-chart-list.js.map
