import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-runtime";
import { Box, Text } from "ink";
import { Divider } from "./design/Divider.js";
import { PromptInputModeIndicator } from "./PromptInputModeIndicator.js";
import { PromptInputFooterSuggestions } from "./PromptInputFooterSuggestions.js";
import { Spinner, spinnerLabel } from "./Spinner.js";
import TextInput from "./TextInput.js";
import { isChineseUi } from "../services/ui/languageService.js";
export function Composer(props) {
    const model = composerLayoutModel(props);
    return (_jsxs(Box, { flexDirection: "column", children: [_jsx(PromptInputFooterSuggestions, { suggestions: props.suggestions, selectedSuggestion: props.selectedSuggestion, width: props.width }), model.hint ? (_jsx(Box, { paddingX: 1, children: _jsx(Text, { color: "gray", wrap: "truncate", children: model.hint }) })) : null, _jsx(Divider, { width: props.width, tone: props.busy ? "warning" : "brand" }), _jsxs(Box, { paddingX: 1, minHeight: 1, width: props.width, children: [_jsx(PromptInputModeIndicator, { mode: "chat", busy: props.busy }), _jsx(TextInput, { value: props.value, cursor: props.cursor, width: model.promptWidth, placeholder: model.placeholder }), props.busy && (_jsxs(_Fragment, { children: [_jsx(Text, { children: " " }), _jsx(Spinner, { mode: model.spinnerMode, label: model.spinnerLabel, detail: model.spinnerDetail, width: 28, inline: true })] }))] })] }));
}
export function composerLayoutModel(input) {
    const zh = isChineseUi(input.language);
    const spinnerMode = activitySpinnerMode(input.activity);
    const activityLabelText = activitySpinnerLabel(input.activity, zh);
    const spinnerDetail = busyDetail(input.queuedCount, input.activity, input.activityNowMs, zh);
    const busyLabel = input.busy
        ? ` - ${spinnerLabel(spinnerMode, activityLabelText, spinnerDetail, 28)}`
        : "";
    return {
        busyLabel,
        promptWidth: Math.max(8, input.width - 6 - busyLabel.length),
        hint: input.activePromptHint ?? "",
        placeholder: input.activePromptHint ? (zh ? "在这里输入，或使用上方快捷键" : "Type here or use shortcuts above") : "",
        spinnerMode,
        spinnerLabel: activityLabelText,
        spinnerDetail,
    };
}
function busyDetail(queuedCount, activity, activityNowMs, zh) {
    const parts = [];
    if (activity?.detail)
        parts.push(compact(activity.detail.replace(/\s+/g, " "), 18));
    if (activity) {
        const idleSeconds = Math.max(0, Math.floor(((activityNowMs ?? Date.now()) - activity.updatedAtMs) / 1000));
        if (idleSeconds >= 8)
            parts.push(zh ? `静默${idleSeconds}s` : `silent ${idleSeconds}s`);
    }
    if (queuedCount > 0)
        parts.push(`queue ${queuedCount}`);
    return parts.join(" ");
}
function activitySpinnerMode(activity) {
    if (!activity)
        return "working";
    if (activity.phase === "planning" || activity.phase === "classifying" || activity.phase === "cache_guard") {
        return "thinking";
    }
    if (activity.phase === "tool" || activity.phase === "validating")
        return "tool";
    if (activity.phase === "waiting_user")
        return "queued";
    return "working";
}
function activitySpinnerLabel(activity, zh) {
    if (!activity)
        return undefined;
    if (!zh) {
        return {
            starting: "starting",
            command: "command",
            classifying: "classifying",
            cache_guard: "cache",
            planning: "thinking",
            chatting: "replying",
            tool: "tool",
            validating: "validating",
            waiting_user: "waiting",
            finishing: "finishing",
        }[activity.phase];
    }
    return {
        starting: "启动中",
        command: "命令中",
        classifying: "分类中",
        cache_guard: "缓存检查",
        planning: "思考中",
        chatting: "回复中",
        tool: "工具中",
        validating: "验证中",
        waiting_user: "等你确认",
        finishing: "收尾中",
    }[activity.phase];
}
function compact(value, max) {
    return value.length <= max ? value : `${value.slice(0, max - 3)}...`;
}
