"use client";
import { useCallback, useMemo, useState } from "react";
import { getGrantableRoles, iamRoleDisplayName, iamRoleDescription, iamRoleToString, } from "@stigmer/sdk";
/**
 * Headless hook for selecting an IAM role for a given resource kind.
 *
 * Provides role options with display metadata (label, description,
 * FGA relation string) and selection state. Use this directly if you
 * need a custom role picker UI; for the styled version, use
 * {@link RoleSelector}.
 *
 * @param kind - The resource kind to show grantable roles for, or `null`.
 * @param defaultRole - Optional initial selection.
 *
 * @example
 * ```tsx
 * const { options, selected, select } = useRoleSelector(ApiResourceKind.organization);
 *
 * options.map(opt => (
 *   <button key={opt.value} onClick={() => select(opt.role)}>
 *     {opt.label} — {opt.description}
 *   </button>
 * ));
 * ```
 */
export function useRoleSelector(kind, defaultRole) {
    const [selected, setSelected] = useState(defaultRole ?? null);
    const options = useMemo(() => {
        if (kind === null)
            return [];
        return getGrantableRoles(kind).map((role) => ({
            role,
            label: iamRoleDisplayName(role),
            description: iamRoleDescription(role),
            value: iamRoleToString(role),
        }));
    }, [kind]);
    const selectedValue = useMemo(() => (selected !== null ? iamRoleToString(selected) : ""), [selected]);
    const select = useCallback((role) => setSelected(role), []);
    const clear = useCallback(() => setSelected(null), []);
    return {
        options,
        selected,
        selectedValue,
        select,
        clear,
        hasOptions: options.length > 0,
    };
}
//# sourceMappingURL=useRoleSelector.js.map