"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.findProvider = findProvider;
exports.findProviders = findProviders;
exports.findTtsProvider = findTtsProvider;
const drizzle_orm_1 = require("drizzle-orm");
const constants_1 = require("../../constants");
const database_1 = require("../../database");
const tables_1 = require("../../database/tables");
const logger_1 = __importDefault(require("../../logger"));
/**
 * Generic function to find a provider configuration from the database
 * @param workspaceId - Workspace ID to search for
 * @param providerType - Type of provider to find (e.g., PROVIDER_TYPE.TTS, PROVIDER_TYPE.STT)
 * @returns Provider configuration or null if not found
 */
async function findProvider(workspaceId, providerType) {
    try {
        const db = await (0, database_1.getDb)();
        const providerConfigs = await db
            .select()
            .from(tables_1.providersConfigTable)
            .where((0, drizzle_orm_1.and)((0, drizzle_orm_1.eq)(tables_1.providersConfigTable.workspaceId, workspaceId), (0, drizzle_orm_1.eq)(tables_1.providersConfigTable.providerType, providerType), (0, drizzle_orm_1.eq)(tables_1.providersConfigTable.status, 1)))
            .orderBy((0, drizzle_orm_1.desc)(tables_1.providersConfigTable.isDefault)) // Prefer default providers (true first)
            .limit(1);
        if (providerConfigs.length > 0) {
            const providerConfig = providerConfigs[0];
            logger_1.default.debug(`Found ${providerType} provider: ${providerConfig.providerName} for workspace ${workspaceId}`);
            return providerConfig;
        }
        logger_1.default.debug(`No ${providerType} provider found for workspace ${workspaceId}, will use defaults`);
        return null;
    }
    catch (error) {
        logger_1.default.warn(`Failed to fetch ${providerType} provider configuration: ${error}`);
        return null;
    }
}
/**
 * Find multiple provider configurations for a workspace
 * @param workspaceId - Workspace ID to search for
 * @param providerTypes - Array of provider types to find
 * @returns Map of providerType -> ProviderConfigResult
 */
async function findProviders(workspaceId, providerTypes) {
    const providerMap = new Map();
    // Fetch all providers in parallel
    const providerPromises = providerTypes.map(async (providerType) => {
        const provider = await findProvider(workspaceId, providerType);
        if (provider) {
            providerMap.set(providerType, provider);
        }
    });
    await Promise.all(providerPromises);
    return providerMap;
}
/**
 * Legacy function for backward compatibility
 * @deprecated Use findProvider(workspaceId, PROVIDER_TYPE.TTS) instead
 */
async function findTtsProvider(workspaceId) {
    return findProvider(workspaceId, constants_1.PROVIDER_TYPE.TTS);
}
//# sourceMappingURL=findWorkspaceProvider.js.map