import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { Box, Text } from "ink";
import { formatSshTarget } from "../services/remote/sshProfileService.js";
import { useTerminalSize } from "../hooks/useTerminalSize.js";
import { Pane } from "./design/Pane.js";
import { StatusBadge } from "./design/StatusBadge.js";
import { truncateCells } from "./design/textLayout.js";
export function SshPanel(props) {
    const { columns } = useTerminalSize();
    const width = Math.max(52, Math.min(112, columns - 4));
    return (_jsx(Box, { flexDirection: "column", marginBottom: 1, children: _jsxs(Pane, { width: width, title: "ssh", tone: "brand", paddingX: 1, children: [_jsxs(Box, { flexDirection: "row", justifyContent: "space-between", children: [_jsxs(Box, { flexDirection: "column", children: [_jsx(Text, { bold: true, color: "cyan", children: props.model.title }), _jsx(Text, { color: "gray", children: truncateCells(props.model.subtitle, Math.max(24, width - 20)) })] }), _jsx(StatusBadge, { label: `${props.model.rows.length}`, tone: props.model.rows.length > 0 ? "brand" : "muted" })] }), _jsx(Box, { flexDirection: "column", marginTop: 1, children: props.model.rows.length === 0 ? (_jsx(Text, { color: "gray", children: "No SSH records" })) : props.model.rows.map((row) => (_jsx(SshPanelRowView, { row: row, width: width }, row.key))) }), props.model.preview && props.model.preview.length > 0 ? (_jsxs(Box, { flexDirection: "column", marginBottom: 1, children: [_jsx(Text, { color: "gray", children: "ssh detail" }), props.model.preview.map((line, index) => (_jsx(Text, { color: "gray", children: truncateCells(`  ${line}`, Math.max(24, width - 4)) }, `${index}-${line}`)))] })) : null, _jsx(Text, { color: "gray", children: truncateCells(props.model.footer, Math.max(24, width - 4)) })] }) }));
}
export function sshProfilesPanelModel(profiles) {
    return {
        title: "SSH profiles",
        subtitle: `${profiles.length} configured profile${profiles.length === 1 ? "" : "s"}`,
        rows: profiles.map((profile) => sshProfileRow(profile)),
        footer: "/ssh show <name> | /ssh health <name> | /ssh connect <name>",
    };
}
export function sshProfileDetailPanelModel(profile, preview) {
    return {
        title: `SSH profile: ${profile.name}`,
        subtitle: formatSshTarget(profile),
        rows: [sshProfileRow(profile)],
        preview: [
            `preview: ${preview}`,
            profile.remotePath ? `remote path: ${profile.remotePath}` : "remote path: (none)",
        ],
        footer: `/ssh health ${profile.name} | /ssh run ${profile.name} <command>`,
    };
}
export function sshSessionsPanelModel(sessions) {
    return {
        title: "SSH sessions",
        subtitle: `${sessions.length} recent session${sessions.length === 1 ? "" : "s"}`,
        rows: sessions.map((session) => ({
            key: session.id,
            name: session.id,
            status: session.status,
            tone: toneForSession(session.status),
            detail: `${session.profileName} ${session.target}`,
            note: `updated=${new Date(session.updatedAtMs).toISOString()}`,
        })),
        footer: "/ssh close <session-id> | /ssh connect <name>",
    };
}
export function sshHistoryPanelModel(records) {
    return {
        title: "SSH history",
        subtitle: `${records.length} command record${records.length === 1 ? "" : "s"}`,
        rows: records.map((record) => ({
            key: record.id,
            name: record.id,
            status: record.timedOut ? "timeout" : record.exitCode === 0 ? "ok" : `exit ${record.exitCode ?? "?"}`,
            tone: record.timedOut || record.exitCode !== 0 ? "error" : "success",
            detail: `${record.profileName} ${record.command}`,
            note: `${record.target} duration=${record.durationMs}ms`,
        })),
        footer: "/ssh history [limit] | /ssh run <name> <command>",
    };
}
export function sshHealthPanelModel(result, sessionId) {
    return {
        title: "SSH health",
        subtitle: result.target,
        rows: [{
                key: result.profileName,
                name: result.profileName,
                status: result.status,
                tone: result.status === "ok" ? "success" : "error",
                detail: result.message,
                note: sessionId ? `session=${sessionId}` : "",
            }],
        footer: `/ssh run ${result.profileName} <command> | /ssh sessions`,
    };
}
export function sshWorkerPanelModel(result) {
    return {
        title: "SSH worker",
        subtitle: `${result.profileName} / ${result.runId}`,
        rows: result.steps.map((step, index) => ({
            key: step.task?.id ?? `step-${index}`,
            name: step.task?.id ?? "idle",
            status: step.status,
            tone: step.status === "completed" ? "success" : step.status === "failed" ? "error" : "muted",
            detail: step.task ? `${step.task.agent}: ${step.task.title}` : step.message,
            note: step.task ? step.message : "",
        })),
        preview: [`status: ${result.status}`, `message: ${result.message}`, `steps: ${result.steps.length}`],
        footer: `/tasks ${result.runId} | /queue ${result.runId}`,
    };
}
export function sshOperationPanelModel(input) {
    return {
        title: input.title,
        subtitle: input.subtitle,
        rows: [{
                key: input.name,
                name: input.name,
                status: input.status,
                tone: input.tone ?? (input.status === "ok" || input.status === "saved" ? "success" : "brand"),
                detail: input.detail,
                note: input.note ?? "",
            }],
        preview: input.preview,
        footer: input.footer,
    };
}
function SshPanelRowView(props) {
    const detailWidth = Math.max(20, props.width - 38);
    const noteWidth = Math.max(20, props.width - 12);
    return (_jsxs(Box, { flexDirection: "column", marginBottom: 1, children: [_jsxs(Box, { flexDirection: "row", children: [_jsx(StatusBadge, { label: props.row.status, tone: props.row.tone }), _jsx(Text, { children: " " }), _jsx(Text, { color: "cyan", children: truncateCells(props.row.name.padEnd(20), 20) }), _jsx(Text, { color: "gray", children: truncateCells(props.row.detail, detailWidth) })] }), props.row.note ? (_jsxs(Box, { flexDirection: "row", children: [_jsx(Text, { color: "gray", children: "  " }), _jsx(Text, { color: "gray", children: truncateCells(props.row.note, noteWidth) })] })) : null] }));
}
function sshProfileRow(profile) {
    return {
        key: profile.name,
        name: profile.name,
        status: "profile",
        tone: "brand",
        detail: formatSshTarget(profile),
        note: [
            profile.port ? `port=${profile.port}` : "",
            profile.remotePath ? `remotePath=${profile.remotePath}` : "",
        ].filter(Boolean).join(" "),
    };
}
function toneForSession(status) {
    if (status === "connected")
        return "success";
    if (status === "failed")
        return "error";
    if (status === "planned")
        return "warning";
    return "muted";
}
