import { addEngineCounter } from '../perf/engine-counters.js';
import { applyLookupColumnOwnerLiteralWrite, buildLookupColumnOwner, } from '../engine/services/lookup-column-owner.js';
function registryKey(sheetName, col) {
    return `${sheetName}\t${col}`;
}
function getCurrentColumnVersions(state, sheetName, col) {
    const emptyColumnVersions = new Uint32Array(0);
    const sheet = state.workbook.getSheet(sheetName);
    const sheetColumnVersions = sheet?.columnVersions ?? emptyColumnVersions;
    return {
        columnVersion: sheetColumnVersions[col] ?? 0,
        structureVersion: sheet?.structureVersion ?? 0,
        sheetColumnVersions,
    };
}
export function createColumnIndexStore(args) {
    const ownerIndices = new Map();
    const ensureLookupColumnOwner = (sheetName, col) => {
        const key = registryKey(sheetName, col);
        const currentVersions = getCurrentColumnVersions(args.state, sheetName, col);
        const existing = ownerIndices.get(key);
        if (existing &&
            existing.columnVersion === currentVersions.columnVersion &&
            existing.structureVersion === currentVersions.structureVersion &&
            existing.sheetColumnVersions === currentVersions.sheetColumnVersions) {
            return existing;
        }
        if (args.state.counters) {
            addEngineCounter(args.state.counters, 'lookupOwnerBuilds');
        }
        const owner = buildLookupColumnOwner({
            owner: args.runtimeColumnStore.getColumnOwner({ sheetName, col }),
            normalizeStringId: args.runtimeColumnStore.normalizeStringId,
        });
        if (owner) {
            ownerIndices.set(key, owner);
            return owner;
        }
        ownerIndices.delete(key);
        return undefined;
    };
    return {
        reset() {
            ownerIndices.clear();
        },
        getLookupColumnOwner(request) {
            return ensureLookupColumnOwner(request.sheetName, request.col);
        },
        invalidateColumn(request) {
            ownerIndices.delete(registryKey(request.sheetName, request.col));
        },
        recordLiteralWrite(request, options) {
            const key = registryKey(request.sheetName, request.col);
            const owner = ownerIndices.get(key);
            if (!owner) {
                return;
            }
            const currentVersions = getCurrentColumnVersions(args.state, request.sheetName, request.col);
            owner.columnVersion = currentVersions.columnVersion;
            owner.structureVersion = currentVersions.structureVersion;
            owner.sheetColumnVersions = currentVersions.sheetColumnVersions;
            if (!applyLookupColumnOwnerLiteralWrite({
                owner,
                write: request,
                normalizeStringId: args.runtimeColumnStore.normalizeStringId,
                ...options,
            })) {
                ownerIndices.delete(key);
            }
        },
    };
}
//# sourceMappingURL=column-index-store.js.map