import { buildNativeReqOutboundCompatAdapterContext } from '../../hub/pipeline/compat/native-adapter-context.js';
import { applyIflowToolTextFallbackWithNative } from '../../../router/virtual-router/engine-selection/native-compat-action-semantics.js';
const PROFILE = 'chat:iflow';
const DEFAULT_PROVIDER_PROTOCOL = 'openai-chat';
const DEFAULT_ENTRY_ENDPOINT = '/v1/chat/completions';
function buildIflowCompatContext(adapterContext) {
    const nativeContext = buildNativeReqOutboundCompatAdapterContext(adapterContext);
    return {
        ...nativeContext,
        compatibilityProfile: nativeContext.compatibilityProfile ??
            adapterContext?.compatibilityProfile ??
            PROFILE,
        providerProtocol: nativeContext.providerProtocol ??
            adapterContext?.providerProtocol ??
            DEFAULT_PROVIDER_PROTOCOL,
        entryEndpoint: nativeContext.entryEndpoint ??
            adapterContext?.entryEndpoint ??
            DEFAULT_ENTRY_ENDPOINT,
    };
}
export function applyIflowToolTextFallback(payload, options) {
    if (!payload || typeof payload !== 'object' || Array.isArray(payload)) {
        return payload;
    }
    const adapterContext = options?.routeId
        ? { routeId: options.routeId }
        : undefined;
    return applyIflowToolTextFallbackWithNative(payload, buildIflowCompatContext(adapterContext), Array.isArray(options?.models) ? options?.models : []);
}
