import { jsxs as _jsxs, jsx as _jsx } from "react/jsx-runtime";
import { Box, Text } from "ink";
import { truncateCells } from "./textLayout.js";
import { toneColor } from "./terminalTheme.js";
export function resolveSelectedTabId(tabs, selectedId) {
    if (tabs.length === 0)
        return undefined;
    if (selectedId && tabs.some((tab) => tab.id === selectedId && !tab.disabled))
        return selectedId;
    return tabs.find((tab) => !tab.disabled)?.id ?? tabs[0]?.id;
}
export function buildTabStripParts(tabs, selectedId) {
    const selected = resolveSelectedTabId(tabs, selectedId);
    return tabs.map((tab) => {
        const label = tab.count === undefined ? tab.title : `${tab.title} ${tab.count}`;
        return {
            id: tab.id,
            label,
            selected: tab.id === selected,
            disabled: tab.disabled ?? false,
            tone: tab.disabled ? "muted" : tab.tone ?? (tab.id === selected ? "brand" : "muted"),
        };
    });
}
export function Tabs(props) {
    if (props.hidden)
        return null;
    const parts = buildTabStripParts(props.tabs, props.selectedId);
    const width = props.width ?? 96;
    return (_jsxs(Box, { flexDirection: "column", children: [_jsxs(Box, { flexDirection: "row", children: [props.title ? _jsxs(Text, { bold: true, color: toneColor(props.tone ?? "brand"), children: [truncateCells(props.title, 18), " "] }) : null, parts.map((part) => (_jsx(Text, { bold: part.selected, color: toneColor(part.tone), inverse: part.selected, dimColor: part.disabled, children: ` ${truncateCells(part.label, Math.max(4, Math.min(24, width - 8)))} ` }, part.id)))] }), props.children ? _jsx(Box, { flexDirection: "column", marginTop: 1, children: props.children }) : null, props.footer ? _jsx(Box, { marginTop: 1, children: _jsx(Text, { color: "gray", children: props.footer }) }) : null] }));
}
