import { Temporal } from "@js-temporal/polyfill";
import "urlpattern-polyfill";
globalThis.addEventListener = () => {};
import { g as Multikey, s as CryptographicKey } from "./vocab-C5EUmVTj.mjs";
//#region src/federation/keycache.ts
const NULL_KEY_CACHE_VALUE = { _fedify: "key-unavailable" };
const NULL_KEY_CACHE_TTL = Temporal.Duration.from({ minutes: 5 });
function isNullKeyCacheValue(value) {
	return typeof value === "object" && value != null && "_fedify" in value && value._fedify === NULL_KEY_CACHE_VALUE._fedify;
}
var KvKeyCache = class {
	kv;
	prefix;
	options;
	nullKeys;
	constructor(kv, prefix, options = {}) {
		this.kv = kv;
		this.prefix = prefix;
		this.nullKeys = /* @__PURE__ */ new Set();
		this.options = options;
	}
	async get(keyId) {
		if (this.nullKeys.has(keyId.href)) return null;
		const serialized = await this.kv.get([...this.prefix, keyId.href]);
		if (serialized == null) return void 0;
		if (isNullKeyCacheValue(serialized)) {
			this.nullKeys.add(keyId.href);
			return null;
		}
		try {
			return await CryptographicKey.fromJsonLd(serialized, this.options);
		} catch {
			try {
				return await Multikey.fromJsonLd(serialized, this.options);
			} catch {
				await this.kv.delete([...this.prefix, keyId.href]);
				return;
			}
		}
	}
	async set(keyId, key) {
		if (key == null) {
			this.nullKeys.add(keyId.href);
			await this.kv.set([...this.prefix, keyId.href], NULL_KEY_CACHE_VALUE, { ttl: NULL_KEY_CACHE_TTL });
			return;
		}
		this.nullKeys.delete(keyId.href);
		const serialized = await key.toJsonLd(this.options);
		await this.kv.set([...this.prefix, keyId.href], serialized);
	}
};
//#endregion
export { KvKeyCache as t };
