export const FLASHING_CELL_ROW_KEY = '__ROW';
/**
 * This service manages Flashing Cells
 */
export class FlashingCellService {
    api;
    // map of rowPrimaryKey -> ColIdOrRowId -> FlashingCellUuid
    gridCellsCurrentlyFlashing;
    // map of FlashingCellUuid -> FlashingCell
    flashingCellsMapping;
    constructor(api) {
        this.api = api;
        this.gridCellsCurrentlyFlashing = {};
        this.flashingCellsMapping = {};
    }
    destroy() {
        this.gridCellsCurrentlyFlashing = {};
        this.flashingCellsMapping = {};
    }
    addGridCellFlash(flashingCell) {
        const { rowPrimaryKey } = flashingCell;
        this.gridCellsCurrentlyFlashing[rowPrimaryKey] =
            this.gridCellsCurrentlyFlashing[rowPrimaryKey] || {};
        const columnIds = Object.keys(flashingCell.flashColumnIds);
        if (flashingCell.flashTarget === 'row' || flashingCell.flashTarget.includes('row')) {
            columnIds.push(FLASHING_CELL_ROW_KEY);
        }
        columnIds.forEach((colId) => {
            this.gridCellsCurrentlyFlashing[rowPrimaryKey][colId] = flashingCell.Uuid;
        });
        this.flashingCellsMapping[flashingCell.Uuid] = flashingCell;
        this.refreshGridCells(flashingCell);
    }
    clearGridCellFlash(flashingCell) {
        const { rowPrimaryKey } = flashingCell;
        const columnIds = Object.keys(flashingCell.flashColumnIds);
        if (flashingCell.flashTarget === 'row' || flashingCell.flashTarget.includes('row')) {
            columnIds.push(FLASHING_CELL_ROW_KEY);
        }
        // for high frequency rates, we might have multiple flashes for the same cell
        if (this.gridCellsCurrentlyFlashing[rowPrimaryKey]) {
            columnIds.forEach((colId) => {
                if (this.gridCellsCurrentlyFlashing[rowPrimaryKey][colId] === flashingCell.Uuid) {
                    delete this.gridCellsCurrentlyFlashing[rowPrimaryKey][colId];
                }
            });
            if (!Object.keys(this.gridCellsCurrentlyFlashing[rowPrimaryKey]).length) {
                delete this.gridCellsCurrentlyFlashing[rowPrimaryKey];
            }
        }
        delete this.flashingCellsMapping[flashingCell.Uuid];
        this.refreshGridCells(flashingCell);
    }
    clearAllGridCellFlashes() {
        this.gridCellsCurrentlyFlashing = {};
        this.flashingCellsMapping = {};
        this.api.gridApi.refreshAllCells(true);
    }
    isAnyFlashingCellActive() {
        return Object.keys(this.gridCellsCurrentlyFlashing).length > 0;
    }
    refreshGridCells(flashingCell) {
        const { cellDataChangedInfo } = flashingCell;
        if (!cellDataChangedInfo) {
            return;
        }
        this.api.gridApi.refreshCells(cellDataChangedInfo.rowNode, Object.keys(flashingCell.flashColumnIds), true);
    }
}
