import { config } from "../config.js";
import { listHistoryRows } from "./historyList.js";
const HISTORY_INDEX_TTL_MS = 3000;
let snapshot = null;
let pending = null;
function isFreshIndex(current, baseDir) {
    return Boolean(current &&
        current.baseDir === baseDir &&
        Date.now() - current.builtAt < HISTORY_INDEX_TTL_MS);
}
export async function getHistoryIndex(baseDir = config.storage.generatedDir) {
    if (isFreshIndex(snapshot, baseDir))
        return snapshot;
    if (pending)
        return pending;
    pending = (async () => {
        const rows = await listHistoryRows(baseDir);
        const next = { baseDir, builtAt: Date.now(), rows };
        snapshot = next;
        return next;
    })().finally(() => {
        pending = null;
    });
    return pending;
}
export function invalidateHistoryIndex() {
    snapshot = null;
    pending = null;
}
export function invalidateFavoriteOverlay() {
    // Favorite state is browser-scoped and read from SQLite per request today.
    // Keep a no-op invalidation seam so future overlay caches do not touch the
    // global history index or leak favorite state across browsers.
}
