import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { Box, Text } from "ink";
import { useAttachedRun } from "../hooks/useAttachedRun.js";
import { useCacheSummary } from "../hooks/useCacheSummary.js";
import { useGitStatus } from "../hooks/useGitStatus.js";
import { useRuns } from "../hooks/useRuns.js";
import { useTodos } from "../hooks/useTodos.js";
import { AgentWorkflowService } from "../services/agents/agentWorkflow.js";
import { AttachedRunPanel } from "./AttachedRunPanel.js";
import { CacheEfficiencyNotice } from "./CacheEfficiencyNotice.js";
import { CompactRunList } from "./CompactRunList.js";
import { GitStatus } from "./GitStatus.js";
import { MemoryPanel } from "./MemoryPanel.js";
import { PanelSection } from "./PanelSection.js";
import { PermissionStatus } from "./PermissionStatus.js";
import { ProviderStatus } from "./ProviderStatus.js";
import { TodoPanel } from "./TodoPanel.js";
import { Divider } from "./design/Divider.js";
import { StatusBadge } from "./design/StatusBadge.js";
export function SidePanel(props) {
    const runs = useRuns(props.state, 5);
    const cache = useCacheSummary(props.state);
    const attached = useAttachedRun(props.state, props.config.projectPath);
    const git = useGitStatus(props.config.projectPath);
    const todos = useTodos(props.config.projectPath);
    const width = 35;
    return (_jsxs(Box, { flexDirection: "column", width: width, paddingX: 1, children: [_jsxs(Box, { justifyContent: "space-between", children: [_jsx(Text, { color: "cyan", bold: true, children: "DeepSeekCode" }), _jsx(StatusBadge, { label: props.busy ? "running" : "idle", tone: props.busy ? "warning" : "success" })] }), _jsx(Divider, { width: width - 2, tone: "muted" }), _jsx(PanelSection, { title: "provider", children: _jsx(ProviderStatus, { config: props.config }) }), _jsx(PanelSection, { title: "project", children: _jsx(Text, { children: compact(props.config.projectPath, 30) }) }), _jsx(PanelSection, { title: "cache", children: _jsx(CacheEfficiencyNotice, { cache: cache, compact: true }) }), _jsx(PanelSection, { title: "usage", children: _jsx(UsageSummary, { lastTurn: props.lastTurnUsage, session: props.sessionUsage }) }), _jsx(PanelSection, { title: "git", children: _jsx(GitStatus, { git: git }) }), _jsx(PanelSection, { title: "permissions", children: _jsx(PermissionStatus, { ...props.permissions }) }), _jsx(PanelSection, { title: "attached run", children: _jsx(AttachedRunPanel, { run: attached.run, runId: attached.runId }) }), _jsx(PanelSection, { title: "agent team", children: _jsx(AgentWorkflowSummary, { state: props.state, projectPath: props.config.projectPath }) }), _jsx(PanelSection, { title: "todos", children: _jsx(TodoPanel, { todos: todos }) }), _jsx(PanelSection, { title: "recent runs", children: _jsx(CompactRunList, { runs: runs }) }), _jsx(PanelSection, { title: "memory", children: _jsx(MemoryPanel, { projectPath: props.config.projectPath }) })] }));
}
function AgentWorkflowSummary(props) {
    try {
        const service = new AgentWorkflowService(props.state, props.projectPath);
        const status = service.status();
        const done = status.tasks.filter((task) => task.status === "succeeded").length;
        const failed = status.tasks.filter((task) => task.status === "failed").length;
        const running = status.tasks.filter((task) => task.status === "running").length;
        const latest = status.messages.at(-1);
        return (_jsxs(Box, { flexDirection: "column", children: [_jsx(Text, { color: status.record.status === "failed" ? "red" : "cyan", children: status.record.status }), _jsx(Text, { color: "gray", children: `roles ${status.record.roles.length} / tasks ${done}/${status.tasks.length}` }), _jsx(Text, { color: "gray", children: `running ${running} / failed ${failed}` }), latest ? _jsx(Text, { color: "gray", children: compact(`${latest.from}->${latest.to}: ${latest.message}`, 30) }) : null] }));
    }
    catch {
        return _jsx(Text, { color: "gray", children: "no active workflow" });
    }
}
function UsageSummary(props) {
    const last = formatUsage(props.lastTurn);
    const session = formatUsage(props.session);
    return (_jsxs(Box, { flexDirection: "column", children: [_jsx(Text, { color: "gray", children: `turn ${last}` }), _jsx(Text, { color: "gray", children: `session ${session}` })] }));
}
function formatUsage(usage) {
    const input = usage?.inputTokens ?? 0;
    const output = usage?.outputTokens ?? 0;
    const hit = usage?.cacheHitTokens ?? 0;
    const miss = usage?.cacheMissTokens ?? 0;
    const cache = hit + miss > 0 ? `${Math.round((hit / (hit + miss)) * 100)}%` : "n/a";
    return `in ${input} / out ${output} / cache ${cache}`;
}
function compact(value, max) {
    if (value.length <= max)
        return value;
    return `...${value.slice(-(max - 3))}`;
}
