/**
 * @license
 * Copyright 2025 HopCode Team
 * SPDX-License-Identifier: Apache-2.0
 */
import { StreamingState } from '../types.js';
import { useTimer } from './useTimer.js';
import { usePhraseCycler } from './usePhraseCycler.js';
import { useState, useEffect, useRef } from 'react';
export const useLoadingIndicator = (streamingState, customWittyPhrases, currentCandidatesTokens) => {
    const [timerResetKey, setTimerResetKey] = useState(0);
    const isTimerActive = streamingState === StreamingState.Responding;
    const elapsedTimeFromTimer = useTimer(isTimerActive, timerResetKey);
    const isPhraseCyclingActive = streamingState === StreamingState.Responding;
    const isWaiting = streamingState === StreamingState.WaitingForConfirmation;
    const currentLoadingPhrase = usePhraseCycler(isPhraseCyclingActive, isWaiting, customWittyPhrases);
    const [retainedElapsedTime, setRetainedElapsedTime] = useState(0);
    const [taskStartTokens, setTaskStartTokens] = useState(0);
    const prevStreamingStateRef = useRef(null);
    useEffect(() => {
        if (prevStreamingStateRef.current === StreamingState.WaitingForConfirmation &&
            streamingState === StreamingState.Responding) {
            setTimerResetKey((prevKey) => prevKey + 1);
            setRetainedElapsedTime(0);
            setTaskStartTokens(currentCandidatesTokens ?? 0);
        }
        else if (streamingState === StreamingState.Idle &&
            prevStreamingStateRef.current === StreamingState.Responding) {
            setTimerResetKey((prevKey) => prevKey + 1);
            setRetainedElapsedTime(0);
            setTaskStartTokens(0);
        }
        else if (streamingState === StreamingState.Responding &&
            prevStreamingStateRef.current !== StreamingState.Responding) {
            setTaskStartTokens(currentCandidatesTokens ?? 0);
        }
        else if (streamingState === StreamingState.WaitingForConfirmation) {
            setRetainedElapsedTime(elapsedTimeFromTimer);
        }
        prevStreamingStateRef.current = streamingState;
    }, [streamingState, elapsedTimeFromTimer, currentCandidatesTokens]);
    return {
        elapsedTime: streamingState === StreamingState.WaitingForConfirmation
            ? retainedElapsedTime
            : elapsedTimeFromTimer,
        currentLoadingPhrase,
        taskStartTokens,
    };
};
//# sourceMappingURL=useLoadingIndicator.js.map