import { useCallback } from "react";
import { normalizeAgentId } from "../agentMessageHelpers";
export function isAgentNotFoundLoadError(error) {
    const message = error instanceof Error ? error.message : String(error?.message ?? error);
    return message.includes("404") || message.toLowerCase().includes("not found");
}
export function useAgentLoadErrorHandler(input) {
    const { agentId, messagesRef, actions, setAgent, setError, onAgentNotFound } = input;
    return useCallback((error) => {
        if (isAgentNotFoundLoadError(error)) {
            const normalizedAgentId = normalizeAgentId(agentId);
            const hasExistingMessages = messagesRef.current.some((message) => normalizeAgentId(message.agentId) === normalizedAgentId);
            setAgent(undefined);
            if (!hasExistingMessages) {
                messagesRef.current = [];
                actions.replaceTranscript([]);
            }
            setError(null);
            actions.finishReady();
            onAgentNotFound?.(agentId);
            return;
        }
        console.error("[AgentTerminal] Load error:", error?.message);
        setError("Failed to load agent data");
        actions.applyError("Failed to load agent data");
    }, [actions, agentId, messagesRef, onAgentNotFound, setAgent, setError]);
}
//# sourceMappingURL=useAgentLoadErrorHandler.js.map