"use client";
import { create } from "@bufbuild/protobuf";
import { ListResourceAccessInputSchema, } from "@stigmer/protos/ai/stigmer/iam/iampolicy/v1/io_pb";
import { ApiResourceRefSchema } from "@stigmer/protos/ai/stigmer/iam/iampolicy/v1/spec_pb";
import { useStigmer } from "../hooks";
import { useFetch } from "../internal/useFetch";
/**
 * Data hook that fetches the list of principals and their roles on a
 * resource.
 *
 * Wraps `iamPolicy.listResourceAccessByPrincipal()`. Returns each
 * principal with full display information (`ApiResourceRefView`) and
 * all their role grants, optionally including roles inherited from
 * parent resources.
 *
 * Pass `null` as `resource` to skip fetching (stable no-op).
 *
 * **Generic** — works for organizations, agents, environments, or any
 * resource kind that has IAM policies.
 *
 * @param resource - The resource to query access for, or `null` to skip.
 * @param options  - Optional configuration.
 *
 * @example
 * ```tsx
 * const { members, isLoading } = useResourceAccess(
 *   { kind: "organization", id: orgId },
 *   { includeInherited: true },
 * );
 * ```
 */
export function useResourceAccess(resource, options) {
    const stigmer = useStigmer();
    const kind = resource?.kind ?? null;
    const id = resource?.id ?? null;
    const includeInherited = options?.includeInherited ?? false;
    const { data: members, isLoading, isRefetching, error, refetch } = useFetch(kind && id
        ? () => {
            const input = create(ListResourceAccessInputSchema, {
                resource: create(ApiResourceRefSchema, { kind, id }),
                includeInherited,
            });
            return stigmer.iamPolicy
                .listResourceAccessByPrincipal(input)
                .then((r) => [...r.entries]);
        }
        : null, [kind, id, includeInherited, stigmer], []);
    return { members, isLoading, isRefetching, error, refetch };
}
//# sourceMappingURL=useResourceAccess.js.map