export class FallbackSecureStore {
    primary;
    fallback;
    constructor(primary, fallback) {
        this.primary = primary;
        this.fallback = fallback;
    }
    async get(ref) {
        try {
            const primarySecret = await this.primary.get(ref);
            if (primarySecret !== undefined)
                return primarySecret;
        }
        catch (error) {
            if (!this.fallback)
                throw error;
        }
        return this.fallback?.get(ref);
    }
    async set(ref, secret) {
        try {
            await this.primary.set(ref, secret);
            return;
        }
        catch (error) {
            if (!this.fallback)
                throw error;
        }
        await this.fallback.set(ref, secret);
    }
    async delete(ref) {
        let primaryError;
        try {
            await this.primary.delete(ref);
        }
        catch (error) {
            primaryError = error;
        }
        if (this.fallback) {
            await this.fallback.delete(ref);
            return;
        }
        if (primaryError)
            throw primaryError;
    }
    async status(ref) {
        try {
            const primaryStatus = await this.primary.status(ref);
            if (primaryStatus.state !== "missing")
                return primaryStatus;
            if (!this.fallback)
                return primaryStatus;
        }
        catch (error) {
            if (!this.fallback)
                throw error;
        }
        const fallbackStatus = await this.fallback.status(ref);
        return fallbackStatus.state === "missing"
            ? fallbackStatus
            : { ...fallbackStatus, degraded: true };
    }
}
