/**
 * Cooldown Manager Module
 *
 * Manages provider cooldown TTLs and health snapshot persistence.
 */
export class CooldownManager {
    providerCooldowns = new Map();
    healthStore;
    healthConfig = null;
    quotaView;
    constructor(deps) {
        if (deps?.healthStore)
            this.healthStore = deps.healthStore;
        if (deps?.healthConfig !== undefined)
            this.healthConfig = deps.healthConfig;
        if (deps?.quotaView)
            this.quotaView = deps.quotaView;
    }
    updateDeps(deps) {
        if ('healthStore' in deps)
            this.healthStore = deps.healthStore ?? undefined;
        if ('healthConfig' in deps)
            this.healthConfig = deps.healthConfig ?? null;
        if ('quotaView' in deps) {
            const prevQuotaEnabled = Boolean(this.quotaView);
            this.quotaView = deps.quotaView ?? undefined;
            const nextQuotaEnabled = Boolean(this.quotaView);
            // When quotaView is enabled, cooldown must be driven by quotaView only.
            if (!prevQuotaEnabled && nextQuotaEnabled) {
                this.providerCooldowns.clear();
            }
            else if (prevQuotaEnabled && !nextQuotaEnabled) {
                this.providerCooldowns.clear();
                this.restoreHealthFromStore();
            }
        }
    }
    markProviderCooldown(providerKey, cooldownMs) {
        if (!providerKey)
            return;
        const ttl = typeof cooldownMs === 'number' ? Math.round(cooldownMs) : Number.NaN;
        if (!Number.isFinite(ttl) || ttl <= 0)
            return;
        this.providerCooldowns.set(providerKey, Date.now() + ttl);
        this.persistHealthSnapshot();
    }
    clearProviderCooldown(providerKey) {
        if (!providerKey)
            return;
        if (this.providerCooldowns.delete(providerKey)) {
            this.persistHealthSnapshot();
        }
    }
    isProviderCoolingDown(providerKey) {
        if (!providerKey)
            return false;
        const expiry = this.providerCooldowns.get(providerKey);
        if (!expiry)
            return false;
        if (Date.now() >= expiry) {
            this.providerCooldowns.delete(providerKey);
            return false;
        }
        return true;
    }
    restoreHealthFromStore() {
        if (!this.healthStore || typeof this.healthStore.loadInitialSnapshot !== 'function')
            return;
        // When quotaView is enabled, health/cooldown must be driven by quotaView only.
        if (this.quotaView)
            return;
        let snapshot = null;
        try {
            snapshot = this.healthStore.loadInitialSnapshot();
        }
        catch {
            snapshot = null;
        }
        if (!snapshot)
            return;
        const now = Date.now();
        const byKey = new Map();
        for (const entry of snapshot.cooldowns || []) {
            if (!entry?.providerKey || !Number.isFinite(entry.cooldownExpiresAt) || entry.cooldownExpiresAt <= now)
                continue;
            byKey.set(entry.providerKey, entry);
            this.providerCooldowns.set(entry.providerKey, entry.cooldownExpiresAt);
        }
        // Note: Provider health manager's state is separate; we only restore local cooldowns.
    }
    buildHealthSnapshot() {
        const cooldowns = [];
        const now = Date.now();
        for (const [providerKey, expiry] of this.providerCooldowns.entries()) {
            if (!expiry || expiry <= now)
                continue;
            cooldowns.push({ providerKey, cooldownExpiresAt: expiry });
        }
        return { providers: [], cooldowns }; // providers part handled by health manager
    }
    persistHealthSnapshot() {
        if (!this.healthStore || typeof this.healthStore.persistSnapshot !== 'function')
            return;
        try {
            const snapshot = this.buildHealthSnapshot();
            this.healthStore.persistSnapshot(snapshot);
        }
        catch {
            // persistence failure does not affect routing
        }
    }
    clearAllCooldowns() {
        this.providerCooldowns.clear();
    }
    getCooldownMap() {
        return this.providerCooldowns;
    }
}
