export function toAcpModelResponse(response) {
    if (hasOwnEvents(response)) {
        return response;
    }
    return {
        events: legacyResponseToEvents(response)
    };
}
function hasOwnEvents(response) {
    return Object.hasOwn(response, "events");
}
export async function collectProviderEvents(response) {
    const events = [];
    for await (const event of response.events) {
        events.push(event);
    }
    return events;
}
async function* legacyResponseToEvents(response) {
    const message = response.message;
    const content = typeof message?.content === "string"
        ? message.content
        : typeof response.content === "string"
            ? response.content
            : undefined;
    const reasoning = normalizeNonEmptyString(message?.reasoning_content) ??
        normalizeNonEmptyString(message?.reasoning);
    const toolCalls = [
        ...(message?.toolCalls ?? response.toolCalls ?? []),
        ...fromOpenAiToolCalls(message?.tool_calls)
    ];
    if (response.deltas) {
        for await (const chunk of response.deltas) {
            if (typeof chunk !== "string" || chunk.length === 0) {
                continue;
            }
            yield {
                type: "text",
                text: chunk
            };
        }
    }
    else if (typeof content === "string" && content.length > 0) {
        yield {
            type: "text",
            text: content
        };
    }
    if (reasoning) {
        yield {
            type: "thinking",
            text: reasoning
        };
    }
    for (const [index, toolCall] of toolCalls.entries()) {
        const id = normalizeNonEmptyString(toolCall.intentId) ??
            normalizeNonEmptyString(toolCall.id) ??
            `intent-${index + 1}`;
        const name = normalizeNonEmptyString(toolCall.tool) ?? normalizeNonEmptyString(toolCall.name);
        if (!name) {
            continue;
        }
        const normalizedArguments = normalizeToolArguments(toolCall.args ?? toolCall.arguments);
        if (normalizedArguments.raw !== undefined) {
            yield {
                type: "tool_use_delta",
                id,
                name,
                argsDelta: normalizedArguments.raw
            };
        }
        yield {
            type: "tool_use_complete",
            id,
            name,
            args: normalizedArguments.value
        };
    }
    if (response.usage) {
        yield {
            type: "usage",
            inputTokens: response.usage.inputTokens,
            outputTokens: response.usage.outputTokens,
            cachedTokens: response.usage.cachedTokens,
            cacheCreationTokens: response.usage.cacheCreationTokens
        };
    }
    yield {
        type: "stop",
        reason: toolCalls.length > 0 ? "tool_use" : "end_turn"
    };
}
function fromOpenAiToolCalls(toolCalls) {
    if (!toolCalls) {
        return [];
    }
    return toolCalls.map((toolCall) => ({
        id: toolCall.id,
        name: toolCall.function?.name,
        arguments: toolCall.function?.arguments
    }));
}
function normalizeToolArguments(value) {
    if (typeof value !== "string") {
        return { value };
    }
    try {
        return {
            value: JSON.parse(value),
            raw: value
        };
    }
    catch {
        return {
            value,
            raw: value
        };
    }
}
function normalizeNonEmptyString(value) {
    if (typeof value !== "string") {
        return undefined;
    }
    const normalized = value.trim();
    return normalized.length > 0 ? normalized : undefined;
}
