import { FILENAMES, init_paths } from "../config/paths.js";
import { SessionSearchIndex } from "./search-index.js";
import { stat } from "node:fs/promises";
import { join } from "node:path";
//#region src/session/search-index-cache.ts
/**
* Shared cache for {@link SessionSearchIndex} builds (invalidated on session writes).
*/
init_paths();
const INDEX_CACHE_TTL_MS = 9e4;
let indexCache = null;
function invalidateSessionSearchIndexCache() {
	indexCache = null;
}
async function getOrLoadSessionSearchIndex(sessionsRoot) {
	const indexPath = join(sessionsRoot, FILENAMES.SESSIONS_MAP);
	let mtime = 0;
	try {
		mtime = (await stat(indexPath)).mtimeMs;
	} catch {
		mtime = 0;
	}
	const now = Date.now();
	if (indexCache && indexCache.root === sessionsRoot && indexCache.indexFileMtime === mtime && now - indexCache.builtAt < INDEX_CACHE_TTL_MS) return indexCache.index;
	const idx = new SessionSearchIndex();
	await idx.buildIndex(sessionsRoot);
	let m2 = mtime;
	try {
		m2 = (await stat(indexPath)).mtimeMs;
	} catch {
		m2 = Date.now();
	}
	indexCache = {
		root: sessionsRoot,
		index: idx,
		indexFileMtime: m2,
		builtAt: Date.now()
	};
	return idx;
}
//#endregion
export { getOrLoadSessionSearchIndex, invalidateSessionSearchIndexCache };

//# sourceMappingURL=search-index-cache.js.map