import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { Box, Text } from "ink";
import { useTerminalSize } from "../hooks/useTerminalSize.js";
import { Pane } from "./design/Pane.js";
import { StatusBadge } from "./design/StatusBadge.js";
import { truncateCells } from "./design/textLayout.js";
import { ValidationErrorsList } from "./ValidationErrorsList.js";
export function PluginPanel(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: "plugins", 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 plugins discovered" })) : props.model.rows.map((row) => (_jsx(PluginPanelRowView, { 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: "plugin components" }), props.model.preview.map((line, index) => (_jsx(Text, { color: "gray", children: truncateCells(`  ${line}`, Math.max(24, width - 4)) }, `${index}-${line}`)))] })) : null, _jsx(ValidationErrorsList, { errors: props.model.validationErrors ?? [], width: width - 2 }), _jsx(Text, { color: "gray", children: truncateCells(props.model.footer, Math.max(24, width - 4)) })] }) }));
}
export function pluginListPanelModel(plugins) {
    return {
        title: "Plugins",
        subtitle: `${plugins.length} discovered plugin${plugins.length === 1 ? "" : "s"}`,
        rows: plugins.map((plugin) => pluginRow(plugin)),
        footer: "/plugins show <name> | /plugins enable <name> | /plugins validate [name]",
    };
}
export function pluginSearchPanelModel(plugins, query) {
    return {
        title: "Plugin search",
        subtitle: query ? `query: ${query}` : "all plugins",
        rows: plugins.map((plugin) => ({
            key: `${plugin.scope}/${plugin.name}`,
            name: `${plugin.scope}/${plugin.name}`,
            status: plugin.enabled ? "enabled" : "disabled",
            tone: plugin.enabled ? toneForScope(plugin.scope) : "muted",
            detail: plugin.description || "(no description)",
            note: [
                plugin.commands.length ? `commands=${plugin.commands.map((command) => `/${plugin.name}:${command}`).join(",")}` : "commands=none",
                plugin.source ? `source=${plugin.source.kind}:${plugin.source.sourcePath}` : plugin.path,
            ].join(" "),
        })),
        footer: "/plugins show <name> | /plugins install <path> [name]",
    };
}
export function pluginDetailPanelModel(plugin) {
    return {
        title: `Plugin: ${plugin.name}`,
        subtitle: `${plugin.scope} / ${plugin.path}`,
        rows: [pluginRow(plugin)],
        preview: pluginComponentPreview(plugin),
        footer: `/plugins ${plugin.enabled ? "disable" : "enable"} ${plugin.name} | /plugins validate ${plugin.name}`,
    };
}
export function pluginValidationPanelModel(results) {
    return {
        title: "Plugin validation",
        subtitle: `${results.length} validation result${results.length === 1 ? "" : "s"}`,
        rows: results.map((result) => ({
            key: result.name,
            name: result.name,
            status: result.ok ? "ok" : "failed",
            tone: result.ok ? "success" : "error",
            detail: result.path || "(missing)",
            note: [
                ...result.errors.map((error) => `error: ${error}`),
                ...result.warnings.map((warning) => `warning: ${warning}`),
            ].join("; "),
        })),
        validationErrors: results.flatMap((result) => [
            ...result.errors.map((error) => ({
                file: result.path || result.name,
                path: result.name,
                message: error,
                severity: "error",
            })),
            ...result.warnings.map((warning) => ({
                file: result.path || result.name,
                path: result.name,
                message: warning,
                severity: "warning",
            })),
        ]),
        footer: "/plugins list | /plugins show <name>",
    };
}
function PluginPanelRowView(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 pluginRow(plugin) {
    const loaded = "manifest" in plugin || "manifestError" in plugin ? plugin : undefined;
    const manifest = loaded?.manifest;
    const status = loaded?.manifestError ? "invalid" : plugin.enabled ? "enabled" : "disabled";
    return {
        key: `${plugin.scope}/${plugin.name}`,
        name: `${plugin.scope}/${plugin.name}`,
        status,
        tone: loaded?.manifestError ? "error" : plugin.enabled ? toneForScope(plugin.scope) : "muted",
        detail: loaded?.manifestError ?? manifest?.description ?? "(manifest not loaded)",
        note: [
            manifest?.commands.length ? `commands=${manifest.commands.map((command) => `/${manifest.name}:${command.name}`).join(",")}` : "commands=none",
            componentCounts(loaded),
            plugin.path,
        ].filter(Boolean).join(" "),
    };
}
function toneForScope(scope) {
    if (scope === "project")
        return "success";
    if (scope === "user")
        return "brand";
    return "muted";
}
function componentCounts(plugin) {
    const manifest = plugin?.manifest;
    if (!manifest)
        return "";
    return [
        manifest.agents.length ? `agents=${manifest.agents.length}` : "",
        manifest.skills.length ? `skills=${manifest.skills.length}` : "",
        manifest.output_styles.length ? `styles=${manifest.output_styles.length}` : "",
        manifest.hooks.length ? `hooks=${manifest.hooks.length}` : "",
    ].filter(Boolean).join(" ");
}
function pluginComponentPreview(plugin) {
    const manifest = plugin.manifest;
    if (plugin.manifestError)
        return [`manifest error: ${plugin.manifestError}`];
    if (!manifest)
        return ["manifest missing"];
    return [
        `description: ${manifest.description || "(none)"}`,
        `commands: ${manifest.commands.length ? manifest.commands.map((command) => `/${manifest.name}:${command.name}`).join(", ") : "none"}`,
        `agents: ${manifest.agents.length ? manifest.agents.join(", ") : "none"}`,
        `skills: ${manifest.skills.length ? manifest.skills.join(", ") : "none"}`,
        `output styles: ${manifest.output_styles.length ? manifest.output_styles.join(", ") : "none"}`,
        `hooks: ${manifest.hooks.length ? manifest.hooks.map((hook) => `${hook.event}:${hook.id}`).join(", ") : "none"}`,
    ];
}
