import { expiryFromTtl, CacheStore } from './chunk-Y2ROUSC3.js';
import { decryptEnvBlobSync, encryptEnvBlobSync } from './chunk-IQ226IPZ.js';
import { __name } from './chunk-IBAHCH6O.js';
import { createHash } from 'crypto';

// src/lib/cache/in-memory-cache-store.ts
var InMemoryCacheStore = class {
  static {
    __name(this, "InMemoryCacheStore");
  }
  entries = /* @__PURE__ */ new Map();
  inFlight = /* @__PURE__ */ new Map();
  async get(cacheKey) {
    const entry = this.entries.get(cacheKey);
    if (!entry) return void 0;
    if (Date.now() > entry.expiresAt) {
      this.entries.delete(cacheKey);
      return void 0;
    }
    return {
      value: entry.value,
      cachedAt: entry.cachedAt,
      expiresAt: entry.expiresAt
    };
  }
  async set(cacheKey, value, ttlMs) {
    const now = Date.now();
    const expiresAt = expiryFromTtl(now, ttlMs);
    this.entries.set(cacheKey, { value, cachedAt: now, expiresAt });
    return { cachedAt: now, expiresAt };
  }
  async getOrSet(cacheKey, ttlMs, producer) {
    const existing = await this.get(cacheKey);
    if (existing) {
      return { ...existing, cacheHit: true };
    }
    const inFlight = this.inFlight.get(cacheKey);
    if (inFlight) return await inFlight;
    const pending = (async () => {
      const latest = await this.get(cacheKey);
      if (latest) return { ...latest, cacheHit: true };
      const value = await producer();
      if (value === void 0) return void 0;
      const stored = await this.set(cacheKey, value, ttlMs);
      return { value, ...stored, cacheHit: false };
    })();
    this.inFlight.set(cacheKey, pending);
    try {
      return await pending;
    } finally {
      if (this.inFlight.get(cacheKey) === pending) {
        this.inFlight.delete(cacheKey);
      }
    }
  }
  async delete(cacheKey) {
    this.entries.delete(cacheKey);
  }
  async clearAll() {
    const count = this.entries.size;
    this.entries.clear();
    return count;
  }
};
var CACHE_ENV_KEY_VAR = "_VARLOCK_CACHE_KEY";
function getCacheEnvKey(env = process.env) {
  return env[CACHE_ENV_KEY_VAR] || void 0;
}
__name(getCacheEnvKey, "getCacheEnvKey");
function createEnvKeyCacheStore(hexKey) {
  decryptEnvBlobSync(encryptEnvBlobSync("probe", hexKey), hexKey);
  const fingerprint = createHash("sha256").update(hexKey).digest("hex").slice(0, 12);
  return new CacheStore(`env-key-${fingerprint}`, {
    ensureReady: /* @__PURE__ */ __name(() => {
    }, "ensureReady"),
    encrypt: /* @__PURE__ */ __name((plaintext) => encryptEnvBlobSync(plaintext, hexKey), "encrypt"),
    decrypt: /* @__PURE__ */ __name((ciphertext) => decryptEnvBlobSync(ciphertext, hexKey), "decrypt")
  });
}
__name(createEnvKeyCacheStore, "createEnvKeyCacheStore");

export { CACHE_ENV_KEY_VAR, InMemoryCacheStore, createEnvKeyCacheStore, getCacheEnvKey };
//# sourceMappingURL=chunk-223S32SW.js.map
//# sourceMappingURL=chunk-223S32SW.js.map