/**
 * Browser-friendly packages only.
 * (No Node.js built-ins like fs, path, dotenv, etc.)
 * Works in both Node.js and modern browsers.
 */
import { provider2LLMAgent, provider2TTSAgent, provider2ImageAgent, provider2MovieAgent, provider2SoundEffectAgent, provider2LipSyncAgent, } from "../types/provider2agent.js";
export const llmPair = (_llm, _model) => {
    const llmKey = _llm ?? "openai";
    const agent = provider2LLMAgent[llmKey]?.agentName ?? provider2LLMAgent.openai.agentName;
    const model = _model ?? provider2LLMAgent[llmKey]?.defaultModel ?? provider2LLMAgent.openai.defaultModel;
    const max_tokens = provider2LLMAgent[llmKey]?.max_tokens ?? provider2LLMAgent.openai.max_tokens;
    return { agent, model, max_tokens };
};
export const chunkArray = (array, size = 3) => {
    const chunks = [];
    const copy = [...array];
    while (copy.length)
        chunks.push(copy.splice(0, size));
    return chunks;
};
export const isHttp = (fileOrUrl) => {
    return /^https?:\/\//.test(fileOrUrl);
};
export const localizedText = (beat, multiLingualData, targetLang, defaultLang) => {
    if (targetLang === defaultLang) {
        return beat.text;
    }
    if (targetLang && multiLingualData?.multiLingualTexts?.[targetLang]?.text) {
        return multiLingualData.multiLingualTexts[targetLang].text;
    }
    return beat.text;
};
export function processLineBreaks(text) {
    return text.replace(/\n/g, "<br>");
}
export function userAssert(condition, message) {
    if (!condition) {
        throw new Error(message);
    }
}
export const settings2GraphAIConfig = (settings, env) => {
    const getKey = (prefix, key) => {
        return settings?.[`${prefix}_${key}`] ?? settings?.[key] ?? env?.[`${prefix}_${key}`] ?? env?.[key];
    };
    const addProviderConfigs = (config, providers, prefix) => {
        Object.entries(providers).forEach(([__provider, info]) => {
            if (info.agentName === "mediaMockAgent" || !info.keyName)
                return;
            const apiKeyName = info.apiKeyNameOverride || info.keyName;
            config[info.agentName] = {
                apiKey: getKey(prefix, apiKeyName),
            };
            if (info.baseURLKeyName) {
                config[info.agentName].baseURL = getKey(prefix, info.baseURLKeyName);
            }
            if (info.apiVersionKeyName) {
                config[info.agentName].apiVersion = getKey(prefix, info.apiVersionKeyName);
            }
        });
    };
    const config = {};
    addProviderConfigs(config, provider2LLMAgent, "LLM");
    addProviderConfigs(config, provider2TTSAgent, "TTS");
    addProviderConfigs(config, provider2ImageAgent, "IMAGE");
    addProviderConfigs(config, provider2MovieAgent, "MOVIE");
    addProviderConfigs(config, provider2SoundEffectAgent, "SOUND_EFFECT");
    addProviderConfigs(config, provider2LipSyncAgent, "LIPSYNC");
    // TODO
    // browserlessAgent
    // geminiAgent, groqAgent for tool
    // TAVILY_API_KEY ( for deep research)
    return deepClean(config) ?? {};
};
export const deepClean = (input) => {
    if (input === null || input === undefined || input === "") {
        return undefined;
    }
    if (Array.isArray(input)) {
        const cleanedArray = input.map(deepClean).filter((v) => v !== undefined);
        return cleanedArray.length > 0 ? cleanedArray : undefined;
    }
    if (typeof input === "object") {
        const result = {};
        for (const [key, value] of Object.entries(input)) {
            const cleaned = deepClean(value);
            if (cleaned !== undefined) {
                result[key] = cleaned;
            }
        }
        return Object.keys(result).length > 0 ? result : undefined;
    }
    return input;
};
export const beatId = (id, index) => {
    const key = id ?? `__index__${index}`;
    return key;
};
export const multiLingualObjectToArray = (multiLingual, beats) => {
    return beats.map((beat, index) => {
        const key = beatId(beat?.id, index);
        if (multiLingual?.[key]) {
            return multiLingual[key];
        }
        return { multiLingualTexts: {} };
    });
};
export const getAspectRatio = (canvasSize, ASPECT_RATIOS) => {
    const target = canvasSize.width / canvasSize.height;
    return ASPECT_RATIOS.reduce((best, ratio) => {
        const [w, h] = ratio.split(":").map(Number);
        const r = w / h;
        const diff = Math.abs(target - r);
        return diff < best.diff ? { ratio, diff } : best;
    }, { ratio: ASPECT_RATIOS[0], diff: Infinity }).ratio;
};
