/**
 * @license
 * Copyright 2025 Vybestack LLC
 * SPDX-License-Identifier: Apache-2.0
 */
import { useState, useRef, useCallback, useEffect } from 'react';
import { ApprovalMode, } from '@vybestack/llxprt-code-core';
import { useTodoContext } from '../contexts/TodoContext.js';
/**
 * React hook for todo continuation - monitors stream completion and triggers continuation
 * prompts when active todos exist but no tool calls were made.
 * [REQ-001] Todo Continuation Detection, [REQ-002] Continuation Prompting
 */
export const useTodoContinuation = (geminiClient, config, isResponding, onDebugMessage) => {
    const [continuationState, setContinuationState] = useState({
        isActive: false,
        attemptCount: 0,
    });
    // Track if a continuation is currently in progress to prevent rapid firing
    const continuationInProgressRef = useRef(false);
    const todoContext = useTodoContext();
    const abortControllerRef = useRef(undefined);
    const continuationTimeoutRef = useRef(undefined);
    // Private helper methods
    const _evaluateContinuationConditions = useCallback((hadToolCalls) => {
        const ephemeralSettings = config.getEphemeralSettings();
        const isEnabled = ephemeralSettings['todo-continuation'] === true;
        const hasActiveTodos = todoContext.todos.some((todo) => todo.status === 'pending' || todo.status === 'in_progress');
        return {
            streamCompleted: true,
            noToolCallsMade: !hadToolCalls,
            hasActiveTodos,
            continuationEnabled: isEnabled,
            notAlreadyContinuing: !continuationState.isActive,
        };
    }, [config, todoContext.todos, continuationState.isActive]);
    const _shouldTriggerContinuation = useCallback((conditions) => {
        // Don't trigger if AI is currently responding
        if (isResponding) {
            return false;
        }
        return (conditions.streamCompleted &&
            conditions.noToolCallsMade &&
            conditions.hasActiveTodos &&
            conditions.continuationEnabled &&
            conditions.notAlreadyContinuing);
    }, [isResponding]);
    const _findMostRelevantActiveTodo = useCallback((todos) => {
        // Priority order: in_progress > pending
        const inProgressTodos = todos.filter((todo) => todo.status === 'in_progress');
        if (inProgressTodos.length > 0) {
            return inProgressTodos[0];
        }
        const pendingTodos = todos.filter((todo) => todo.status === 'pending');
        if (pendingTodos.length > 0) {
            return pendingTodos[0];
        }
        return null;
    }, []);
    const _generateContinuationPrompt = useCallback((todo) => {
        const isYoloMode = config.getApprovalMode() === ApprovalMode.YOLO;
        if (isYoloMode) {
            return `Continue to proceed with the active task without waiting for confirmation: "${todo.content}"`;
        }
        return `Please continue working on the following task: "${todo.content}"`;
    }, [config]);
    const _triggerContinuation = useCallback((activeTodo) => {
        // Prevent multiple rapid continuations
        if (continuationInProgressRef.current) {
            return;
        }
        // Mark continuation as in progress
        continuationInProgressRef.current = true;
        // Update state to indicate continuation is active
        setContinuationState((prev) => ({
            isActive: true,
            taskDescription: activeTodo.content,
            attemptCount: prev.attemptCount + 1,
            lastPromptTime: new Date(),
        }));
        // Generate continuation prompt
        const continuationPrompt = _generateContinuationPrompt(activeTodo);
        // Send out-of-band prompt (ephemeral, not stored in history)
        // Fire and forget - don't await this
        geminiClient.sendMessageStream(continuationPrompt, { ephemeral: true })
            .catch((error) => {
            onDebugMessage(`[TodoContinuation] Error sending continuation prompt: ${error instanceof Error ? error.message : String(error)}`);
            // Reset state on error
            setContinuationState((prev) => ({
                isActive: false,
                attemptCount: prev.attemptCount,
                lastPromptTime: prev.lastPromptTime,
                taskDescription: prev.taskDescription,
            }));
        })
            .finally(() => {
            // Always reset the in-progress flag
            continuationInProgressRef.current = false;
        });
    }, [geminiClient, _generateContinuationPrompt, onDebugMessage]);
    // Public interface methods
    const handleStreamCompleted = useCallback((hadToolCalls) => {
        // Reset continuation state if tool calls were made
        if (hadToolCalls) {
            setContinuationState((prev) => ({
                ...prev,
                isActive: false,
            }));
            return;
        }
        // Check all continuation conditions
        const conditions = _evaluateContinuationConditions(hadToolCalls);
        if (!_shouldTriggerContinuation(conditions)) {
            return;
        }
        // Find the most relevant active todo
        const activeTodo = _findMostRelevantActiveTodo(todoContext.todos);
        if (!activeTodo ||
            !activeTodo.content ||
            activeTodo.content.trim() === '') {
            return;
        }
        // Prevent multiple rapid continuations
        if (continuationState.isActive || continuationInProgressRef.current) {
            return;
        }
        // Start continuation process
        _triggerContinuation(activeTodo);
    }, [
        _evaluateContinuationConditions,
        _shouldTriggerContinuation,
        _findMostRelevantActiveTodo,
        _triggerContinuation,
        todoContext.todos,
        continuationState.isActive,
    ]);
    const handleTodoPause = useCallback((reason) => ({
        type: 'pause',
        reason,
        message: `Task paused: ${reason}`,
    }), []);
    useEffect(() => () => {
        abortControllerRef.current?.abort();
        clearTimeout(continuationTimeoutRef.current);
    }, []);
    return { handleStreamCompleted, continuationState, handleTodoPause };
};
//# sourceMappingURL=useTodoContinuation.js.map