import { MAX_COLS } from '@bilig/protocol';
export class WorkbookColumnVersionStore {
    options;
    batchedColumnVersionUpdates = null;
    constructor(options) {
        this.options = options;
    }
    withBatchedColumnVersionUpdates(execute) {
        if (this.batchedColumnVersionUpdates) {
            return execute();
        }
        const pending = new Map();
        this.batchedColumnVersionUpdates = pending;
        try {
            return execute();
        }
        finally {
            this.batchedColumnVersionUpdates = null;
            pending.forEach((columns, sheetId) => {
                columns.forEach((col) => {
                    this.bumpColumnVersion(sheetId, col);
                });
            });
        }
    }
    notifyCellValueWritten(cellIndex) {
        this.bumpColumnVersionByCellIndex(cellIndex);
    }
    notifyColumnsWritten(sheetId, columns) {
        const pending = this.batchedColumnVersionUpdates;
        if (pending) {
            const pendingColumns = ensurePendingColumns(pending, sheetId);
            for (let index = 0; index < columns.length; index += 1) {
                pendingColumns.add(columns[index]);
            }
            return;
        }
        for (let index = 0; index < columns.length; index += 1) {
            this.bumpColumnVersion(sheetId, columns[index]);
        }
    }
    notifyColumnWritten(sheetId, col) {
        const pending = this.batchedColumnVersionUpdates;
        if (pending) {
            ensurePendingColumns(pending, sheetId).add(col);
            return;
        }
        this.bumpColumnVersion(sheetId, col);
    }
    notifyColumnPairWritten(sheetId, firstCol, secondCol) {
        const pending = this.batchedColumnVersionUpdates;
        if (pending) {
            const pendingColumns = ensurePendingColumns(pending, sheetId);
            pendingColumns.add(firstCol);
            pendingColumns.add(secondCol);
            return;
        }
        this.bumpColumnVersion(sheetId, firstCol);
        if (secondCol !== firstCol) {
            this.bumpColumnVersion(sheetId, secondCol);
        }
    }
    bumpColumnVersionByCellIndex(cellIndex) {
        const sheetId = this.options.cellStore.sheetIds[cellIndex];
        const sheet = this.options.getSheetById(sheetId);
        if (!sheet) {
            return;
        }
        const col = sheet.structureVersion === 1
            ? this.options.cellStore.cols[cellIndex]
            : (sheet.logical.getCellVisiblePosition(cellIndex)?.col ?? this.options.cellStore.cols[cellIndex]);
        const pending = this.batchedColumnVersionUpdates;
        if (pending) {
            let columns = pending.get(sheetId);
            if (!columns) {
                columns = new Set();
                pending.set(sheetId, columns);
            }
            columns.add(col);
            return;
        }
        const columnVersions = ensureColumnVersionCapacity(sheet, col);
        columnVersions[col] = (columnVersions[col] ?? 0) + 1;
    }
    bumpColumnVersion(sheetId, col) {
        const sheet = this.options.getSheetById(sheetId);
        if (!sheet) {
            return;
        }
        const columnVersions = ensureColumnVersionCapacity(sheet, col);
        columnVersions[col] = (columnVersions[col] ?? 0) + 1;
    }
}
function ensurePendingColumns(pending, sheetId) {
    let pendingColumns = pending.get(sheetId);
    if (!pendingColumns) {
        pendingColumns = new Set();
        pending.set(sheetId, pendingColumns);
    }
    return pendingColumns;
}
function ensureColumnVersionCapacity(sheet, col) {
    if (col < sheet.columnVersions.length) {
        return sheet.columnVersions;
    }
    let nextLength = sheet.columnVersions.length === 0 ? 16 : sheet.columnVersions.length;
    while (nextLength <= col && nextLength < MAX_COLS) {
        nextLength = Math.min(MAX_COLS, nextLength * 2);
    }
    const nextColumnVersions = new Uint32Array(nextLength);
    nextColumnVersions.set(sheet.columnVersions);
    sheet.columnVersions = nextColumnVersions;
    return nextColumnVersions;
}
//# sourceMappingURL=workbook-column-version-store.js.map