"use strict";
/**
 * AI Service for BFUI Using Native AI Plugin System
 *
 * This service provides AI capabilities to BFUI components using the
 * Native AI plugin system. It replaces the previous direct SDK integration
 * with a more flexible and extensible plugin-based approach.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.nativeAiService = void 0;
const index_js_1 = require("./native-ai/index.js");
// Ensure the plugin manager is initialized
async function ensureInitialized() {
    if (!index_js_1.pluginManager.isInitialized()) {
        await index_js_1.pluginManager.initialize();
    }
}
/**
 * The AI service for BFUI using the Native AI plugin system
 */
exports.nativeAiService = {
    /**
     * Execute a chat completion request
     */
    chat: async (chatRequest, options) => {
        try {
            await ensureInitialized();
            // Get provider adapter for the specified provider or default
            const adapter = new index_js_1.ProviderAdapter(chatRequest.provider || '');
            // Execute chat request
            return await adapter.chat(chatRequest, options);
        }
        catch (error) {
            // Rethrow with consistent error format
            throw {
                code: error.code || 'unknown_error',
                message: error.message || 'An unexpected error occurred',
                details: error.details || error
            };
        }
    },
    /**
     * Generate embeddings for text
     */
    embeddings: async (texts, model, providerId) => {
        try {
            await ensureInitialized();
            // Get provider adapter
            const adapter = new index_js_1.ProviderAdapter(providerId || '');
            // Generate embeddings
            return await adapter.embeddings(texts, model);
        }
        catch (error) {
            throw {
                code: error.code || 'embedding_error',
                message: error.message || 'Failed to generate embeddings',
                details: error.details || error
            };
        }
    },
    /**
     * Generate RFP content using the BFUI adapter
     */
    generateRFPContent: async (rfpDocument, requirements, providerId) => {
        try {
            await ensureInitialized();
            // Create BFUI adapter
            const adapter = new index_js_1.BFUIAdapter(providerId);
            // Generate RFP content
            return await adapter.generateRFPContent(rfpDocument, requirements);
        }
        catch (error) {
            throw {
                code: error.code || 'rfp_generation_error',
                message: error.message || 'Failed to generate RFP content',
                details: error.details || error
            };
        }
    },
    /**
     * Analyze document using the BFUI adapter
     */
    analyzeDocument: async (document, options) => {
        try {
            await ensureInitialized();
            // Create BFUI adapter
            const adapter = new index_js_1.BFUIAdapter(options?.providerId);
            // Analyze document
            return await adapter.analyzeDocument(document, {
                detailed: options?.detailed,
                categories: options?.categories
            });
        }
        catch (error) {
            throw {
                code: error.code || 'document_analysis_error',
                message: error.message || 'Failed to analyze document',
                details: error.details || error
            };
        }
    },
    /**
     * Generate a compliance report
     */
    generateComplianceReport: async (requirements, implementationDetails, providerId) => {
        try {
            await ensureInitialized();
            // Create BFUI adapter
            const adapter = new index_js_1.BFUIAdapter(providerId);
            // Generate compliance report
            return await adapter.generateComplianceReport(requirements, implementationDetails);
        }
        catch (error) {
            throw {
                code: error.code || 'compliance_report_error',
                message: error.message || 'Failed to generate compliance report',
                details: error.details || error
            };
        }
    },
    /**
     * Register a plugin
     */
    registerPlugin: async (plugin) => {
        index_js_1.pluginManager.registerPlugin(plugin);
        // Initialize if manager is already initialized
        if (index_js_1.pluginManager.isInitialized() && !plugin.isInitialized()) {
            // This will automatically initialize the plugin with the right context
            await ensureInitialized();
        }
    },
    /**
     * Get all registered plugins
     */
    getPlugins: () => {
        return index_js_1.pluginManager.getAllPlugins();
    },
    /**
     * Get plugin manager instance
     */
    getPluginManager: () => {
        return index_js_1.pluginManager;
    },
    /**
     * Initialize the service and all plugins
     */
    initialize: async () => {
        return ensureInitialized();
    },
    /**
     * Shut down the service and all plugins
     */
    shutdown: async () => {
        if (index_js_1.pluginManager.isInitialized()) {
            await index_js_1.pluginManager.shutdown();
        }
    }
};
exports.default = exports.nativeAiService;
//# sourceMappingURL=native-ai-service.js.map