"use client";
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { useMemo } from "react";
import { cn } from "@stigmer/theme";
import { RunnerPhase } from "@stigmer/protos/ai/stigmer/agentic/runner/v1/enum_pb";
import { useRunnerList } from "./useRunnerList";
import { isActivePhase, phaseDotColor, PHASE_SORT_ORDER } from "./phase";
/**
 * Cursor-inspired "Run On" section for the workspace popover.
 *
 * Renders a compact, sectioned list of available runners so users can
 * pick where their session runs without leaving the workspace context.
 * Designed to be composed alongside {@link WorkspaceEditor} inside the
 * workspace popover — NOT as a standalone picker.
 *
 * Label strategy (SDK-agnostic, no platform branding):
 * - "Auto" for null selection (platform assigns a runner)
 * - "Cloud" for system-managed runners
 * - "This machine" when the host provides `localRunnerIds`
 * - Runner name / hostname for everything else
 */
export function WorkspaceRunnerSelector({ org, value, onChange, disabled, localRunnerIds, }) {
    const { runners, isLoading } = useRunnerList(org);
    const active = useMemo(() => {
        return runners
            .filter((r) => isActivePhase(r.status?.phase ?? RunnerPhase.UNSPECIFIED))
            .sort((a, b) => {
            const pa = a.status?.phase ?? RunnerPhase.UNSPECIFIED;
            const pb = b.status?.phase ?? RunnerPhase.UNSPECIFIED;
            const po = PHASE_SORT_ORDER[pa] - PHASE_SORT_ORDER[pb];
            if (po !== 0)
                return po;
            return (a.metadata?.name ?? "").localeCompare(b.metadata?.name ?? "");
        });
    }, [runners]);
    return (_jsxs("div", { className: "space-y-1", role: "listbox", "aria-label": "Run on", children: [_jsx("div", { className: "px-1 text-[0.6rem] font-medium uppercase tracking-wider text-muted-foreground", children: "Run On" }), _jsx(RunOnOption, { selected: value === null, onClick: () => onChange(null), disabled: disabled, label: "Auto", hint: "platform assigns", dotClass: "bg-primary" }), active.map((r) => {
                const id = r.metadata.id;
                return (_jsx(RunOnOption, { selected: value === id, onClick: () => onChange(id), disabled: disabled, label: runnerLabel(r, localRunnerIds), hint: runnerHint(r, localRunnerIds), dotClass: phaseDotColor(r.status?.phase ?? RunnerPhase.UNSPECIFIED) }, id));
            }), isLoading && active.length === 0 && (_jsx("div", { className: "px-1 py-2 text-[0.65rem] text-muted-foreground", children: "Loading runners..." })), !isLoading && active.length === 0 && (_jsx("div", { className: "px-1 py-2 text-[0.65rem] text-muted-foreground", children: "No runners available" }))] }));
}
function RunOnOption({ selected, onClick, disabled, label, hint, dotClass, }) {
    return (_jsxs("button", { type: "button", onClick: onClick, disabled: disabled, className: cn("flex w-full items-center gap-2 rounded-md px-2 py-1.5 text-left text-xs transition-colors", "disabled:pointer-events-none disabled:opacity-50", selected
            ? "bg-accent font-medium text-foreground"
            : "text-foreground hover:bg-accent-hover"), role: "option", "aria-selected": selected, children: [_jsx("span", { className: cn("inline-block h-1.5 w-1.5 shrink-0 rounded-full", dotClass), "aria-hidden": "true" }), _jsx("span", { className: "min-w-0 flex-1 truncate", children: label }), hint && (_jsx("span", { className: "shrink-0 text-[0.6rem] text-muted-foreground", children: hint }))] }));
}
function runnerLabel(runner, localRunnerIds) {
    const id = runner.metadata?.id ?? "";
    if (localRunnerIds?.has(id))
        return "This machine";
    return runner.metadata?.name ?? runner.status?.connectionInfo?.hostname ?? "Unnamed";
}
function runnerHint(runner, localRunnerIds) {
    const id = runner.metadata?.id ?? "";
    if (localRunnerIds?.has(id)) {
        return runner.status?.connectionInfo?.hostname;
    }
    const name = runner.metadata?.name;
    const hostname = runner.status?.connectionInfo?.hostname;
    if (name && hostname && name !== hostname)
        return hostname;
    return undefined;
}
//# sourceMappingURL=WorkspaceRunnerSelector.js.map