const isRecord = (value) => typeof value === 'object' && value !== null && !Array.isArray(value);
export function normalizeResponsePayload(payload, config) {
    const normalized = { ...payload };
    if (isRecord(normalized.usage)) {
        normalized.usage = normalizeUsageFields(normalized.usage);
    }
    if (typeof normalized.created_at === 'number') {
        normalized.created = normalized.created_at;
        delete normalized.created_at;
    }
    if (Array.isArray(normalized.choices)) {
        normalized.choices = normalizeChoices(normalized.choices, config);
    }
    return normalized;
}
function normalizeUsageFields(usage) {
    const normalizedUsage = { ...usage };
    const fieldMappings = {
        input_tokens: 'prompt_tokens',
        output_tokens: 'completion_tokens',
        total_input_tokens: 'prompt_tokens',
        total_output_tokens: 'completion_tokens'
    };
    for (const [glmField, standardField] of Object.entries(fieldMappings)) {
        const value = normalizedUsage[glmField];
        if (typeof value === 'number') {
            normalizedUsage[standardField] = value;
            delete normalizedUsage[glmField];
        }
    }
    const promptTokens = typeof normalizedUsage.prompt_tokens === 'number' ? normalizedUsage.prompt_tokens : 0;
    const completionTokens = typeof normalizedUsage.completion_tokens === 'number'
        ? normalizedUsage.completion_tokens
        : 0;
    normalizedUsage.prompt_tokens = promptTokens;
    normalizedUsage.completion_tokens = completionTokens;
    if (typeof normalizedUsage.total_tokens !== 'number') {
        normalizedUsage.total_tokens = promptTokens + completionTokens;
    }
    return normalizedUsage;
}
function normalizeChoices(choices, config) {
    const map = config?.finishReasonMap || {};
    return choices.map((choice, idx) => {
        if (!isRecord(choice)) {
            return choice;
        }
        const normalizedChoice = {
            index: typeof choice.index === 'number' ? choice.index : idx,
            ...choice
        };
        if (typeof normalizedChoice.finish_reason === 'string') {
            normalizedChoice.finish_reason = map[normalizedChoice.finish_reason] ?? normalizedChoice.finish_reason;
        }
        if (isRecord(normalizedChoice.message)) {
            normalizedChoice.message = normalizeChoiceMessage(normalizedChoice.message);
        }
        return normalizedChoice;
    });
}
function normalizeChoiceMessage(message) {
    const normalizedMessage = { ...message };
    if (normalizedMessage.content !== undefined && typeof normalizedMessage.content !== 'string') {
        normalizedMessage.content = normalizeMessageContent(normalizedMessage.content);
    }
    if (Array.isArray(normalizedMessage.tool_calls)) {
        normalizedMessage.tool_calls = normalizeToolCalls(normalizedMessage.tool_calls);
    }
    return normalizedMessage;
}
function normalizeMessageContent(content) {
    if (typeof content === 'string') {
        return content;
    }
    if (Array.isArray(content)) {
        return content.map(item => (typeof item === 'string' ? item : JSON.stringify(item))).join('');
    }
    if (isRecord(content)) {
        return JSON.stringify(content);
    }
    return String(content ?? '');
}
function normalizeToolCalls(toolCalls) {
    return toolCalls.map(toolCall => {
        if (!isRecord(toolCall)) {
            return toolCall;
        }
        const normalizedToolCall = { ...toolCall };
        const func = normalizedToolCall.function;
        if (isRecord(func) && func.arguments !== undefined) {
            func.arguments = normalizeToolArguments(func.arguments);
            normalizedToolCall.function = func;
        }
        return normalizedToolCall;
    });
}
function normalizeToolArguments(args) {
    let normalized = '';
    if (typeof args === 'string') {
        normalized = args;
    }
    else {
        try {
            normalized = JSON.stringify(args ?? {});
        }
        catch {
            normalized = String(args ?? '');
        }
    }
    const trimmed = normalized.trim();
    if (!trimmed.length) {
        return '';
    }
    try {
        const parsed = JSON.parse(trimmed);
        return JSON.stringify(parsed);
    }
    catch {
        return trimmed;
    }
}
