import { jsxs as _jsxs, jsx as _jsx, Fragment as _Fragment } from "react/jsx-runtime";
/**
 * ============================================================
 *  src/ui/StatusLine.tsx —— 底部状态栏
 * ------------------------------------------------------------
 *  显示：
 *    cwd ~/proj · {provider}/{model} · tokens 12.3K / 200K · 距压缩 7.7K
 *  接近压缩阈值时 tokens / 距压缩 变黄；超过阈值时变红。
 * ============================================================
 */
import React from 'react';
import { Box, Text } from 'ink';
import { homedir } from 'node:os';
import { sep } from 'node:path';
import { getWorkingDir } from '../bootstrap/workingDir.js';
import { useTokenUsage } from './hooks/useTokenUsage.js';
/**
 * React.memo 包装。
 *
 * props: provider 引用启动后稳定；history 引用变化（useMemo slice）但内容变化
 * 才需要重算 useTokenUsage —— React.memo 浅比较 history 引用就够了（slice 后引用
 * 变了说明真有新消息）；pluginProgress 仅插件循环时更新。
 *
 * 流式 streamingText 高频更新不影响 history 引用，StatusLine 不会被带着 rerender。
 */
export const StatusLine = React.memo(function StatusLine({ provider, history, pluginProgress, }) {
    const usage = useTokenUsage(history, provider);
    const ratio = usage.tokens / usage.threshold;
    const color = ratio >= 1 ? 'red' : ratio >= 0.7 ? 'yellow' : 'green';
    const cwdDisplay = shortenPath(getWorkingDir());
    return (_jsxs(Box, { children: [pluginProgress && (_jsxs(_Fragment, { children: [_jsxs(Text, { color: "magenta", children: ["\uD83D\uDD04 ", pluginProgress.pluginId, " "] }), _jsxs(Text, { color: "magenta", children: [pluginProgress.current, "/", pluginProgress.max] }), _jsx(Text, { color: "gray", children: ' · ' })] })), _jsx(Text, { color: "cyan", children: "cwd " }), _jsx(Text, { color: "gray", children: cwdDisplay }), _jsx(Text, { color: "gray", children: ' · ' }), _jsxs(Text, { color: "gray", children: [provider.name, "/", provider.model] }), _jsx(Text, { color: "gray", children: ' · ' }), _jsxs(Text, { color: color, children: ["tokens ", fmt(usage.tokens), " / ", fmt(usage.contextWindow)] }), _jsx(Text, { color: "gray", children: ' · ' }), _jsx(Text, { color: color, children: usage.toThreshold > 0 ? `距压缩 ${fmt(usage.toThreshold)}` : `已超阈值（下轮压缩）` })] }));
});
/** 把整数格式化为 1.2K / 12.3K / 1.5M */
function fmt(n) {
    if (n < 1000)
        return `${n}`;
    if (n < 1_000_000)
        return `${(n / 1000).toFixed(1)}K`;
    return `${(n / 1_000_000).toFixed(2)}M`;
}
/**
 * 把绝对路径精简成适合状态栏显示的形式：
 *   - home 下 → 以 ~ 开头
 *   - 超过 40 字符 → 保留首段 + … + 末两段
 */
export function shortenPath(abs) {
    const home = homedir();
    let p = abs;
    if (home && (p === home || p.startsWith(home + sep))) {
        p = '~' + p.slice(home.length);
    }
    if (p.length <= 40)
        return p;
    const parts = p.split(/[\\/]/).filter(Boolean);
    if (parts.length <= 3)
        return p;
    const head = p.startsWith('~') ? '~' : parts[0];
    const tail = parts.slice(-2).join(sep);
    return `${head}${sep}…${sep}${tail}`;
}
