export class CellAxisIdentityStore {
    sheetIds = [];
    rowIds = [];
    colIds = [];
    denseRowMajorBlocks = [];
    denseDeletedCellIndices = new Set();
    get(cellIndex) {
        const sheetId = this.sheetIds[cellIndex];
        const rowId = this.rowIds[cellIndex];
        const colId = this.colIds[cellIndex];
        if (sheetId !== undefined && rowId !== undefined && colId !== undefined) {
            return {
                sheetId,
                rowId,
                colId,
            };
        }
        return this.getDenseRowMajorIdentity(cellIndex);
    }
    set(cellIndex, identity) {
        this.setParts(cellIndex, identity.sheetId, identity.rowId, identity.colId);
    }
    setParts(cellIndex, sheetId, rowId, colId) {
        this.denseDeletedCellIndices.delete(cellIndex);
        this.sheetIds[cellIndex] = sheetId;
        this.rowIds[cellIndex] = rowId;
        this.colIds[cellIndex] = colId;
    }
    setDenseRowMajorParts(firstCellIndex, sheetId, rowIds, colIds) {
        const colCount = colIds.length;
        const cellCount = rowIds.length * colCount;
        if (cellCount === 0) {
            return;
        }
        this.denseRowMajorBlocks.push({
            firstCellIndex,
            cellCount,
            colCount,
            sheetId,
            rowIds,
            colIds,
        });
    }
    delete(cellIndex) {
        const hasStoredIdentity = this.sheetIds[cellIndex] !== undefined && this.rowIds[cellIndex] !== undefined && this.colIds[cellIndex] !== undefined;
        const hasDenseIdentity = this.getDenseRowMajorIdentity(cellIndex) !== undefined;
        if (!hasStoredIdentity && !hasDenseIdentity) {
            return false;
        }
        this.sheetIds[cellIndex] = undefined;
        this.rowIds[cellIndex] = undefined;
        this.colIds[cellIndex] = undefined;
        if (hasDenseIdentity) {
            this.denseDeletedCellIndices.add(cellIndex);
        }
        return true;
    }
    clear() {
        this.sheetIds.length = 0;
        this.rowIds.length = 0;
        this.colIds.length = 0;
        this.denseRowMajorBlocks.length = 0;
        this.denseDeletedCellIndices.clear();
    }
    forEach(callback) {
        for (let cellIndex = 0; cellIndex < this.rowIds.length; cellIndex += 1) {
            const sheetId = this.sheetIds[cellIndex];
            const rowId = this.rowIds[cellIndex];
            const colId = this.colIds[cellIndex];
            if (sheetId === undefined || rowId === undefined || colId === undefined) {
                continue;
            }
            callback({ sheetId, rowId, colId }, cellIndex);
        }
        const emittedDenseCellIndices = new Set();
        for (let blockIndex = this.denseRowMajorBlocks.length - 1; blockIndex >= 0; blockIndex -= 1) {
            const block = this.denseRowMajorBlocks[blockIndex];
            for (let offset = 0; offset < block.cellCount; offset += 1) {
                const cellIndex = block.firstCellIndex + offset;
                if (emittedDenseCellIndices.has(cellIndex) ||
                    this.denseDeletedCellIndices.has(cellIndex) ||
                    (this.sheetIds[cellIndex] !== undefined && this.rowIds[cellIndex] !== undefined && this.colIds[cellIndex] !== undefined)) {
                    continue;
                }
                emittedDenseCellIndices.add(cellIndex);
                const rowOffset = Math.floor(offset / block.colCount);
                const colOffset = offset - rowOffset * block.colCount;
                callback({
                    sheetId: block.sheetId,
                    rowId: block.rowIds[rowOffset],
                    colId: block.colIds[colOffset],
                }, cellIndex);
            }
        }
    }
    entries() {
        const entries = [];
        this.forEach((identity, cellIndex) => {
            entries.push([cellIndex, identity]);
        });
        return entries;
    }
    getDenseRowMajorIdentity(cellIndex) {
        if (this.denseDeletedCellIndices.has(cellIndex)) {
            return undefined;
        }
        for (let blockIndex = this.denseRowMajorBlocks.length - 1; blockIndex >= 0; blockIndex -= 1) {
            const block = this.denseRowMajorBlocks[blockIndex];
            const offset = cellIndex - block.firstCellIndex;
            if (offset < 0 || offset >= block.cellCount) {
                continue;
            }
            const rowOffset = Math.floor(offset / block.colCount);
            const colOffset = offset - rowOffset * block.colCount;
            return {
                sheetId: block.sheetId,
                rowId: block.rowIds[rowOffset],
                colId: block.colIds[colOffset],
            };
        }
        return undefined;
    }
}
//# sourceMappingURL=cell-axis-identity-store.js.map