/**
 * Fallback Provider
 *
 * Generic wrapper that implements LLMProvider by trying a primary provider first,
 * then falling back to a secondary provider on failure.
 *
 * Not Workers AI specific — works with any two providers.
 */
import { consoleLogger } from '../../utils/logger.js';
export class FallbackProvider {
    get name() { return this.primary.name; }
    get defaultModel() { return this.primary.defaultModel; }
    constructor(primary, fallback, logger) {
        this.primary = primary;
        this.fallback = fallback;
        this.logger = logger || consoleLogger;
    }
    async analyze(prompt, options) {
        try {
            return await this.primary.analyze(prompt, options);
        }
        catch (err) {
            this.logger.warn(`Primary provider (${this.primary.name}) failed: ${err.message}. Trying fallback (${this.fallback.name}).`);
            const result = await this.fallback.analyze(prompt, options);
            return { ...result, wasRetry: true };
        }
    }
    async analyzeStructured(prompt, options) {
        try {
            return await this.primary.analyzeStructured(prompt, options);
        }
        catch (err) {
            this.logger.warn(`Primary provider (${this.primary.name}) structured analysis failed: ${err.message}. Trying fallback.`);
            return await this.fallback.analyzeStructured(prompt, options);
        }
    }
    estimateCost(inputTokens, outputTokens, model) {
        return this.primary.estimateCost(inputTokens, outputTokens, model);
    }
    async isAvailable() {
        const primaryAvailable = await this.primary.isAvailable();
        if (primaryAvailable)
            return true;
        return await this.fallback.isAvailable();
    }
    async getStatus() {
        const primaryStatus = await this.primary.getStatus();
        if (primaryStatus.available)
            return primaryStatus;
        return await this.fallback.getStatus();
    }
}
//# sourceMappingURL=fallback-provider.js.map