"use client";
import { useRef } from "react";
import { create } from "@bufbuild/protobuf";
import { ListAgentInstancesRequestSchema } from "@stigmer/protos/ai/stigmer/agentic/agentinstance/v1/io_pb";
import { useStigmer } from "../hooks";
import { useFetch } from "../internal/useFetch";
/**
 * Data hook that fetches a paginated list of {@link AgentInstance} entries
 * for a given organization, with optional label filtering.
 *
 * Pass `null` as `org` to skip fetching (stable no-op). When the org
 * or labels change, the previous in-flight request is discarded and a
 * fresh fetch begins. Call `refetch()` to re-query after mutations.
 *
 * This is a Layer 1 building-block hook for the **Environment Flow**.
 * For the managed "personal agent instance" convenience, see
 * {@link usePersonalAgentInstance}.
 *
 * @example
 * ```tsx
 * const { agentInstances, isLoading } = useAgentInstanceList("acme", {
 *   "stigmer.ai/personal": "true",
 * });
 * ```
 */
export function useAgentInstanceList(org, labels) {
    const stigmer = useStigmer();
    const labelsRef = useRef(labels);
    if (labels !== labelsRef.current &&
        JSON.stringify(labels) !== JSON.stringify(labelsRef.current)) {
        labelsRef.current = labels;
    }
    const stableLabels = labelsRef.current;
    const fetchFn = org
        ? async () => {
            const result = await stigmer.agentInstance.list(create(ListAgentInstancesRequestSchema, {
                org,
                labels: stableLabels ?? {},
            }));
            return {
                agentInstances: result.items,
                totalCount: result.totalCount,
            };
        }
        : null;
    const { data, isLoading, isRefetching, error, refetch } = useFetch(fetchFn, [org, stableLabels, stigmer], { agentInstances: [], totalCount: 0 });
    return {
        agentInstances: data.agentInstances,
        totalCount: data.totalCount,
        isLoading,
        isRefetching,
        error,
        refetch,
    };
}
//# sourceMappingURL=useAgentInstanceList.js.map