import { createLogger } from "@open-mercato/shared/lib/logger";
const logger = createLogger("customers");
const CACHE_PREFIX = "customers:dictionaries";
const DICTIONARY_CACHE_TTL_MS = 5 * 60 * 1e3;
function createDictionaryCacheKey(options) {
  const scope = options.readableOrganizationIds.join("|");
  const organizationPart = options.organizationId ?? "all";
  const sortPart = options.sortMode ? `:sort=${options.sortMode}` : "";
  return `${CACHE_PREFIX}:${options.tenantId}:${options.mappedKind}:org=${organizationPart}:scope=${scope}${sortPart}`;
}
function createDictionaryCacheTags(options) {
  const tags = /* @__PURE__ */ new Set();
  if (options.includeBase !== false) {
    tags.add(`${CACHE_PREFIX}:${options.tenantId}:${options.mappedKind}`);
  }
  for (const orgId of options.organizationIds) {
    if (orgId) {
      tags.add(`${CACHE_PREFIX}:${options.tenantId}:${options.mappedKind}:org:${orgId}`);
    }
  }
  return Array.from(tags);
}
async function invalidateDictionaryCache(cache, options) {
  if (!cache?.deleteByTags) return;
  const tags = createDictionaryCacheTags({ ...options, includeBase: false });
  if (!tags.length) return;
  try {
    await cache.deleteByTags(tags);
  } catch (err) {
    logger.warn("Failed to invalidate cache", { component: "dictionaries.cache", err });
  }
}
export {
  DICTIONARY_CACHE_TTL_MS,
  createDictionaryCacheKey,
  createDictionaryCacheTags,
  invalidateDictionaryCache
};
//# sourceMappingURL=cache.js.map
