"use client";
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { memo, useMemo } from "react";
import { create } from "@bufbuild/protobuf";
import { ExecutionPhase } from "@stigmer/protos/ai/stigmer/agentic/workflowexecution/v1/enum_pb";
import { ListWorkflowExecutionsRequestSchema } from "@stigmer/protos/ai/stigmer/agentic/workflowexecution/v1/io_pb";
import { cn } from "@stigmer/theme";
import { useStigmer } from "../hooks.js";
import { useFetch } from "../internal/useFetch.js";
import { useWorkflowDashboardSummary, } from "./useWorkflowDashboardSummary.js";
import { usePendingApprovals } from "./usePendingApprovals.js";
import { ExecutionSummaryWidget } from "./ExecutionSummaryWidget.js";
import { PendingApprovalsWidget } from "./PendingApprovalsWidget.js";
import { FailedRunsWidget } from "./FailedRunsWidget.js";
const FAILED_LIST_INITIAL = [];
/**
 * Composed dashboard widget that aggregates execution KPIs,
 * pending approvals, and recent failures into a responsive layout.
 *
 * Composes three sub-widgets:
 * - **ExecutionSummaryWidget** — phase counts, cost, duration
 * - **PendingApprovalsWidget** — human_input tasks awaiting decisions
 * - **FailedRunsWidget** — recent failed executions (from existing list API)
 *
 * All data fetching is internal. The consumer provides org context and
 * navigation callbacks.
 *
 * @example
 * ```tsx
 * <WorkflowDashboard
 *   org="acme"
 *   onApprovalClick={(id) => navigate(`/workflows/executions/${id}`)}
 *   onFailedRunClick={(id) => navigate(`/workflows/executions/${id}`)}
 * />
 * ```
 */
export const WorkflowDashboard = memo(function WorkflowDashboard({ org, timeWindow, onApprovalClick, onFailedRunClick, className, }) {
    const stigmer = useStigmer();
    const { summary, isLoading: summaryLoading } = useWorkflowDashboardSummary({
        org,
        timeWindow,
        refetchInterval: 60_000,
    });
    const { approvals, totalCount: approvalCount, isLoading: approvalsLoading, } = usePendingApprovals({ org, refetchInterval: 30_000 });
    const fetchFailedFn = useMemo(() => org
        ? async () => {
            const resp = await stigmer.workflowExecution.list(create(ListWorkflowExecutionsRequestSchema, {
                pageSize: 5,
                phase: ExecutionPhase.EXECUTION_FAILED,
            }));
            return [...resp.entries];
        }
        : null, [stigmer, org]);
    const { data: failedRuns, isLoading: failedLoading } = useFetch(fetchFailedFn, [stigmer, org], FAILED_LIST_INITIAL, {
        refetchInterval: 60_000,
    });
    return (_jsxs("section", { "aria-label": "Workflow dashboard", className: cn("space-y-6", className), children: [_jsx(ExecutionSummaryWidget, { summary: summary, isLoading: summaryLoading }), _jsxs("div", { className: "grid gap-6 lg:grid-cols-2", children: [_jsx(PendingApprovalsWidget, { approvals: approvals, totalCount: approvalCount, isLoading: approvalsLoading, onReviewClick: onApprovalClick }), _jsx(FailedRunsWidget, { executions: failedRuns, isLoading: failedLoading, onViewClick: onFailedRunClick })] })] }));
});
//# sourceMappingURL=WorkflowDashboard.js.map