"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.HybridAIClient = void 0;
const AICore_1 = require("../core/ai/AICore");
/**
 * HybridAIClient (Legacy Wrapper)
 *
 * This class is now a backward-compatibility layer that delegates to AICore.
 * New code should use AICore.getInstance().getBestClient() directly.
 */
class HybridAIClient {
    constructor() {
        // AICore is likely already initialized by the command entry point
        try {
            AICore_1.AICore.getInstance();
        }
        catch (e) {
            AICore_1.AICore.getInstance({
                workingDirectory: process.cwd(),
                fallbackEnabled: true
            });
        }
    }
    get client() {
        return AICore_1.AICore.getInstance().getBestClient();
    }
    async getActiveProviderName() {
        const client = this.client;
        return client.providerName || "hybrid";
    }
    async getActiveTextModelName() {
        return "unified-model";
    }
    async generateText(prompt, options = {}) {
        const text = await this.client.generateText(prompt, options);
        return { text, provider: await this.getActiveProviderName() };
    }
    async generateComponent(prompt, options = {}) {
        const result = await this.client.generateComponent(prompt, undefined, options);
        const { code, explanation } = this.parseAIResponse(result);
        return { code, provider: await this.getActiveProviderName(), explanation };
    }
    async generateComponentRefinement(componentCode, prompt, options = {}) {
        const result = await this.client.generateComponentRefinement(componentCode, prompt, undefined, options);
        const { code, explanation } = this.parseAIResponse(result);
        return { code, provider: await this.getActiveProviderName(), explanation };
    }
    parseAIResponse(response) {
        // Reuse the parsing logic from old client for compatibility
        const codeBlockPatterns = [
            /```(?:tsx|jsx|ts|js|typescript|javascript)?\s*\n([\s\S]*?)```/g,
            /```(?:tsx|jsx|ts|js|typescript|javascript)?\s*([\s\S]*?)```/g,
            /```\s*\n([\s\S]*?)```/g,
            /```\s*([\s\S]*?)```/g,
        ];
        for (const pattern of codeBlockPatterns) {
            const matches = [...response.matchAll(pattern)];
            if (matches.length > 0) {
                const longestMatch = matches.reduce((longest, match) => (match[1]?.length || 0) > (longest[1]?.length || 0) ? match : longest);
                const code = longestMatch[1]?.trim() || "";
                const explanation = response.replace(longestMatch[0], "").trim();
                return { code, explanation: explanation || undefined };
            }
        }
        return { code: response.trim() };
    }
}
exports.HybridAIClient = HybridAIClient;
//# sourceMappingURL=hybridAIClient.js.map