"use client";
import { create } from "@bufbuild/protobuf";
import { ListWorkflowExecutionsRequestSchema, ListWorkflowExecutionsByWorkflowRequestSchema, } from "@stigmer/protos/ai/stigmer/agentic/workflowexecution/v1/io_pb";
import { useStigmer } from "../hooks";
import { useFetch } from "../internal/useFetch";
const INITIAL_DATA = {
    executions: [],
    totalPages: 0,
};
/**
 * Data hook that fetches a paginated list of workflow executions.
 *
 * When `workflowId` is provided, fetches executions for that specific
 * workflow via `listByWorkflow()`. When omitted, fetches all executions
 * across all workflows via `list()`.
 *
 * @example
 * ```tsx
 * // All executions
 * const { executions, isLoading } = useWorkflowExecutionList();
 *
 * // Executions for a specific workflow
 * const { executions } = useWorkflowExecutionList({
 *   workflowId: workflow.metadata?.id,
 *   pageSize: 10,
 * });
 * ```
 */
export function useWorkflowExecutionList(options) {
    const stigmer = useStigmer();
    const pageSize = options?.pageSize ?? 20;
    const pageToken = options?.pageToken ?? "";
    const workflowId = options?.workflowId ?? null;
    const fetchFn = async () => {
        if (workflowId) {
            const resp = await stigmer.workflowExecution.listByWorkflow(create(ListWorkflowExecutionsByWorkflowRequestSchema, { workflowId, pageSize, pageToken }));
            return {
                executions: [...resp.entries],
                totalPages: resp.totalPages,
            };
        }
        const resp = await stigmer.workflowExecution.list(create(ListWorkflowExecutionsRequestSchema, { pageSize, pageToken }));
        return {
            executions: [...resp.entries],
            totalPages: resp.totalPages,
        };
    };
    const { data, isLoading, isRefetching, error, refetch, } = useFetch(fetchFn, [stigmer, workflowId, pageSize, pageToken], INITIAL_DATA);
    return {
        executions: data.executions,
        totalPages: data.totalPages,
        isLoading,
        isRefetching,
        error,
        refetch,
    };
}
//# sourceMappingURL=useWorkflowExecutionList.js.map