"use client";
import { useRef } from "react";
import { create } from "@bufbuild/protobuf";
import { ListEnvironmentsRequestSchema } from "@stigmer/protos/ai/stigmer/agentic/environment/v1/io_pb";
import { useStigmer } from "../hooks";
import { useFetch } from "../internal/useFetch";
/**
 * Data hook that fetches a paginated list of {@link Environment} 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.
 *
 * Secret values in returned environments are redacted server-side.
 *
 * This is a Layer 1 building-block hook for the **Environment Flow**.
 * For the managed "personal environment" convenience, see
 * {@link usePersonalEnvironment}.
 *
 * @example
 * ```tsx
 * const { environments, isLoading } = useEnvironmentList("acme", {
 *   "stigmer.ai/personal": "true",
 * });
 * ```
 */
export function useEnvironmentList(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.environment.list(create(ListEnvironmentsRequestSchema, {
                org,
                labels: stableLabels ?? {},
            }));
            return {
                environments: result.items,
                totalCount: result.totalCount,
            };
        }
        : null;
    const { data, isLoading, isRefetching, error, refetch } = useFetch(fetchFn, [org, stableLabels, stigmer], { environments: [], totalCount: 0 });
    return {
        environments: data.environments,
        totalCount: data.totalCount,
        isLoading,
        isRefetching,
        error,
        refetch,
    };
}
//# sourceMappingURL=useEnvironmentList.js.map