"use client";
import { useCallback, useState } from "react";
import { useStigmer } from "../hooks";
import { toError } from "../internal/toError";
/**
 * Behavior hook that wraps `iamPolicy.create()` with loading/error
 * state.
 *
 * Creates an IAM policy binding — granting a principal (identity
 * account, team, etc.) a specific role on a resource. The caller
 * provides the full {@link IamPolicySpec} with `principal`, `resource`,
 * and `relation` (the role string, e.g. "admin").
 *
 * @example
 * ```tsx
 * const { create, isCreating, error } = useCreateIamPolicy();
 *
 * await create({
 *   principal: { kind: "identity_account", id: accountId },
 *   resource: { kind: "organization", id: orgId },
 *   relation: "admin",
 * });
 * ```
 */
export function useCreateIamPolicy() {
    const stigmer = useStigmer();
    const [isCreating, setIsCreating] = useState(false);
    const [error, setError] = useState(null);
    const clearError = useCallback(() => setError(null), []);
    const create = useCallback(async (spec) => {
        setIsCreating(true);
        setError(null);
        try {
            return await stigmer.iamPolicy.create(spec);
        }
        catch (err) {
            setError(toError(err));
            throw err;
        }
        finally {
            setIsCreating(false);
        }
    }, [stigmer]);
    return { create, isCreating, error, clearError };
}
//# sourceMappingURL=useCreateIamPolicy.js.map