import { AssetHandle } from "./AssetHandle.js";
export class AssetCache {
    entries = new Map();
    inFlight = new Map();
    has(key) {
        return this.entries.has(key) || this.inFlight.has(key);
    }
    get(key) {
        const entry = this.entries.get(key);
        if (!entry) {
            return undefined;
        }
        return entry.handle;
    }
    getInFlight(key) {
        return this.inFlight.get(key);
    }
    set(key, handle) {
        this.entries.set(key, { handle: handle });
    }
    setInFlight(key, promise) {
        this.inFlight.set(key, promise);
        promise
            .then((handle) => {
            this.entries.set(key, { handle: handle });
        })
            .catch(() => {
            // Failed loads are intentionally not cached; callers receive the original rejection.
        })
            .finally(() => {
            this.inFlight.delete(key);
        });
    }
    delete(key) {
        return this.entries.delete(key);
    }
    clear() {
        this.entries.clear();
        this.inFlight.clear();
    }
    keys() {
        return [...new Set([...this.entries.keys(), ...this.inFlight.keys()])];
    }
    snapshot() {
        const keys = this.keys();
        return {
            cachedEntries: this.entries.size,
            inFlightEntries: this.inFlight.size,
            totalEntries: keys.length,
            keys
        };
    }
}
//# sourceMappingURL=AssetCache.js.map