import { useCallback } from "react";
import { applyServerUserMessageToTranscript } from "./agentSessionState";
export function useAgentUserMessageSocketHandler(input) {
    const { agent, messagesRef, seenMessageIdsRef, actions } = input;
    return useCallback((messageType, payload) => {
        if (messageType !== "agent:user:message") {
            return false;
        }
        if (!agent) {
            return true;
        }
        const { messageId, content, timestamp, sourceAgentName, sourceAgent, clientMessageId, } = payload;
        const normalizedId = messageId.toLowerCase();
        if (seenMessageIdsRef.current.has(normalizedId)) {
            console.log("[AgentTerminal] Skipping duplicate user message:", messageId);
            return true;
        }
        seenMessageIdsRef.current.add(normalizedId);
        const echo = {
            agentId: agent.id,
            messageId,
            content,
            timestamp,
            clientMessageId,
            sourceAgentName,
            sourceAgent,
        };
        const nextMessages = applyServerUserMessageToTranscript(messagesRef.current, echo);
        if (nextMessages !== messagesRef.current) {
            console.log("[AgentTerminal] Reconciled user message from server:", {
                messageId,
                clientMessageId,
            });
            messagesRef.current = nextMessages;
        }
        actions.reconcileServerUserMessage(echo);
        return true;
    }, [actions, agent, messagesRef, seenMessageIdsRef]);
}
//# sourceMappingURL=useAgentUserMessageSocketHandler.js.map