"use client";
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { useCallback, useRef, useState } from "react";
import { cn } from "@stigmer/theme";
import { ApiResourceVisibility } from "@stigmer/protos/ai/stigmer/commons/apiresource/enum_pb";
const OPTIONS = [
    { value: ApiResourceVisibility.visibility_private, label: "Private" },
    { value: ApiResourceVisibility.visibility_public, label: "Public" },
];
/**
 * Segmented control for toggling resource visibility between
 * Private and Public.
 *
 * Switching to PUBLIC shows a brief inline confirmation prompt
 * since making a resource publicly visible is a consequential
 * action. Switching to PRIVATE applies immediately without
 * confirmation (revoking access is always safe).
 *
 * Follows the same visual pattern as {@link ScopeToggle} —
 * WAI-ARIA Radio Group with roving tabindex. All visual
 * properties flow through `--stgm-*` design tokens.
 *
 * @example
 * ```tsx
 * <VisibilityToggle
 *   visibility={skill.metadata.visibility}
 *   onVisibilityChange={handleVisibilityChange}
 *   isPending={isPending}
 * />
 * ```
 */
export function VisibilityToggle({ visibility, onVisibilityChange, isPending = false, disabled = false, className, }) {
    const [confirming, setConfirming] = useState(false);
    const optionRefs = useRef([]);
    const isPublic = visibility === ApiResourceVisibility.visibility_public;
    const effectivelyDisabled = disabled || isPending;
    const handleSelect = useCallback((value) => {
        if (value === visibility)
            return;
        if (value === ApiResourceVisibility.visibility_public) {
            setConfirming(true);
            return;
        }
        onVisibilityChange(value);
    }, [visibility, onVisibilityChange]);
    const confirmPublic = useCallback(() => {
        setConfirming(false);
        onVisibilityChange(ApiResourceVisibility.visibility_public);
    }, [onVisibilityChange]);
    const cancelConfirm = useCallback(() => {
        setConfirming(false);
    }, []);
    const handleKeyDown = useCallback((e, index) => {
        let nextIndex = null;
        if (e.key === "ArrowRight" || e.key === "ArrowDown") {
            e.preventDefault();
            nextIndex = (index + 1) % OPTIONS.length;
        }
        else if (e.key === "ArrowLeft" || e.key === "ArrowUp") {
            e.preventDefault();
            nextIndex = (index - 1 + OPTIONS.length) % OPTIONS.length;
        }
        if (nextIndex !== null) {
            optionRefs.current[nextIndex]?.focus();
            handleSelect(OPTIONS[nextIndex].value);
        }
    }, [handleSelect]);
    const currentIndex = OPTIONS.findIndex((o) => o.value === visibility);
    return (_jsxs("div", { className: cn("inline-flex flex-col gap-1.5", className), children: [_jsx("div", { role: "radiogroup", "aria-label": "Resource visibility", "aria-disabled": effectivelyDisabled || undefined, className: cn("inline-flex rounded-md bg-muted p-0.5", effectivelyDisabled && "pointer-events-none opacity-50"), children: OPTIONS.map((option, index) => {
                    const isSelected = visibility === option.value;
                    return (_jsx("button", { ref: (el) => {
                            optionRefs.current[index] = el;
                        }, type: "button", role: "radio", "aria-checked": isSelected, tabIndex: isSelected ? 0 : -1, disabled: effectivelyDisabled, onClick: () => handleSelect(option.value), onKeyDown: (e) => handleKeyDown(e, index), className: cn("cursor-pointer rounded-sm px-3 py-1 text-xs font-medium transition-colors", "focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring", isSelected && option.value === ApiResourceVisibility.visibility_public
                            ? "bg-emerald-100 text-emerald-800 shadow-sm dark:bg-emerald-900/40 dark:text-emerald-300"
                            : isSelected
                                ? "bg-background text-foreground shadow-sm"
                                : "text-muted-foreground hover:text-foreground"), children: isPending && isSelected ? (_jsxs("span", { className: "inline-flex items-center gap-1", children: [_jsx("span", { className: "inline-block h-3 w-3 animate-spin rounded-full border-2 border-current border-t-transparent", "aria-hidden": "true" }), option.label] })) : (option.label) }, option.value));
                }) }), confirming && (_jsxs("div", { className: cn("flex items-center gap-2 rounded-md border border-amber-200 bg-amber-50 px-3 py-1.5 text-xs", "dark:border-amber-800/50 dark:bg-amber-950/30"), role: "alert", children: [_jsx("span", { className: "text-amber-800 dark:text-amber-200", children: "Make visible to all users?" }), _jsx("button", { type: "button", onClick: confirmPublic, className: cn("rounded px-2 py-0.5 text-xs font-medium", "bg-amber-600 text-white hover:bg-amber-700", "dark:bg-amber-600 dark:hover:bg-amber-500", "focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring"), children: "Confirm" }), _jsx("button", { type: "button", onClick: cancelConfirm, className: cn("rounded px-2 py-0.5 text-xs font-medium", "text-amber-700 hover:text-amber-900", "dark:text-amber-300 dark:hover:text-amber-100", "focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring"), children: "Cancel" })] }))] }));
}
//# sourceMappingURL=VisibilityToggle.js.map