// src/qmd-recall-cache.ts
var qmdRecallCache = /* @__PURE__ */ new Map();
function cloneCacheValue(value) {
  return structuredClone(value);
}
function normalizeQuery(query) {
  return query.trim().toLowerCase().replace(/\s+/g, " ");
}
function normalizePathScope(pathValue) {
  if (typeof pathValue !== "string") return "";
  return pathValue.trim().replace(/\\/g, "/");
}
function normalizeSearchOptions(options) {
  if (!options) return {};
  return Object.fromEntries(
    Object.entries(options).filter(([, value]) => value !== void 0).sort(([left], [right]) => left.localeCompare(right))
  );
}
function buildQmdRecallCacheKey(options) {
  return JSON.stringify({
    query: normalizeQuery(options.query),
    namespaces: [...options.namespaces].sort(),
    recallMode: options.recallMode,
    maxResults: options.maxResults,
    memoryDir: normalizePathScope(options.memoryDir),
    collection: options.collection ?? "",
    searchOptions: normalizeSearchOptions(options.searchOptions)
  });
}
function getCachedQmdRecall(cacheKey, options) {
  const entry = qmdRecallCache.get(cacheKey);
  if (!entry) return null;
  const ageMs = Date.now() - entry.cachedAtMs;
  if (ageMs <= options.freshTtlMs) {
    return { value: cloneCacheValue(entry.value), source: "fresh", ageMs };
  }
  if (ageMs <= options.staleTtlMs) {
    return { value: cloneCacheValue(entry.value), source: "stale", ageMs };
  }
  qmdRecallCache.delete(cacheKey);
  return null;
}
function setCachedQmdRecall(cacheKey, value, options) {
  qmdRecallCache.delete(cacheKey);
  if (options.maxEntries <= 0) return;
  qmdRecallCache.set(cacheKey, {
    value: cloneCacheValue(value),
    cachedAtMs: Date.now()
  });
  while (qmdRecallCache.size > options.maxEntries) {
    const oldestKey = qmdRecallCache.keys().next().value;
    if (typeof oldestKey !== "string") break;
    qmdRecallCache.delete(oldestKey);
  }
}
function clearQmdRecallCache() {
  qmdRecallCache.clear();
}

export {
  buildQmdRecallCacheKey,
  getCachedQmdRecall,
  setCachedQmdRecall,
  clearQmdRecallCache
};
//# sourceMappingURL=chunk-YCN4BVDK.js.map