import { clearApiProviders, registerApiProvider } from "../api-registry.js";
import { AssistantMessageEventStream } from "../utils/event-stream.js";
function createLazyErrorMessage(model, error) {
    return {
        role: "assistant",
        content: [],
        api: model.api,
        provider: model.provider,
        model: model.id,
        usage: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, totalTokens: 0, cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 } },
        stopReason: "error",
        errorMessage: error instanceof Error ? error.message : String(error),
        timestamp: Date.now(),
    };
}
function forwardStream(target, source) {
    (async () => {
        for await (const event of source)
            target.push(event);
        target.end();
    })();
}
function createLazyStream(loadModule) {
    return (model, context, options) => {
        const outer = new AssistantMessageEventStream();
        loadModule()
            .then((mod) => {
            const inner = mod.stream(model, context, options);
            forwardStream(outer, inner);
        })
            .catch((error) => {
            const message = createLazyErrorMessage(model, error);
            outer.push({ type: "error", reason: "error", error: message });
            outer.end(message);
        });
        return outer;
    };
}
function createLazySimpleStream(loadModule) {
    return (model, context, options) => {
        const outer = new AssistantMessageEventStream();
        loadModule()
            .then((mod) => {
            const inner = mod.streamSimple(model, context, options);
            forwardStream(outer, inner);
        })
            .catch((error) => {
            const message = createLazyErrorMessage(model, error);
            outer.push({ type: "error", reason: "error", error: message });
            outer.end(message);
        });
        return outer;
    };
}
let anthropicProviderPromise;
function loadAnthropic() {
    anthropicProviderPromise ||= import("./anthropic.js").then((mod) => {
        const m = mod;
        return { stream: m.streamAnthropic, streamSimple: m.streamSimpleAnthropic };
    });
    return anthropicProviderPromise;
}
let openaiCompletionsPromise;
function loadOpenAICompletions() {
    openaiCompletionsPromise ||= import("./openai-completions.js").then((mod) => {
        const m = mod;
        return { stream: m.streamOpenAICompletions, streamSimple: m.streamSimpleOpenAICompletions };
    });
    return openaiCompletionsPromise;
}
// ─── Exports ──────────────────────────────────────────────────────────
export const streamAnthropic = createLazyStream(loadAnthropic);
export const streamSimpleAnthropic = createLazySimpleStream(loadAnthropic);
export const streamOpenAICompletions = createLazyStream(loadOpenAICompletions);
export const streamSimpleOpenAICompletions = createLazySimpleStream(loadOpenAICompletions);
/**
 * Register only the API providers needed by Aexol AI proxy:
 * - anthropic-messages (Claude models routed through backend)
 * - openai-completions (OpenAI-compatible models routed through backend)
 */
export function registerBuiltInApiProviders() {
    registerApiProvider({
        api: "anthropic-messages",
        stream: streamAnthropic,
        streamSimple: streamSimpleAnthropic,
    });
    registerApiProvider({
        api: "openai-completions",
        stream: streamOpenAICompletions,
        streamSimple: streamSimpleOpenAICompletions,
    });
}
export function resetApiProviders() {
    clearApiProviders();
    registerBuiltInApiProviders();
}
registerBuiltInApiProviders();
