"use client";
import { useCallback, useState } from "react";
import { useStigmer } from "../hooks";
import { toError } from "../internal/toError";
/**
 * Behavior hook that wraps `invitation.create()` with loading and
 * error state.
 *
 * Creates an invitation resource. The caller provides an
 * {@link InvitationInput} with `name`, `org`, `role`, `expiresAt`,
 * and optionally `maxRedemptions` and `label`.
 *
 * The returned {@link Invitation} contains the server-generated token
 * in `status?.token`. Use this to build the invite URL:
 * `https://<host>/invite/<token>`.
 *
 * Creating an invitation requires `can_grant_access` on the
 * organization — typically limited to admins and owners.
 *
 * @example
 * ```tsx
 * const { create, isCreating, error } = useCreateInvitation();
 *
 * const invitation = await create({
 *   name: "engineering-link",
 *   org: "acme",
 *   role: IamRole.VIEWER,
 *   expiresAt: new Date(Date.now() + 30 * 86_400_000),
 *   label: "Engineering team invite",
 * });
 * const inviteUrl = `${window.location.origin}/invite/${invitation.status?.token}`;
 * ```
 */
export function useCreateInvitation() {
    const stigmer = useStigmer();
    const [isCreating, setIsCreating] = useState(false);
    const [error, setError] = useState(null);
    const clearError = useCallback(() => setError(null), []);
    const create = useCallback(async (input) => {
        setIsCreating(true);
        setError(null);
        try {
            return await stigmer.invitation.create(input);
        }
        catch (err) {
            setError(toError(err));
            throw err;
        }
        finally {
            setIsCreating(false);
        }
    }, [stigmer]);
    return { create, isCreating, error, clearError };
}
//# sourceMappingURL=useCreateInvitation.js.map