import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { Box, Text } from "ink";
import { inferProfile, listPermissionProfiles, } from "../services/permissions/permissionProfiles.js";
import { useTerminalSize } from "../hooks/useTerminalSize.js";
import { Pane } from "./design/Pane.js";
import { SelectList } from "./design/SelectList.js";
import { StatusBadge } from "./design/StatusBadge.js";
import { Tabs } from "./design/Tabs.js";
import { truncateCells } from "./design/textLayout.js";
import { PermissionRiskCallout } from "./PermissionRiskCallout.js";
export function PermissionPanel(props) {
    const model = permissionPanelModel(props.state, props.profiles ?? listPermissionProfiles());
    const { columns } = useTerminalSize();
    const width = Math.max(58, Math.min(112, columns - 4));
    const profileOptions = permissionPanelProfileOptions(model);
    const selectedProfileIndex = Math.max(0, profileOptions.findIndex((option) => option.selected));
    return (_jsx(Box, { flexDirection: "column", marginBottom: 1, children: _jsxs(Pane, { width: width, title: "permissions", tone: permissionTone(model), paddingX: 1, children: [_jsxs(Box, { flexDirection: "row", justifyContent: "space-between", children: [_jsxs(Box, { flexDirection: "column", children: [_jsx(Text, { bold: true, color: "cyan", children: props.title ?? "Permissions" }), _jsx(Text, { color: "gray", children: truncateCells(`shell ${model.shell} | browser ${model.browser}`, Math.max(24, width - 20)) })] }), _jsx(StatusBadge, { label: model.currentProfile, tone: permissionTone(model) })] }), _jsx(Box, { marginTop: 1, children: _jsx(Tabs, { title: "view", selectedId: "profiles", tabs: [
                            { id: "profiles", title: "profiles", count: model.rows.length, tone: "brand" },
                            { id: "rules", title: "rules", tone: "muted" },
                            { id: "approval", title: "approval", tone: "muted" },
                        ], width: width }) }), _jsxs(Box, { flexDirection: "column", marginTop: 1, children: [_jsx(PermissionRow, { label: "profile", value: model.currentProfile, tone: permissionTone(model) }), _jsx(PermissionRow, { label: "shell", value: model.shell, tone: model.shell === "on" ? "warning" : "success" }), _jsx(PermissionRow, { label: "browser", value: model.browser, tone: model.browser === "on" ? "warning" : "success" })] }), _jsx(Box, { marginTop: 1, children: _jsx(PermissionRiskCallout, { state: props.state }) }), _jsx(Box, { marginTop: 1, children: _jsx(Text, { color: "gray", children: "profiles" }) }), _jsx(SelectList, { options: profileOptions, selectedIndex: selectedProfileIndex, visibleCount: 6, width: width }), _jsx(Box, { marginTop: 1, children: _jsx(Text, { color: "gray", children: "commands" }) }), _jsx(SelectList, { options: permissionPanelCommandOptions(), selectedIndex: 0, visibleCount: 4, width: width }), _jsx(Text, { color: "gray", children: "/permissions profile dev | /permissions profile safe | /approval policy on" })] }) }));
}
export function permissionPanelModel(state, profiles = listPermissionProfiles()) {
    const currentProfile = state.profile ?? inferProfile(state);
    return {
        currentProfile,
        shell: state.allowShell ? "on" : "off",
        browser: state.allowBrowser ? "on" : "off",
        rows: profiles.map((profile) => ({
            name: profile.name,
            active: currentProfile === profile.name,
            shell: profile.allowShell ? "on" : "off",
            browser: profile.allowBrowser ? "on" : "off",
            description: profile.description,
        })),
    };
}
export function permissionPanelProfileOptions(model) {
    const options = model.rows.map((row) => ({
        id: row.name,
        label: row.name,
        detail: `shell ${row.shell} | browser ${row.browser}`,
        description: row.description,
        selected: row.active,
        tone: row.active ? "brand" : row.name === "open" ? "warning" : "muted",
    }));
    if (model.currentProfile === "custom" && !options.some((option) => option.selected)) {
        return [{
                id: "custom",
                label: "custom",
                detail: `shell ${model.shell} | browser ${model.browser}`,
                description: "manual runtime override; not one of the built-in profiles",
                selected: true,
                tone: "warning",
            }, ...options];
    }
    return options;
}
export function permissionPanelCommandOptions() {
    return [
        {
            id: "safe",
            label: "safe",
            detail: "/permissions profile safe",
            description: "disable shell and browser actions",
            tone: "success",
        },
        {
            id: "dev",
            label: "dev",
            detail: "/permissions profile dev",
            description: "allow local shell while keeping browser off",
            tone: "brand",
        },
        {
            id: "browser",
            label: "browser",
            detail: "/permissions profile browser",
            description: "allow browser actions without shell",
            tone: "brand",
        },
        {
            id: "open",
            label: "open",
            detail: "/permissions profile open",
            description: "allow shell and browser; use only in trusted workspaces",
            tone: "warning",
        },
    ];
}
function PermissionRow(props) {
    return (_jsxs(Box, { flexDirection: "row", children: [_jsx(StatusBadge, { label: props.label, tone: props.tone ?? "muted" }), _jsx(Text, { children: " " }), _jsx(Text, { color: "gray", children: props.value })] }));
}
function permissionTone(model) {
    if (model.currentProfile === "open")
        return "warning";
    if (model.shell === "on" || model.browser === "on")
        return "brand";
    if (model.currentProfile === "custom")
        return "warning";
    return "success";
}
