"use client";
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { useCallback } from "react";
import { cn } from "@stigmer/theme";
import { WorkflowExecutionVisibility } from "@stigmer/protos/ai/stigmer/agentic/workflowinstance/v1/spec_pb";
import { getUserMessage } from "@stigmer/sdk";
import { useUpdateWorkflowInstanceExecutionVisibility } from "./useUpdateWorkflowInstanceExecutionVisibility.js";
const RUN_VISIBILITY_OPTIONS = [
    {
        value: WorkflowExecutionVisibility.private,
        label: "Only the person who runs it",
        description: "Each run is visible only to whoever started it (and explicit shares).",
    },
    {
        value: WorkflowExecutionVisibility.organization,
        label: "All organization members",
        description: "Everyone in the organization can observe every run of this instance.",
    },
];
/**
 * Segmented control for an instance's run (execution) visibility — a separate
 * axis from the instance's own visibility: an owner can keep the instance
 * private while letting the whole org observe its runs.
 *
 * Writes the `execution_visibility` spec field via the dedicated RPC, which
 * reconciles the dormant `workflow_instance#execution_viewer` FGA relation in
 * Cloud mode. `unspecified` is treated as private (the default).
 *
 * Only meaningful while the instance itself is private; an ORG/PUBLIC instance
 * already exposes its runs to org members by inheritance, so callers should
 * render this only in the private case.
 */
export function RunVisibilityControl({ instanceId, executionVisibility, onChanged, }) {
    const { updateExecutionVisibility, isUpdating, error } = useUpdateWorkflowInstanceExecutionVisibility();
    const current = executionVisibility === WorkflowExecutionVisibility.unspecified
        ? WorkflowExecutionVisibility.private
        : executionVisibility;
    const handleSelect = useCallback(async (value) => {
        if (value === current || isUpdating)
            return;
        try {
            await updateExecutionVisibility(instanceId, value);
            onChanged?.();
        }
        catch {
            // error surfaced below
        }
    }, [current, isUpdating, updateExecutionVisibility, instanceId, onChanged]);
    return (_jsxs("div", { className: "space-y-2", children: [_jsx("div", { role: "radiogroup", "aria-label": "Run visibility", className: "flex flex-col gap-1.5", children: RUN_VISIBILITY_OPTIONS.map((option) => {
                    const selected = option.value === current;
                    return (_jsxs("button", { type: "button", role: "radio", "aria-checked": selected, disabled: isUpdating, onClick: () => handleSelect(option.value), className: cn("flex flex-col items-start gap-0.5 rounded-md border px-3 py-2 text-left", "focus:outline-none focus:ring-2 focus:ring-ring", "disabled:opacity-60", selected
                            ? "border-primary bg-primary/5"
                            : "border-border hover:bg-accent-hover"), children: [_jsx("span", { className: "text-xs font-medium text-foreground", children: option.label }), _jsx("span", { className: "text-[0.65rem] text-muted-foreground", children: option.description })] }, option.value));
                }) }), error && (_jsx("p", { className: "text-xs text-destructive", role: "alert", children: getUserMessage(error) }))] }));
}
//# sourceMappingURL=RunVisibilityControl.js.map