import { getKeybindings, truncateToWidth, visibleWidth } from "@sheason/pi-tui";
import { theme } from "../../components/index.js";
import { renderForkedPanelBorder } from "./selector-themes.js";
const STATUS_COLOR = {
    running: "success",
    starting: "warning",
    stopped: "muted",
    error: "error",
};
export class RemoteSourceListSelectorComponent {
    sources;
    onSelectSource;
    onCancelSelection;
    selectedIndex = 0;
    focused = false;
    constructor(sources, onSelectSource, onCancelSelection) {
        this.sources = sources;
        this.onSelectSource = onSelectSource;
        this.onCancelSelection = onCancelSelection;
        if (this.sources.length === 0) {
            this.selectedIndex = 0;
        }
    }
    getFocusTarget() {
        return this;
    }
    render(width) {
        const lines = [
            renderForkedPanelBorder(width),
            "",
            theme.bold(theme.fg("accent", "Hub and Peer Sources")),
            theme.fg("muted", "Select a source to inspect or manage."),
            "",
        ];
        if (this.sources.length === 0) {
            lines.push(theme.fg("muted", "  No sources are configured."));
            lines.push("");
            lines.push(theme.fg("dim", "  Esc to close"));
            lines.push("");
            lines.push(renderForkedPanelBorder(width));
            return lines.map((line) => truncateToWidth(line, width, ""));
        }
        const nameColumnWidth = computeNameColumnWidth(this.sources);
        for (let i = 0; i < this.sources.length; i += 1) {
            const source = this.sources[i];
            if (!source) {
                continue;
            }
            const isSelected = i === this.selectedIndex;
            lines.push(this.renderRow(source, isSelected, nameColumnWidth));
            if (source.error) {
                const padding = " ".repeat(2 + 2 + nameColumnWidth + 2);
                lines.push(theme.fg("muted", `${padding}error: ${source.error}`));
            }
        }
        lines.push("");
        lines.push(theme.fg("dim", "  Enter to inspect · Esc to close"));
        lines.push("");
        lines.push(renderForkedPanelBorder(width));
        return lines.map((line) => truncateToWidth(line, width, ""));
    }
    handleInput(data) {
        const kb = getKeybindings();
        if (this.sources.length === 0) {
            if (kb.matches(data, "tui.select.cancel")) {
                this.onCancelSelection();
            }
            return;
        }
        if (kb.matches(data, "tui.select.up")) {
            this.selectedIndex = this.selectedIndex === 0 ? this.sources.length - 1 : this.selectedIndex - 1;
            return;
        }
        if (kb.matches(data, "tui.select.down")) {
            this.selectedIndex = this.selectedIndex === this.sources.length - 1 ? 0 : this.selectedIndex + 1;
            return;
        }
        if (kb.matches(data, "tui.select.confirm")) {
            const selected = this.sources[this.selectedIndex];
            if (selected) {
                this.onSelectSource(selected);
            }
            return;
        }
        if (kb.matches(data, "tui.select.cancel")) {
            this.onCancelSelection();
        }
    }
    invalidate() { }
    renderRow(source, isSelected, nameColumnWidth) {
        const prefix = isSelected ? theme.fg("accent", "→ ") : "  ";
        const namePadded = padEndVisible(source.name, nameColumnWidth);
        const name = isSelected ? theme.fg("accent", namePadded) : namePadded;
        const transport = theme.fg("muted", `[${source.transport}]`);
        const origin = theme.fg("muted", source.origin === "peer" ? "[peer]" : "[hub]");
        const statusColor = STATUS_COLOR[source.status];
        const status = theme.fg(statusColor, source.status);
        return `${prefix}${name}  ${origin} ${transport} ${status}`;
    }
}
function computeNameColumnWidth(sources) {
    let max = 4;
    for (const source of sources) {
        const w = visibleWidth(source.name);
        if (w > max) {
            max = w;
        }
    }
    return Math.min(max, 32);
}
function padEndVisible(text, columnWidth) {
    const w = visibleWidth(text);
    if (w >= columnWidth) {
        return text;
    }
    return `${text}${" ".repeat(columnWidth - w)}`;
}
//# sourceMappingURL=source-list-selector.js.map