// src/memory-cache.ts
var hotCacheByDir = /* @__PURE__ */ new Map();
var archiveCacheByDir = /* @__PURE__ */ new Map();
function getCachedMemories(baseDir, currentVersion) {
  if (currentVersion === 0) return null;
  const entry = hotCacheByDir.get(baseDir);
  if (!entry || entry.version !== currentVersion) return null;
  return [...entry.memories.values()];
}
function setCachedMemories(baseDir, memories, version) {
  const map = /* @__PURE__ */ new Map();
  for (const m of memories) map.set(m.path, m);
  hotCacheByDir.set(baseDir, { memories: map, version, loadedAt: Date.now() });
}
function updateCacheOnWrite(baseDir, memory) {
  const entry = hotCacheByDir.get(baseDir);
  if (entry) entry.memories.set(memory.path, memory);
}
function updateCacheOnDelete(baseDir, filePath) {
  const entry = hotCacheByDir.get(baseDir);
  if (entry) entry.memories.delete(filePath);
}
function getCachedArchivedMemories(baseDir, currentVersion) {
  if (currentVersion === 0) return null;
  const entry = archiveCacheByDir.get(baseDir);
  if (!entry || entry.version !== currentVersion) return null;
  return [...entry.memories.values()];
}
function setCachedArchivedMemories(baseDir, memories, version) {
  const map = /* @__PURE__ */ new Map();
  for (const m of memories) map.set(m.path, m);
  archiveCacheByDir.set(baseDir, { memories: map, version, loadedAt: Date.now() });
}
var entityCacheByDir = /* @__PURE__ */ new Map();
function buildEntityCacheKey(baseDir, schemaKey = "") {
  return `${baseDir}\0${schemaKey}`;
}
function getCachedEntities(baseDir, currentVersion, schemaKey = "") {
  if (currentVersion === 0) return null;
  const entry = entityCacheByDir.get(buildEntityCacheKey(baseDir, schemaKey));
  if (!entry || entry.version !== currentVersion) return null;
  return entry.entities;
}
function setCachedEntities(baseDir, entities, version, schemaKey = "") {
  entityCacheByDir.set(buildEntityCacheKey(baseDir, schemaKey), {
    entities,
    version,
    loadedAt: Date.now()
  });
}
function invalidateCachedEntities(baseDir) {
  const prefix = `${baseDir}\0`;
  for (const key of entityCacheByDir.keys()) {
    if (key.startsWith(prefix)) entityCacheByDir.delete(key);
  }
}
var episodeMapByDir = /* @__PURE__ */ new Map();
var ruleMemoriesByDir = /* @__PURE__ */ new Map();
function getCachedEpisodeMap(baseDir, currentVersion) {
  if (currentVersion === 0) return null;
  const entry = episodeMapByDir.get(baseDir);
  if (!entry || entry.sourceVersion !== currentVersion) return null;
  return entry.data;
}
function setCachedEpisodeMap(baseDir, memories, version) {
  const map = /* @__PURE__ */ new Map();
  for (const m of memories) {
    if (m.frontmatter.status === "archived" || m.frontmatter.status === "forgotten") continue;
    if (m.frontmatter.memoryKind !== "episode") continue;
    map.set(m.frontmatter.id, m);
  }
  episodeMapByDir.set(baseDir, { data: map, sourceVersion: version });
  return map;
}
function getCachedRuleMemories(baseDir, currentVersion) {
  if (currentVersion === 0) return null;
  const entry = ruleMemoriesByDir.get(baseDir);
  if (!entry || entry.sourceVersion !== currentVersion) return null;
  return entry.data;
}
function setCachedRuleMemories(baseDir, memories, version) {
  const byId = /* @__PURE__ */ new Map();
  const all = [];
  for (const m of memories) {
    byId.set(m.frontmatter.id, m);
    if (m.frontmatter.category === "rule" && m.frontmatter.status !== "archived" && m.frontmatter.status !== "forgotten") {
      all.push(m);
    }
  }
  const result = { all, byId };
  ruleMemoriesByDir.set(baseDir, { data: result, sourceVersion: version });
  return result;
}
var QMD_CACHE_TTL_MS = 6e4;
var qmdSearchCache = /* @__PURE__ */ new Map();
function getCachedQmdSearch(cacheKey) {
  const entry = qmdSearchCache.get(cacheKey);
  if (!entry) return null;
  if (Date.now() - entry.cachedAt > QMD_CACHE_TTL_MS) {
    qmdSearchCache.delete(cacheKey);
    return null;
  }
  return entry.results;
}
function setCachedQmdSearch(cacheKey, results) {
  qmdSearchCache.set(cacheKey, { results, cachedAt: Date.now() });
  if (qmdSearchCache.size > 200) {
    const now = Date.now();
    for (const [key, entry] of qmdSearchCache) {
      if (now - entry.cachedAt > QMD_CACHE_TTL_MS) qmdSearchCache.delete(key);
    }
  }
}
function clearMemoryCache(baseDir) {
  if (baseDir) {
    hotCacheByDir.delete(baseDir);
    archiveCacheByDir.delete(baseDir);
    invalidateCachedEntities(baseDir);
    episodeMapByDir.delete(baseDir);
    ruleMemoriesByDir.delete(baseDir);
  } else {
    hotCacheByDir.clear();
    archiveCacheByDir.clear();
    entityCacheByDir.clear();
    episodeMapByDir.clear();
    ruleMemoriesByDir.clear();
    qmdSearchCache.clear();
  }
}
function getMemoryCacheStats(baseDir) {
  const hot = hotCacheByDir.get(baseDir);
  const archive = archiveCacheByDir.get(baseDir);
  return {
    hotSize: hot?.memories.size ?? 0,
    archiveSize: archive?.memories.size ?? 0,
    hotVersion: hot?.version ?? null,
    archiveVersion: archive?.version ?? null
  };
}

export {
  getCachedMemories,
  setCachedMemories,
  updateCacheOnWrite,
  updateCacheOnDelete,
  getCachedArchivedMemories,
  setCachedArchivedMemories,
  getCachedEntities,
  setCachedEntities,
  invalidateCachedEntities,
  getCachedEpisodeMap,
  setCachedEpisodeMap,
  getCachedRuleMemories,
  setCachedRuleMemories,
  getCachedQmdSearch,
  setCachedQmdSearch,
  clearMemoryCache,
  getMemoryCacheStats
};
//# sourceMappingURL=chunk-PFV5C235.js.map