import { buildNativeReqOutboundCompatAdapterContext } from '../../hub/pipeline/compat/native-adapter-context.js';
import { runReqOutboundStage3CompatWithNative } from '../../../router/virtual-router/engine-selection/native-hub-pipeline-req-outbound-semantics.js';
const PROFILE = 'chat:claude-code';
const DEFAULT_PROVIDER_PROTOCOL = 'anthropic-messages';
const DEFAULT_ENTRY_ENDPOINT = '/v1/messages';
function buildClaudeCodeConfigNode(config) {
    if (!config) {
        return undefined;
    }
    const node = {};
    if (typeof config.systemText === 'string' && config.systemText.trim()) {
        node.systemText = config.systemText.trim();
    }
    if (typeof config.preserveExistingSystemAsUserMessage === 'boolean') {
        node.preserveExistingSystemAsUserMessage = config.preserveExistingSystemAsUserMessage;
    }
    return Object.keys(node).length ? node : undefined;
}
function buildClaudeCodeCompatContext(adapterContext, config) {
    const nativeContext = buildNativeReqOutboundCompatAdapterContext(adapterContext);
    const claudeCode = buildClaudeCodeConfigNode(config);
    return {
        ...nativeContext,
        compatibilityProfile: PROFILE,
        providerProtocol: nativeContext.providerProtocol ?? adapterContext?.providerProtocol ?? DEFAULT_PROVIDER_PROTOCOL,
        entryEndpoint: nativeContext.entryEndpoint ?? adapterContext?.entryEndpoint ?? DEFAULT_ENTRY_ENDPOINT,
        ...(claudeCode ? { claudeCode } : {})
    };
}
function buildClaudeCodeCompatInput(payload, config, adapterContext) {
    return {
        payload,
        adapterContext: buildClaudeCodeCompatContext(adapterContext, config),
        explicitProfile: PROFILE
    };
}
export function applyAnthropicClaudeCodeSystemPromptCompat(payload, config, adapterContext) {
    if (!payload || typeof payload !== 'object' || Array.isArray(payload)) {
        return payload;
    }
    return runReqOutboundStage3CompatWithNative(buildClaudeCodeCompatInput(payload, config, adapterContext)).payload;
}
