import { MAX_COLS, MAX_ROWS } from '@bilig/protocol';
const SHEET_STRIDE = MAX_ROWS * MAX_COLS;
export function makeCellKey(sheetId, row, col) {
    return sheetId * SHEET_STRIDE + row * MAX_COLS + col;
}
export function makeLogicalCellKey(sheetId, rowId, colId) {
    return `${sheetId}\t${rowId}\t${colId}`;
}
export function createCellKeyIndexMap(resolve) {
    return new LogicalCellKeyIndexMap(resolve);
}
function decodeCellKey(key) {
    if (!Number.isInteger(key) || key < 0) {
        return undefined;
    }
    const sheetId = Math.floor(key / SHEET_STRIDE);
    const offset = key - sheetId * SHEET_STRIDE;
    const row = Math.floor(offset / MAX_COLS);
    const col = offset - row * MAX_COLS;
    if (sheetId <= 0 || row < 0 || row >= MAX_ROWS || col < 0 || col >= MAX_COLS) {
        return undefined;
    }
    return { sheetId, row, col };
}
class LogicalCellKeyIndexMap extends Map {
    resolve;
    constructor(resolve) {
        super();
        this.resolve = resolve;
    }
    set(key, value) {
        if (decodeCellKey(key)) {
            return this;
        }
        return super.set(key, value);
    }
    get(key) {
        const decoded = decodeCellKey(key);
        if (decoded) {
            return this.resolve(decoded.sheetId, decoded.row, decoded.col);
        }
        return super.get(key);
    }
    has(key) {
        return this.get(key) !== undefined;
    }
}
//# sourceMappingURL=workbook-cell-key-index.js.map