"use client";
import { useMemo } from "react";
import { create } from "@bufbuild/protobuf";
import { timestampDate } from "@bufbuild/protobuf/wkt";
import { ExecutionPhase as AgentPhase } from "@stigmer/protos/ai/stigmer/agentic/agentexecution/v1/enum_pb";
import { ExecutionPhase as WorkflowPhase } from "@stigmer/protos/ai/stigmer/agentic/workflowexecution/v1/enum_pb";
import { ListAgentExecutionsRequestSchema } from "@stigmer/protos/ai/stigmer/agentic/agentexecution/v1/io_pb";
import { ListWorkflowExecutionsRequestSchema } from "@stigmer/protos/ai/stigmer/agentic/workflowexecution/v1/io_pb";
import { useStigmer } from "../hooks.js";
import { useFetch } from "../internal/useFetch.js";
const EPOCH = new Date(0);
const EMPTY = [];
const MAX_TOTAL = 5;
const PAGE_SIZE = 5;
/**
 * Composition hook that fetches recent failed executions from both
 * agent and workflow domains, normalizes them into {@link DashboardFailedRun}
 * entries, and interleaves them by timestamp (newest first).
 *
 * Returns at most 5 total entries to keep the widget compact.
 *
 * @since Unified Platform Dashboard
 */
export function useDashboardFailedRuns(org) {
    const stigmer = useStigmer();
    const orgVal = org ?? "";
    const agentFetchFn = useMemo(() => orgVal
        ? async () => {
            const resp = await stigmer.agentExecution.list(create(ListAgentExecutionsRequestSchema, {
                pageSize: PAGE_SIZE,
                phase: AgentPhase.EXECUTION_FAILED,
            }));
            return [...resp.entries];
        }
        : null, [stigmer, orgVal]);
    const workflowFetchFn = useMemo(() => orgVal
        ? async () => {
            const resp = await stigmer.workflowExecution.list(create(ListWorkflowExecutionsRequestSchema, {
                pageSize: PAGE_SIZE,
                phase: WorkflowPhase.EXECUTION_FAILED,
            }));
            return [...resp.entries];
        }
        : null, [stigmer, orgVal]);
    const { data: agentFailed, isLoading: agLoading, error: agError } = useFetch(agentFetchFn, [stigmer, orgVal], [], {
        refetchInterval: 60_000,
    });
    const { data: workflowFailed, isLoading: wfLoading, error: wfError } = useFetch(workflowFetchFn, [stigmer, orgVal], [], {
        refetchInterval: 60_000,
    });
    const failedRuns = useMemo(() => {
        if (!agentFailed.length && !workflowFailed.length)
            return EMPTY;
        const agentEntries = agentFailed.map((exec) => {
            const ts = exec.status?.audit?.specAudit?.createdAt;
            return {
                id: exec.metadata?.id ?? "",
                type: "agent_execution",
                name: exec.metadata?.name || "Untitled execution",
                error: exec.status?.error ?? "",
                failedAt: ts ? timestampDate(ts) : EPOCH,
                resourceName: exec.spec?.agentId ?? "",
            };
        });
        const workflowEntries = workflowFailed.map((exec) => {
            const ts = exec.status?.audit?.specAudit?.createdAt;
            return {
                id: exec.metadata?.id ?? "",
                type: "workflow_execution",
                name: exec.metadata?.name || "Untitled execution",
                error: exec.status?.error ?? "",
                failedAt: ts ? timestampDate(ts) : EPOCH,
                resourceName: exec.metadata?.slug ?? "",
            };
        });
        const merged = [...agentEntries, ...workflowEntries];
        merged.sort((a, b) => b.failedAt.getTime() - a.failedAt.getTime());
        return merged.slice(0, MAX_TOTAL);
    }, [agentFailed, workflowFailed]);
    return {
        failedRuns,
        isLoading: agLoading || wfLoading,
        error: agError ?? wfError,
    };
}
//# sourceMappingURL=useDashboardFailedRuns.js.map