const tableCacheMap = new WeakMap();
const columnCacheMap = new WeakMap();
const tableMetadataMap = new WeakMap();
function getTableCache(args) {
    let cache = tableCacheMap.get(args.engine.runtimeCacheRef);
    if (!cache) {
        cache = new Set();
        tableCacheMap.set(args.engine.runtimeCacheRef, cache);
    }
    return cache;
}
function getColumnCache(args) {
    let cache = columnCacheMap.get(args.engine.runtimeCacheRef);
    if (!cache) {
        cache = new Map();
        columnCacheMap.set(args.engine.runtimeCacheRef, cache);
    }
    return cache;
}
function getTableMetadataCache(args) {
    let cache = tableMetadataMap.get(args.engine.runtimeCacheRef);
    if (!cache) {
        cache = new Map();
        tableMetadataMap.set(args.engine.runtimeCacheRef, cache);
    }
    return cache;
}
export function getStateCacheV2Tables(args) {
    return getTableCache(args);
}
export function getStateCacheV2Columns(args) {
    const columnCache = getColumnCache({ engine: args.engine });
    let columns = columnCache.get(args.tableName);
    if (!columns) {
        columns = fetchTableColumns(args);
        columnCache.set(args.tableName, columns);
    }
    return columns;
}
export function clearStateCacheV2Columns(args) {
    const columnCache = getColumnCache({ engine: args.engine });
    columnCache.delete(args.tableName);
}
export function registerStateCacheV2TableMetadata(args) {
    const metadataCache = getTableMetadataCache({ engine: args.engine });
    metadataCache.set(args.tableName, {
        schemaKey: args.schemaKey,
        schemaVersion: args.schemaVersion,
    });
}
export function getStateCacheV2TableMetadata(args) {
    const metadataCache = getTableMetadataCache({ engine: args.engine });
    return metadataCache.get(args.tableName);
}
export function clearStateCacheV2TableMetadata(args) {
    const metadataCache = getTableMetadataCache({ engine: args.engine });
    metadataCache.delete(args.tableName);
}
function fetchTableColumns(args) {
    const result = args.engine.executeSync({
        sql: `PRAGMA table_info(${args.tableName})`,
    });
    const rows = Array.isArray(result?.rows) ? result.rows : [];
    const columns = new Set();
    for (const row of rows) {
        if (typeof row?.name === "string") {
            columns.add(row.name);
        }
    }
    return columns;
}
//# sourceMappingURL=schema.js.map