import { finalizeTerminalWorkflowToolEntries } from "./chat-session-host-terminal-cleanup.js";
import { ANIMATION_FRAME_MS, STREAMING_RENDER_THROTTLE_MS, } from "./chat-session-host-utils.js";
export function isChatSessionStreaming(state) {
    return state.sdkBusy || state.isStreamingOverride?.() === true;
}
export function isChatSessionBashRunning(state) {
    return state.localBashRunning || state.isBashRunningOverride?.() === true;
}
export function incrementOptimisticUserSignature(state, signature) {
    state.optimisticUserSignatureCounts.set(signature, (state.optimisticUserSignatureCounts.get(signature) ?? 0) + 1);
}
export function decrementOptimisticUserSignature(state, signature) {
    const count = state.optimisticUserSignatureCounts.get(signature) ?? 0;
    if (count <= 1)
        state.optimisticUserSignatureCounts.delete(signature);
    else
        state.optimisticUserSignatureCounts.set(signature, count - 1);
}
export function syncChatSessionAnimationTick(state) {
    const shouldAnimate = isChatSessionStreaming(state) ||
        (state.sdkBusy && state.liveChat.pendingToolIds().length > 0);
    if (shouldAnimate && !state.animationTimer) {
        state.animationTimer = setInterval(() => {
            state.requestRender?.();
        }, ANIMATION_FRAME_MS);
        state.animationTimer.unref?.();
        return;
    }
    if (!shouldAnimate && state.animationTimer) {
        clearInterval(state.animationTimer);
        state.animationTimer = undefined;
    }
}
export function clearChatSessionBusyForTerminalWorkflowStage(state) {
    state.sdkBusy = false;
    state.workingMessage = undefined;
    if (finalizeTerminalWorkflowToolEntries(state.transcript)) {
        state.transcriptComponent.invalidate();
    }
    state.liveChat.clearPendingTools();
    state.statusMessage = "";
    syncChatSessionAnimationTick(state);
}
export function disposeChatSession(state) {
    if (state.animationTimer) {
        clearInterval(state.animationTimer);
        state.animationTimer = undefined;
    }
    if (state.renderThrottleTimer) {
        clearTimeout(state.renderThrottleTimer);
        state.renderThrottleTimer = undefined;
    }
    state.transcriptComponent.invalidate();
    state.editor = undefined;
}
export function notifyChatSessionWarning(state, message) {
    state.statusMessage = message;
    state.showWarning?.(message);
    state.requestRender?.();
}
export function notifyChatSessionStatus(state, message) {
    state.statusMessage = message;
    state.showStatus?.(message);
    state.requestRender?.();
}
export function requiredChatSessionCommand(state, name) {
    switch (name) {
        case "prompt":
            return async (text) => {
                if (!state.commands.prompt)
                    throw new Error("no prompt command configured for this chat session");
                await state.commands.prompt(text ?? "");
            };
        case "steer":
            return async (text) => {
                if (!state.commands.steer)
                    throw new Error("no steer command configured for this chat session");
                await state.commands.steer(text ?? "");
            };
        case "followUp":
            return async (text) => {
                if (!state.commands.followUp)
                    throw new Error("no followUp command configured for this chat session");
                await state.commands.followUp(text ?? "");
            };
        case "resume":
            return async (text) => {
                if (!state.commands.resume)
                    throw new Error("no resume command configured for this chat session");
                await state.commands.resume(text);
            };
    }
}
export function afterChatSessionEvent(state, changed) {
    syncChatSessionAnimationTick(state);
    if (!changed)
        return;
    requestChatSessionEventRender(state);
}
function requestChatSessionEventRender(state) {
    if (!isChatSessionStreaming(state)) {
        state.requestRender?.();
        return;
    }
    if (state.renderThrottleTimer)
        return;
    state.renderThrottleTimer = setTimeout(() => {
        state.renderThrottleTimer = undefined;
        state.requestRender?.();
    }, STREAMING_RENDER_THROTTLE_MS);
    state.renderThrottleTimer.unref?.();
}
//# sourceMappingURL=chat-session-host-runtime.js.map