import { buildNativeReqOutboundCompatAdapterContext } from "../../hub/pipeline/compat/native-adapter-context.js";
import { runRespInboundStage3CompatWithNative } from "../../../router/virtual-router/engine-selection/native-hub-pipeline-req-outbound-semantics.js";
const PROFILE = "chat:glm";
const DEFAULT_PROVIDER_PROTOCOL = "openai-chat";
const DEFAULT_ENTRY_ENDPOINT = "/v1/chat/completions";
function buildGlmCompatContext(adapterContext) {
    const nativeContext = buildNativeReqOutboundCompatAdapterContext(adapterContext);
    return {
        ...nativeContext,
        compatibilityProfile: PROFILE,
        providerProtocol: nativeContext.providerProtocol ??
            adapterContext?.providerProtocol ??
            DEFAULT_PROVIDER_PROTOCOL,
        entryEndpoint: nativeContext.entryEndpoint ??
            adapterContext?.entryEndpoint ??
            DEFAULT_ENTRY_ENDPOINT,
    };
}
function buildGlmResponseCompatInput(payload, adapterContext) {
    return {
        payload,
        adapterContext: buildGlmCompatContext(adapterContext),
        explicitProfile: PROFILE,
    };
}
export function extractGlmToolMarkup(root, adapterContext) {
    if (!root || typeof root !== "object" || Array.isArray(root)) {
        return;
    }
    const normalized = runRespInboundStage3CompatWithNative(buildGlmResponseCompatInput(root, adapterContext)).payload;
    Object.keys(root).forEach((key) => {
        delete root[key];
    });
    Object.assign(root, normalized);
}
