import { useCallback, useEffect, useMemo, useRef, } from "react";
import { buildPlaceholderAgentDetails, normalizeAgentId, } from "../agentMessageHelpers";
import { selectIsExecuting, selectReadOnly } from "./agentSessionSelectors";
import { useAgentPendingApprovals } from "./useAgentPendingApprovals";
import { useAgentSessionErrorBridge } from "./useAgentSessionErrorBridge";
export function useAgentSessionFacade(input) {
    const { agentStub, agentSession } = input;
    const agent = agentSession.state.agent;
    const agentStateRef = useRef(agent);
    const setAgent = useCallback((nextAgent) => {
        const currentAgent = agentStateRef.current;
        const resolvedAgent = typeof nextAgent === "function" ? nextAgent(currentAgent) : nextAgent;
        if (Object.is(resolvedAgent, currentAgent)) {
            return;
        }
        agentStateRef.current = resolvedAgent;
        agentSession.actions.replaceAgent(resolvedAgent);
    }, [agentSession.actions]);
    const messagesRef = useRef([]);
    const rawMessages = agentSession.state.transcript;
    const messages = useMemo(() => {
        const currentAgentId = normalizeAgentId(agentStub.id);
        return rawMessages.filter((message) => normalizeAgentId(message.agentId) === currentAgentId);
    }, [agentStub.id, rawMessages]);
    const addOptimisticTranscriptMessage = useCallback((message) => {
        if (!messagesRef.current.some((existing) => existing.id === message.id && existing.role === "user")) {
            messagesRef.current = [...messagesRef.current, message];
        }
        agentSession.actions.addOptimisticUserMessage(message);
    }, [agentSession.actions]);
    const removeOptimisticTranscriptMessage = useCallback((messageId) => {
        messagesRef.current = messagesRef.current.filter((message) => message.id !== messageId);
        agentSession.actions.removeOptimisticUserMessage(messageId);
    }, [agentSession.actions]);
    const isLoading = agentSession.state.loadStatus === "loading";
    const isConnecting = agentSession.state.run.isConnecting;
    const isSubmitting = agentSession.state.run.isSubmitting;
    const isWaitingForResponse = agentSession.state.run.isWaitingForResponse;
    const isStopGuardActive = agentSession.state.run.stopGuardActive;
    const isAgentThinking = agentSession.state.run.isAgentThinking;
    const lastRunStatusReason = agentSession.state.run.lastStatusReason;
    const isExecuting = selectIsExecuting(agentSession.state);
    const readOnly = selectReadOnly(agentSession.state);
    const { error, setError } = useAgentSessionErrorBridge({
        state: agentSession.state,
        actions: agentSession.actions,
    });
    const { allPendingApprovals, hasPendingApprovals } = useAgentPendingApprovals(agentSession.state);
    const setIsConnecting = useCallback((isConnecting) => agentSession.actions.patchRunState({ isConnecting }), [agentSession.actions]);
    const setIsSubmitting = useCallback((isSubmitting) => agentSession.actions.patchRunState({ isSubmitting }), [agentSession.actions]);
    const setIsWaitingForResponse = useCallback((isWaitingForResponse) => agentSession.actions.patchRunState({ isWaitingForResponse }), [agentSession.actions]);
    const setIsStopGuardActive = useCallback((stopGuardActive) => agentSession.actions.patchRunState({ stopGuardActive }), [agentSession.actions]);
    const setIsAgentThinking = useCallback((isAgentThinking) => agentSession.actions.patchRunState({ isAgentThinking }), [agentSession.actions]);
    const setLastRunStatusReason = useCallback((lastStatusReason) => agentSession.actions.patchRunState({ lastStatusReason }), [agentSession.actions]);
    const agentMetadata = agentSession.state.metadata;
    const operationHistory = agentSession.state.operationHistory;
    const queuedPrompts = agentSession.state.queuedPrompts;
    const costLimitExceeded = agentSession.state.costLimitExceeded;
    const contextWindowStatus = agentSession.state.contextWindowStatus;
    const liveTotals = agentSession.state.liveTotals;
    const agentMetadataRef = useRef(agentMetadata);
    const setAgentMetadata = useCallback((nextMetadata) => {
        const currentMetadata = agentMetadataRef.current;
        const resolvedMetadata = typeof nextMetadata === "function"
            ? nextMetadata(currentMetadata)
            : nextMetadata;
        if (Object.is(resolvedMetadata, currentMetadata)) {
            return;
        }
        agentMetadataRef.current = resolvedMetadata;
        agentSession.actions.replaceMetadata(resolvedMetadata);
    }, [agentSession.actions]);
    useEffect(() => {
        setAgent((prev) => {
            if (prev?.id === agentStub.id)
                return prev;
            return buildPlaceholderAgentDetails(agentStub);
        });
    }, [agentStub, setAgent]);
    useEffect(() => {
        messagesRef.current = messages;
    }, [messages]);
    useEffect(() => {
        agentStateRef.current = agent;
    }, [agent]);
    useEffect(() => {
        agentMetadataRef.current = agentMetadata;
    }, [agentMetadata]);
    return {
        state: agentSession.state,
        actions: agentSession.actions,
        commands: agentSession.commands,
        bindCommands: agentSession.bindCommands,
        agent,
        agentStateRef,
        setAgent,
        messages,
        messagesRef,
        addOptimisticTranscriptMessage,
        removeOptimisticTranscriptMessage,
        isLoading,
        isConnecting,
        isSubmitting,
        isWaitingForResponse,
        isStopGuardActive,
        isAgentThinking,
        lastRunStatusReason,
        isExecuting,
        readOnly,
        error,
        setError,
        allPendingApprovals,
        hasPendingApprovals,
        setIsConnecting,
        setIsSubmitting,
        setIsWaitingForResponse,
        setIsStopGuardActive,
        setIsAgentThinking,
        setLastRunStatusReason,
        agentMetadata,
        agentMetadataRef,
        setAgentMetadata,
        operationHistory,
        queuedPrompts,
        costLimitExceeded,
        contextWindowStatus,
        liveTotals,
        replaceQueuedPrompts: agentSession.actions.replaceQueuedPrompts,
    };
}
//# sourceMappingURL=useAgentSessionFacade.js.map