import { normalizeChatRequest } from '../index.js';
import { createSnapshotWriter } from '../snapshot-utils.js';
import { buildGovernedFilterPayloadWithNativeFallback } from '../../router/virtual-router/engine-selection/native-chat-request-filter-semantics.js';
import { pruneChatRequestPayloadWithNative } from '../../router/virtual-router/engine-selection/native-hub-pipeline-req-inbound-semantics.js';
/**
 * Native-primary Chat request filters.
 *
 * Historical TS FilterEngine implementation archived at:
 * - src/conversion/shared/archive/chat-request-filters.ts
 */
export async function runStandardChatRequestFilters(chatRequest, profile, context) {
    const existingMetadata = context.metadata ?? {};
    if (!context.metadata) {
        context.metadata = existingMetadata;
    }
    const inboundStreamFromContext = typeof existingMetadata.inboundStream === 'boolean' ? existingMetadata.inboundStream : undefined;
    const inboundStreamDetected = chatRequest && typeof chatRequest === 'object' && chatRequest.stream === true ? true : undefined;
    const normalizedInboundStream = inboundStreamFromContext ?? inboundStreamDetected;
    if (typeof normalizedInboundStream === 'boolean') {
        existingMetadata.inboundStream = normalizedInboundStream;
    }
    const requestId = context.requestId ?? `req_${Date.now()}`;
    const endpoint = context.entryEndpoint || context.endpoint || '/v1/chat/completions';
    const snapshot = createSnapshotWriter({
        requestId,
        endpoint,
        folderHint: 'openai-chat'
    });
    const snapshotStage = (stage, payload) => {
        if (!snapshot)
            return;
        snapshot(stage, payload);
    };
    snapshotStage('req_process_filters_input', chatRequest);
    const incomingProtocol = (profile.incomingProtocol || '').toLowerCase();
    const entryEndpointLower = endpoint.toLowerCase();
    const originalToolCount = chatRequest && typeof chatRequest === 'object' && Array.isArray(chatRequest.tools)
        ? chatRequest.tools.length
        : 0;
    const isAnthropicProfile = incomingProtocol === 'anthropic-messages' ||
        entryEndpointLower.includes('/v1/messages');
    const skipAutoToolInjection = isAnthropicProfile && originalToolCount === 0;
    const nativeGovernedPayload = buildGovernedFilterPayloadWithNativeFallback(chatRequest);
    if (skipAutoToolInjection && nativeGovernedPayload && typeof nativeGovernedPayload === 'object') {
        if (!Array.isArray(nativeGovernedPayload.tools)) {
            nativeGovernedPayload.tools = [];
        }
        nativeGovernedPayload.__rcc_disable_mcp_tools = true;
    }
    snapshotStage('req_process_filters_native_payload', nativeGovernedPayload);
    let normalized = normalizeChatRequest(nativeGovernedPayload);
    snapshotStage('req_process_filters_normalized', normalized);
    if (skipAutoToolInjection && normalized && typeof normalized === 'object') {
        if (!Array.isArray(normalized.tools)) {
            normalized.tools = [];
        }
        normalized.__rcc_disable_mcp_tools = true;
    }
    const preserveStreamField = profile.incomingProtocol === 'openai-chat' && profile.outgoingProtocol === 'openai-chat';
    const pruned = pruneChatRequestPayloadWithNative(normalized, preserveStreamField);
    snapshotStage('req_process_filters_output', pruned);
    return pruned;
}
