import { jsxs as _jsxs, jsx as _jsx, Fragment as _Fragment } from "react/jsx-runtime";
import { Box, Text } from "ink";
import TextInput from "ink-text-input";
import { selectableModels } from "../modelSelection.js";
import { symbols } from "../experimental/theme.js";
import { describePreferenceValue, preferenceRows, preferenceValueString, } from "../onboardingPreferences.js";
const entryOptions = [
    {
        label: "This Device",
        description: "Run Ollama locally — fully offline, nothing leaves the machine"
    },
    {
        label: "Remote Host",
        description: "Reach Ollama on another LAN or Tailscale machine"
    },
    {
        label: "Gemini",
        description: "Google Gemini API — fast hosted models with an API key"
    },
    {
        label: "Gemini-Wrapper",
        description: "Local Gemini Web bridge driven by pasted browser cookies"
    },
    {
        label: "OpenRouter",
        description: "One key, many models — including auto-routed and free tiers"
    },
    {
        label: "NVIDIA",
        description: "NVIDIA NIM OpenAI-compatible inference endpoints"
    },
    {
        label: "Codex",
        description: "Sign in with your ChatGPT account through the Codex CLI"
    }
];
const stepFlow = ["workflow", "host", "auth", "model", "tune"];
export function OnboardingPanel(props) {
    const currentStepIndex = props.state.step === "welcome" || props.state.step === "disclaimer" || props.state.step === "entry"
        ? 0
        : props.state.step === "host" || props.state.step === "host-input"
            ? 1
            : props.state.step === "api-key-choice" || props.state.step === "gemini-key" || props.state.step === "gemini-wrapper-url" || props.state.step === "gemini-wrapper-psid" || props.state.step === "gemini-wrapper-psidts" || props.state.step === "gemini-wrapper-model-mode" || props.state.step === "gemini-wrapper-key" || props.state.step === "openrouter-key" || props.state.step === "nvidia-key" || props.state.step === "codex-login"
                ? 2
                : props.state.step === "model"
                    ? 3
                    : 4;
    const formatModelLabel = props.formatModelLabel ?? ((model) => model);
    const formatModelDescription = props.formatModelDescription ?? (() => "");
    const visibleModels = props.state.step === "model" ? selectableModels(props.input, props.state.models, formatModelLabel) : [];
    const selectedModel = props.state.step === "model" ? visibleModels[props.selectedIndex] ?? null : null;
    return (_jsxs(Box, { borderStyle: "round", borderColor: "cyan", flexDirection: "column", paddingX: 2, paddingY: 1, height: props.height, overflowY: "hidden", children: [_jsxs(Box, { flexDirection: "row", children: [_jsxs(Text, { color: "cyan", bold: true, children: [symbols.assistant, " PatchPilot"] }), _jsx(Text, { color: "gray", children: " \u2014 first-run setup" })] }), _jsxs(Text, { color: "gray", children: ["A few quick choices, then you are ready to ship. ", symbols.bullet, " reopen anytime with /onboarding"] }), _jsx(Box, { marginTop: 1, flexDirection: "row", children: stepFlow.map((step, index) => {
                    const done = index < currentStepIndex;
                    const active = index === currentStepIndex;
                    const glyph = done ? symbols.todoDone : active ? symbols.todoActive : symbols.todoPending;
                    const color = done ? "green" : active ? "cyan" : "gray";
                    return (_jsxs(Text, { color: color, bold: active, children: [index > 0 ? "  " : "", glyph, " ", step] }, step));
                }) }), props.busyMessage ? (_jsx(Box, { marginTop: 1, children: _jsxs(Text, { color: "yellow", children: [symbols.status, " ", props.busyMessage] }) })) : null, props.notice ? (_jsxs(Box, { marginTop: 1, flexDirection: "column", children: [_jsxs(Text, { color: props.notice.tone === "success" ? "green" : props.notice.tone === "warning" ? "yellow" : "red", children: [props.notice.tone === "success" ? symbols.final : props.notice.tone === "warning" ? symbols.approval : symbols.error, " ", props.notice.text] }), props.notice.detail ? _jsxs(Text, { color: "gray", children: ["  ", props.notice.detail] }) : null] })) : null, props.state.step === "welcome" ? (_jsx(WelcomeStep, {})) : null, props.state.step === "disclaimer" ? (_jsx(DisclaimerStep, {})) : null, props.state.step === "entry" ? (_jsx(SelectionList, { title: "Where should the model run?", subtitle: `${symbols.arrow} up/down to move  ${symbols.bullet}  enter to pick  ${symbols.bullet}  esc skips setup`, rows: entryOptions, selectedIndex: props.selectedIndex })) : null, props.state.step === "host" ? (_jsx(SelectionList, { title: "Choose a host", subtitle: `Top row enters a host by hand  ${symbols.bullet}  ${symbols.arrow} left goes back`, rows: [
                    {
                        label: "Enter Host Manually",
                        description: "Type a LAN IP, Tailscale IP, MagicDNS name, or full URL"
                    },
                    ...props.state.hosts.map((host) => ({
                        label: host.deviceName,
                        description: `${host.kind}  ${host.url}${host.version ? `  Ollama ${host.version}` : ""}`
                    }))
                ], selectedIndex: props.selectedIndex })) : null, props.state.step === "host-input" ? (_jsx(InputStep, { title: "Connect to a host", description: "Enter a LAN IP, Tailscale IP, MagicDNS name, or full URL.", prompt: "host > ", value: props.input, onChange: props.onInputChange, onSubmit: props.onInputSubmit })) : null, props.state.step === "api-key-choice" ? (_jsx(SelectionList, { title: props.state.provider === "gemini-wrapper" ? "Gemini-Wrapper bridge auth" : `${providerLabel(props.state.provider)} API key`, subtitle: props.state.provider === "gemini-wrapper" ? "Saved cookies stay in PatchPilot config, never the repo." : "Existing keys stay in PatchPilot config, never the repo.", rows: [
                    ...(props.state.hasExistingKey
                        ? [
                            {
                                label: props.state.provider === "gemini-wrapper" ? "Use Saved Bridge" : "Use Existing Key",
                                description: props.state.provider === "gemini-wrapper" ? "Continue with the saved cookie file" : "Continue with the saved key"
                            }
                        ]
                        : []),
                    ...(props.state.provider === "gemini-wrapper"
                        ? [
                            {
                                label: "Import From Browser",
                                description: "Read Gemini Web cookies locally after this explicit choice"
                            }
                        ]
                        : []),
                    {
                        label: props.state.provider === "gemini-wrapper" ? "Paste Cookie" : "Enter New Key",
                        description: props.state.provider === "gemini-wrapper" ? "Replace the Gemini Web cookie file in PatchPilot config" : "Replace or add the key in PatchPilot config"
                    }
                ], selectedIndex: props.selectedIndex })) : null, props.state.step === "gemini-key" ? (_jsx(InputStep, { title: "Enter your Gemini API key", description: "Stored in PatchPilot's config directory, never in the repository.", prompt: "key  > ", value: props.input, onChange: props.onInputChange, onSubmit: props.onInputSubmit, mask: "*" })) : null, props.state.step === "gemini-wrapper-url" ? (_jsx(InputStep, { title: "Connect Gemini-Wrapper HTTP", description: "Optional advanced mode: enter an explicit OpenAI-compatible wrapper URL.", prompt: "url  > ", value: props.input, onChange: props.onInputChange, onSubmit: props.onInputSubmit })) : null, props.state.step === "gemini-wrapper-psid" ? (_jsx(InputStep, { title: "Connect Gemini-API bridge", description: "Paste __Secure-1PSID. PatchPilot stores it in ~/.patchpilot/gemini-cookies.json with owner-only permissions.", prompt: "psid > ", value: props.input, onChange: props.onInputChange, onSubmit: props.onInputSubmit, mask: "*" })) : null, props.state.step === "gemini-wrapper-psidts" ? (_jsx(InputStep, { title: "Optional Gemini session timestamp", description: "Paste __Secure-1PSIDTS if you have it, or press Enter to skip.", prompt: "ts   > ", value: props.input, onChange: props.onInputChange, onSubmit: props.onInputSubmit, mask: "*" })) : null, props.state.step === "gemini-wrapper-model-mode" ? (_jsx(SelectionList, { title: "Gemini-Wrapper model", subtitle: "Shortcuts resolve through live Gemini Web discovery; Denkaufwand is not a stable bridge control yet.", rows: [
                    {
                        label: "Auto",
                        description: "Let Gemini Web pick the current default model"
                    },
                    {
                        label: "Flash-Lite",
                        description: "Resolve the live Gemini Web Flash-Lite model"
                    },
                    {
                        label: "Flash",
                        description: "Resolve the live Flash model, preferring 3.5 Flash when the bridge lists it"
                    },
                    {
                        label: "Pro",
                        description: "Resolve the live Gemini Web Pro model"
                    },
                    {
                        label: "Manual",
                        description: "Fetch available Gemini Web models and choose one"
                    }
                ], selectedIndex: props.selectedIndex })) : null, props.state.step === "gemini-wrapper-key" ? (_jsx(InputStep, { title: "Enter Gemini-Wrapper API key", description: "Required for remote wrapper URLs. Local wrapper URLs may leave this empty.", prompt: "key  > ", value: props.input, onChange: props.onInputChange, onSubmit: props.onInputSubmit, mask: "*" })) : null, props.state.step === "openrouter-key" ? (_jsx(InputStep, { title: "Enter your OpenRouter API key", description: "Stored in PatchPilot's config directory, never in the repository.", prompt: "key  > ", value: props.input, onChange: props.onInputChange, onSubmit: props.onInputSubmit, mask: "*" })) : null, props.state.step === "nvidia-key" ? (_jsx(InputStep, { title: "Enter your NVIDIA API key", description: "Stored in PatchPilot's config directory, never in the repository.", prompt: "key  > ", value: props.input, onChange: props.onInputChange, onSubmit: props.onInputSubmit, mask: "*" })) : null, props.state.step === "codex-login" ? (_jsxs(Box, { flexDirection: "column", marginTop: 1, children: [_jsxs(Text, { color: "white", bold: true, children: [symbols.arrow, " Connect Codex CLI"] }), _jsx(Text, { color: "gray", children: "Run `codex login` in another terminal, then press Enter here to continue." }), _jsxs(Text, { color: "gray", children: [symbols.bullet, " esc or left arrow goes back"] })] })) : null, props.state.step === "model" ? (_jsxs(_Fragment, { children: [_jsx(InputStep, { title: `Choose a ${props.state.provider} model${props.state.deviceName ? ` on ${props.state.deviceName}` : ""}`, description: "Type to filter. Up/down to move, enter to pick. Left arrow goes back.", prompt: "find > ", value: props.input, onChange: props.onInputChange, onSubmit: props.onInputSubmit }), _jsx(SelectionList, { title: "", subtitle: `${visibleModels.length} matching model${visibleModels.length === 1 ? "" : "s"}`, rows: visibleModels.map((model) => ({
                            label: formatModelLabel(model),
                            description: `${model === selectedModel ? "selected" : "available"}${formatModelDescription(model)}`
                        })), selectedIndex: props.selectedIndex })] })) : null, props.state.step === "preferences" ? (_jsx(PreferencesStep, { provider: props.state.provider, model: props.state.model, preferences: props.state.preferences, selectedIndex: props.selectedIndex })) : null, _jsx(Box, { marginTop: 1, children: _jsxs(Text, { color: "gray", children: [symbols.info, " Remote inference keeps file reads, writes, shell, Git, and tests on this device \u2014 only the model call moves."] }) })] }));
}
function WelcomeStep() {
    return (_jsxs(Box, { marginTop: 1, flexDirection: "column", children: [_jsxs(Text, { color: "cyan", bold: true, children: [symbols.assistant, " A coding agent you can watch work"] }), _jsx(Text, { color: "gray", children: "Local-first. Every read, write, command, and model route stays visible and inspectable." }), _jsxs(Box, { marginTop: 1, flexDirection: "column", children: [_jsxs(Text, { color: "white", children: [symbols.todoActive, " Plan ", symbols.bullet, " read files, map the architecture, keep a live todo list"] }), _jsxs(Text, { color: "white", children: [symbols.todoActive, " Build ", symbols.bullet, " scoped approvals for edits, scripts, tests, and shell"] }), _jsxs(Text, { color: "white", children: [symbols.todoActive, " Review ", symbols.bullet, " show diffs, run checks, leave Git history ready for you"] })] }), _jsxs(Box, { marginTop: 1, flexDirection: "column", children: [_jsxs(Text, { color: "yellow", children: [symbols.pin, " First task to try: \"summarize this repo and list the safest next fixes\""] }), _jsxs(Text, { color: "gray", children: [symbols.arrow, " enter continues ", symbols.bullet, " esc skips ", symbols.bullet, " reopen later with /onboarding"] })] })] }));
}
function DisclaimerStep() {
    return (_jsxs(Box, { marginTop: 1, flexDirection: "column", children: [_jsxs(Text, { color: "yellow", bold: true, children: [symbols.approval, " Use at your own risk"] }), _jsx(Text, { color: "gray", children: "PatchPilot can read files, edit files, and run shell commands when you enable those capabilities." }), _jsx(Text, { color: "gray", children: "You remain responsible for reviewing actions, diffs, commands, provider terms, and generated output." }), _jsx(Text, { color: "gray", children: "The software is provided as-is, without warranty or liability from the maintainer." }), _jsxs(Box, { marginTop: 1, children: [_jsx(Text, { color: "cyan", bold: true, children: "Press [y] to accept and continue" }), _jsxs(Text, { color: "gray", children: ["  ", symbols.bullet, "  Esc goes back"] })] })] }));
}
function providerLabel(provider) {
    return provider === "openrouter" ? "OpenRouter" : provider === "nvidia" ? "NVIDIA" : provider === "gemini-wrapper" ? "Gemini-Wrapper" : "Gemini";
}
function PreferencesStep(props) {
    const rows = preferenceRows;
    const confirmIndex = rows.length;
    return (_jsxs(Box, { flexDirection: "column", marginTop: 1, children: [_jsx(Text, { color: "white", bold: true, children: "Tune your defaults" }), _jsxs(Text, { color: "gray", children: [symbols.final, " ", props.provider, " ", symbols.bullet, " ", props.model] }), _jsxs(Text, { color: "gray", children: [symbols.arrow, " up/down to move ", symbols.bullet, " left/right to change ", symbols.bullet, " enter to finish"] }), rows.map((row, index) => {
                const isSelected = index === props.selectedIndex;
                const value = preferenceValueString(props.preferences, row.key);
                return (_jsxs(Box, { marginTop: 1, flexDirection: "column", children: [_jsxs(Box, { flexDirection: "row", children: [_jsx(Box, { width: 3, children: _jsx(Text, { color: isSelected ? "cyan" : "gray", children: isSelected ? symbols.todoActive : " " }) }), _jsx(Box, { width: 30, children: _jsx(Text, { color: isSelected ? "white" : "gray", bold: isSelected, children: row.label }) }), _jsxs(Text, { color: isSelected ? "cyan" : "gray", bold: true, children: [isSelected ? `${symbols.arrow} ` : "  ", value] })] }), isSelected ? (_jsx(Box, { marginLeft: 3, children: _jsx(Text, { color: "gray", children: describePreferenceValue(row.key, value) }) })) : null] }, row.key));
            }), _jsxs(Box, { marginTop: 1, flexDirection: "row", children: [_jsx(Box, { width: 3, children: _jsx(Text, { color: props.selectedIndex === confirmIndex ? "green" : "gray", children: props.selectedIndex === confirmIndex ? symbols.todoActive : " " }) }), _jsxs(Text, { color: props.selectedIndex === confirmIndex ? "green" : "gray", bold: props.selectedIndex === confirmIndex, children: [symbols.final, " Finish setup and start session"] })] })] }));
}
function InputStep(props) {
    return (_jsxs(Box, { flexDirection: "column", marginTop: 1, children: [_jsx(Text, { color: "white", bold: true, children: props.title }), _jsx(Text, { color: "gray", children: props.description }), _jsxs(Box, { marginTop: 1, children: [_jsxs(Text, { color: "cyan", children: [symbols.user, " ", props.prompt] }), _jsx(TextInput, { value: props.value, onChange: props.onChange, onSubmit: props.onSubmit, mask: props.mask })] })] }));
}
function SelectionList(props) {
    const startIndex = Math.max(0, Math.min(props.selectedIndex - 4, Math.max(0, props.rows.length - 8)));
    const visibleRows = props.rows.slice(startIndex, startIndex + 8);
    const endIndex = startIndex + visibleRows.length;
    return (_jsxs(Box, { flexDirection: "column", marginTop: 1, children: [props.title ? (_jsx(Text, { color: "white", bold: true, children: props.title })) : null, _jsxs(Text, { color: "gray", children: [props.subtitle, " ", props.rows.length > 0 ? `${startIndex + 1}-${endIndex}/${props.rows.length}` : "0/0"] }), visibleRows.map((row, index) => {
                const absoluteIndex = startIndex + index;
                const isSelected = absoluteIndex === props.selectedIndex;
                return (_jsxs(Box, { marginTop: 1, children: [_jsx(Box, { width: 3, children: _jsx(Text, { color: isSelected ? "cyan" : "gray", children: isSelected ? symbols.todoActive : " " }) }), _jsx(Box, { width: 28, children: _jsx(Text, { color: isSelected ? "white" : "cyan", bold: isSelected, wrap: "truncate", children: row.label }) }), _jsx(Text, { color: isSelected ? "white" : "gray", wrap: "truncate", children: row.description })] }, `${absoluteIndex}-${row.label}`));
            })] }));
}
//# sourceMappingURL=OnboardingPanel.js.map