import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { Box, Text } from "ink";
import { CtrlOToExpand } from "./CtrlOToExpand.js";
import { MessageResponse } from "./MessageResponse.js";
const MAX_API_ERROR_CHARS = 1000;
export function SystemAPIErrorMessage(props) {
    const model = systemAPIErrorMessageModel(props);
    return (_jsx(MessageResponse, { tone: "error", children: _jsxs(Box, { flexDirection: "column", children: [_jsx(Text, { color: "red", children: model.title }), _jsx(Text, { color: "red", children: model.body }), model.truncated && _jsx(CtrlOToExpand, {}), model.retry && _jsx(Text, { color: "gray", children: model.retry })] }) }));
}
export function systemAPIErrorMessageModel(input) {
    const formatted = formatAPIError(input.error);
    const truncated = !input.verbose && formatted.length > MAX_API_ERROR_CHARS;
    return {
        title: "DeepSeek API error",
        body: truncated ? `${formatted.slice(0, MAX_API_ERROR_CHARS)}...` : formatted,
        truncated,
        retry: retryText(input.retryAttempt, input.retryInMs, input.maxRetries),
    };
}
export function formatAPIError(error) {
    if (error instanceof Error)
        return error.message || error.name;
    if (typeof error === "string")
        return error.trim();
    try {
        return JSON.stringify(error);
    }
    catch {
        return String(error);
    }
}
function retryText(retryAttempt, retryInMs, maxRetries) {
    if (!retryAttempt || !retryInMs || !maxRetries)
        return null;
    const seconds = Math.max(0, Math.round(retryInMs / 1000));
    const unit = seconds === 1 ? "second" : "seconds";
    return `retrying in ${seconds} ${unit} (attempt ${retryAttempt}/${maxRetries})`;
}
