const GLOBAL_KEY = "__openMercatoRbacFallbackCache__";
const MAX_ENTRIES = 5e3;
function isRbacDefaultCacheEnabled() {
  const raw = process.env.OM_RBAC_DEFAULT_CACHE;
  if (raw === void 0) return false;
  const normalized = raw.trim().toLowerCase();
  if (!normalized.length) return false;
  return normalized === "on" || normalized === "1" || normalized === "true" || normalized === "yes";
}
function nowMs() {
  return Date.now();
}
function createCache() {
  const store = /* @__PURE__ */ new Map();
  const touch = (key) => {
    const entry = store.get(key);
    if (!entry) return void 0;
    if (entry.expiresAt !== null && entry.expiresAt < nowMs()) {
      store.delete(key);
      return void 0;
    }
    store.delete(key);
    store.set(key, entry);
    return entry;
  };
  const evictIfNeeded = () => {
    while (store.size > MAX_ENTRIES) {
      const oldest = store.keys().next().value;
      if (typeof oldest !== "string") break;
      store.delete(oldest);
    }
  };
  const cache = {
    async get(key, _options) {
      const entry = touch(key);
      return entry ? entry.value : null;
    },
    async set(key, value, options) {
      const ttl = options?.ttl ?? null;
      store.delete(key);
      store.set(key, {
        value,
        tags: options?.tags ?? [],
        expiresAt: typeof ttl === "number" && ttl > 0 ? nowMs() + ttl : null
      });
      evictIfNeeded();
    },
    async has(key) {
      return touch(key) !== void 0;
    },
    async delete(key) {
      return store.delete(key);
    },
    async deleteByTags(tags) {
      if (!tags.length) return 0;
      const tagSet = new Set(tags);
      let removed = 0;
      for (const [key, entry] of store.entries()) {
        if (entry.tags.some((tag) => tagSet.has(tag))) {
          store.delete(key);
          removed += 1;
        }
      }
      return removed;
    },
    async clear() {
      const count = store.size;
      store.clear();
      return count;
    },
    async keys(pattern) {
      if (!pattern) return Array.from(store.keys());
      const matcher = pattern.replace(/[.+^${}()|[\]\\]/g, "\\$&").replace(/\*/g, ".*").replace(/\?/g, ".");
      const regex = new RegExp(`^${matcher}$`);
      return Array.from(store.keys()).filter((key) => regex.test(key));
    },
    async size() {
      return store.size;
    },
    async stats() {
      const now = nowMs();
      let expired = 0;
      for (const entry of store.values()) {
        if (entry.expiresAt !== null && entry.expiresAt < now) expired += 1;
      }
      return { size: store.size, expired };
    },
    __reset() {
      store.clear();
    }
  };
  return cache;
}
function createRbacFallbackCache() {
  const existing = globalThis[GLOBAL_KEY];
  if (existing) return existing;
  const cache = createCache();
  globalThis[GLOBAL_KEY] = cache;
  return cache;
}
function resetRbacFallbackCache() {
  const existing = globalThis[GLOBAL_KEY];
  existing?.__reset();
}
export {
  createRbacFallbackCache,
  isRbacDefaultCacheEnabled,
  resetRbacFallbackCache
};
//# sourceMappingURL=rbacDefaultCache.js.map
