/**
 * @license
 * Copyright 2025 OElite Platform
 * SPDX-License-Identifier: Apache-2.0
 */
import { getProviderMetadata } from './providerMetadata.js';
/**
 * Base provider class with common functionality
 */
export class BaseProvider {
    type;
    name;
    config;
    initialized = false;
    constructor(config) {
        this.config = config;
        this.type = config.type;
        this.name = config.name || config.type;
    }
    /**
     * Initialize the provider
     */
    async initialize() {
        try {
            if (!this.isConfigured()) {
                return {
                    success: false,
                    provider: this.type,
                    error: 'Provider not properly configured',
                };
            }
            await this.doInitialize();
            this.initialized = true;
            const models = await this.getAvailableModels();
            return {
                success: true,
                provider: this.type,
                availableModels: models.map((m) => m.id),
            };
        }
        catch (error) {
            return {
                success: false,
                provider: this.type,
                error: error instanceof Error ? error.message : String(error),
            };
        }
    }
    /**
     * Check if the provider is properly configured
     */
    isConfigured() {
        const metadata = getProviderMetadata(this.type);
        if (!metadata)
            return false;
        // Check for required API key if needed
        if (metadata.defaultAuthMethod === 'api-key' &&
            !this.config.apiKey &&
            !this.getApiKeyFromEnv()) {
            return false;
        }
        return true;
    }
    /**
     * Get API key from environment variables
     */
    getApiKeyFromEnv() {
        const metadata = getProviderMetadata(this.type);
        if (!metadata?.envVars.apiKey)
            return undefined;
        for (const envVar of metadata.envVars.apiKey) {
            const value = process.env[envVar];
            if (value)
                return value;
        }
        return undefined;
    }
    /**
     * Get base URL from environment variables
     */
    getBaseUrlFromEnv() {
        const metadata = getProviderMetadata(this.type);
        if (!metadata?.envVars.baseUrl)
            return undefined;
        for (const envVar of metadata.envVars.baseUrl) {
            const value = process.env[envVar];
            if (value)
                return value;
        }
        return undefined;
    }
    /**
     * Get effective API key (from config or environment)
     */
    getEffectiveApiKey() {
        return this.config.apiKey || this.getApiKeyFromEnv();
    }
    /**
     * Get effective base URL (from config or environment)
     */
    getEffectiveBaseUrl() {
        return this.config.baseUrl || this.getBaseUrlFromEnv();
    }
    /**
     * Check provider health
     */
    async checkHealth() {
        const startTime = Date.now();
        try {
            await this.doHealthCheck();
            const latency = Date.now() - startTime;
            return {
                provider: this.type,
                isHealthy: true,
                lastChecked: new Date(),
                latency,
            };
        }
        catch (error) {
            return {
                provider: this.type,
                isHealthy: false,
                lastChecked: new Date(),
                error: error instanceof Error ? error.message : String(error),
            };
        }
    }
    /**
     * Get a specific model by ID
     */
    async getModel(modelId) {
        const models = await this.getAvailableModels();
        return models.find((m) => m.id === modelId) || null;
    }
    /**
     * Validate model ID for this provider
     */
    async validateModel(modelId) {
        const model = await this.getModel(modelId);
        if (!model) {
            throw new Error(`Model '${modelId}' not found in ${this.name} provider`);
        }
    }
}
//# sourceMappingURL=baseProvider.js.map