import { buildNativeReqOutboundCompatAdapterContext } from '../../../compat/native-adapter-context.js';
import { resolveCompatProfileWithNative } from '../../../../../../router/virtual-router/engine-selection/native-hub-pipeline-edge-stage-semantics.js';
import { runReqOutboundStage3CompatWithNative, runRespInboundStage3CompatWithNative } from '../../../../../../router/virtual-router/engine-selection/native-hub-pipeline-req-outbound-semantics.js';
const RATE_LIMIT_ERROR = 'ERR_COMPAT_RATE_LIMIT_DETECTED';
function pickCompatProfile(adapterContext) {
    const candidate = adapterContext.compatibilityProfile;
    const explicit = typeof candidate === 'string' && candidate.trim() ? candidate.trim() : undefined;
    return resolveCompatProfileWithNative(adapterContext, explicit);
}
export async function runReqOutboundStage3Compat(options) {
    const profile = pickCompatProfile(options.adapterContext);
    const nativeCompat = runReqOutboundStage3CompatWithNative({
        payload: options.payload,
        adapterContext: buildNativeReqOutboundCompatAdapterContext(options.adapterContext),
        explicitProfile: profile
    });
    const effectiveProfile = nativeCompat.appliedProfile ?? profile;
    const appliedProfile = nativeCompat.appliedProfile;
    options.stageRecorder?.record('chat_process.req.stage8.outbound.compat', {
        applied: Boolean(appliedProfile),
        profile: appliedProfile || effectiveProfile || 'passthrough'
    });
    return nativeCompat.payload;
}
export function runRespInboundStageCompatResponse(options) {
    const profile = pickCompatProfile(options.adapterContext);
    const nativeCompat = runRespInboundStage3CompatWithNative({
        payload: options.payload,
        adapterContext: buildNativeReqOutboundCompatAdapterContext(options.adapterContext),
        explicitProfile: profile
    });
    const effectiveProfile = nativeCompat.appliedProfile ?? profile;
    const appliedProfile = nativeCompat.appliedProfile;
    if (nativeCompat.nativeApplied === true && nativeCompat.rateLimitDetected === true) {
        const err = new Error('Provider returned rate limit notice');
        err.code = RATE_LIMIT_ERROR;
        err.statusCode = 429;
        throw err;
    }
    options.stageRecorder?.record('chat_process.resp.stage3.compat', {
        applied: Boolean(appliedProfile),
        profile: appliedProfile || effectiveProfile || 'passthrough'
    });
    return nativeCompat.payload;
}
