import { collectCrudCacheStats, purgeCrudCacheSegment } from "@open-mercato/shared/lib/crud/cache-stats";
function normalizeUnique(values) {
  const seen = /* @__PURE__ */ new Set();
  const normalized = [];
  for (const value of values) {
    const trimmed = value.trim();
    if (!trimmed || seen.has(trimmed)) continue;
    seen.add(trimmed);
    normalized.push(trimmed);
  }
  return normalized;
}
async function deleteKeys(cache, keys) {
  let deleted = 0;
  for (const key of keys) {
    const removed = await cache.delete(key);
    if (removed) deleted += 1;
  }
  return deleted;
}
async function resolveIdentifierKeys(cache, ids) {
  const matches = /* @__PURE__ */ new Set();
  for (const id of normalizeUnique(ids)) {
    const keys = await cache.keys(`*${id}*`);
    for (const key of keys) matches.add(key);
  }
  return Array.from(matches).sort((a, b) => a.localeCompare(b));
}
async function collectCacheStats(cache) {
  const stats = await collectCrudCacheStats(cache);
  return {
    generatedAt: stats.generatedAt,
    totalKeys: stats.totalKeys,
    segments: stats.segments.map((segment) => ({
      segment: segment.segment,
      keyCount: segment.keyCount,
      method: segment.method,
      path: segment.path,
      resource: segment.resource
    }))
  };
}
async function previewCachePurge(cache, request) {
  if (request.kind === "all") {
    const keys2 = (await cache.keys()).sort((a, b) => a.localeCompare(b));
    return { deleted: keys2.length, keys: keys2, note: null };
  }
  if (request.kind === "segment") {
    const stats = await collectCrudCacheStats(cache);
    const target = stats.segments.find((segment) => segment.segment === request.segment);
    return {
      deleted: target?.keys.length ?? 0,
      keys: (target?.keys ?? []).slice().sort((a, b) => a.localeCompare(b)),
      note: target ? null : `Cache segment "${request.segment}" was not found in this scope.`
    };
  }
  if (request.kind === "tags") {
    return {
      deleted: 0,
      keys: [],
      note: "Tag purges do not expose matching keys through the cache interface. Run without `--dry-run` to execute."
    };
  }
  if (request.kind === "keys") {
    const requested = normalizeUnique(request.keys);
    const existingKeys = await cache.keys();
    const existingSet = new Set(existingKeys);
    const keys2 = requested.filter((key) => existingSet.has(key));
    return {
      deleted: keys2.length,
      keys: keys2,
      note: keys2.length === requested.length ? null : "Some requested keys were not present in this scope."
    };
  }
  if (request.kind === "ids") {
    const keys2 = await resolveIdentifierKeys(cache, request.ids);
    return {
      deleted: keys2.length,
      keys: keys2,
      note: keys2.length > 0 ? null : "No cache keys matched the requested identifier tokens."
    };
  }
  const keys = (await cache.keys(request.pattern)).sort((a, b) => a.localeCompare(b));
  return {
    deleted: keys.length,
    keys,
    note: keys.length > 0 ? null : `No cache keys matched pattern "${request.pattern}".`
  };
}
async function executeCachePurge(cache, request) {
  if (request.kind === "all") {
    const keys2 = (await cache.keys()).sort((a, b) => a.localeCompare(b));
    const deleted2 = await cache.clear();
    return { deleted: deleted2, keys: keys2, note: null };
  }
  if (request.kind === "segment") {
    const result = await purgeCrudCacheSegment(cache, request.segment);
    return {
      deleted: result.deleted,
      keys: result.keys.slice().sort((a, b) => a.localeCompare(b)),
      note: result.keys.length > 0 ? null : `Cache segment "${request.segment}" was not found in this scope.`
    };
  }
  if (request.kind === "tags") {
    const deleted2 = await cache.deleteByTags(normalizeUnique(request.tags));
    return {
      deleted: deleted2,
      keys: [],
      note: "Tag purges report counts only because the cache interface does not expose tag-to-key listings."
    };
  }
  if (request.kind === "keys") {
    const preview = await previewCachePurge(cache, request);
    const deleted2 = await deleteKeys(cache, preview.keys);
    return {
      deleted: deleted2,
      keys: preview.keys,
      note: preview.note
    };
  }
  if (request.kind === "ids") {
    const keys2 = await resolveIdentifierKeys(cache, request.ids);
    const deleted2 = await deleteKeys(cache, keys2);
    return {
      deleted: deleted2,
      keys: keys2,
      note: keys2.length > 0 ? null : "No cache keys matched the requested identifier tokens."
    };
  }
  const keys = (await cache.keys(request.pattern)).sort((a, b) => a.localeCompare(b));
  const deleted = await deleteKeys(cache, keys);
  return {
    deleted,
    keys,
    note: keys.length > 0 ? null : `No cache keys matched pattern "${request.pattern}".`
  };
}
export {
  collectCacheStats,
  executeCachePurge,
  previewCachePurge
};
//# sourceMappingURL=cache-cli.js.map
