/**
 * Best-effort cache for the SDK.
 *
 * The SDK uses this to avoid re-fetching values it can recreate, primarily
 * client_credentials access tokens. Cache entries may disappear at any time:
 * a miss is normal and callers must be able to rebuild the value.
 *
 * The default cache is in-memory. When the CLI package is installed, the SDK
 * can opportunistically use its filesystem + keychain cache. Browser and
 * minimal server deployments fall back to memory unless a caller injects a
 * Redis, database, or environment-specific cache.
 *
 * Secrets: adapters receive a `secret: true` flag on `set` and decide
 * what to do with it (keychain for the filesystem adapter; ignored for
 * the in-memory adapter; user adapters can throw, plaintext, encrypt —
 * their call). The value returned by `get` is always the plain secret.
 */
/**
 * Simplest possible adapter: a Map. No persistence, no locking (single
 * process only), no differentiation between secrets and non-secrets.
 * This is the default when cli-login isn't installed and no custom
 * cache is provided.
 */
export function createMemoryCache() {
    const store = new Map();
    return {
        async get(key) {
            const entry = store.get(key);
            if (!entry)
                return undefined;
            if (entry.expiresAt !== undefined && entry.expiresAt <= Date.now()) {
                store.delete(key);
                return undefined;
            }
            return { value: entry.value, expiresAt: entry.expiresAt };
        },
        async set(key, value, options) {
            const expiresAt = options?.ttl
                ? Date.now() + options.ttl * 1000
                : undefined;
            store.set(key, { value, expiresAt });
        },
        async delete(key) {
            store.delete(key);
        },
    };
}
