import { AiGenerationState } from "@superblocksteam/library-shared/types";
import { getLogger } from "../../../util/logger.js";
import { APP_RUNTIME_UPDATED_WITHOUT_EDITS, USER_ACCEPTED_DRAFT, USER_CANCELED, AGENT_CANCELED, } from "../clark-fsm.js";
import { sendUserGenerationStateChannel, sendUserMessageChannel, } from "../helpers/peer.js";
export const doIdle = (clark, { chatSessionStore, clarkProfiler }) => {
    const sendUserMessage = sendUserMessageChannel(clark, chatSessionStore);
    const sendUserGenerationState = sendUserGenerationStateChannel(clark);
    return async ({ event }) => {
        switch (event.type) {
            case USER_ACCEPTED_DRAFT:
                if (!event.silently) {
                    const chatmessageId = await chatSessionStore.recordUserAcceptance();
                    if (chatmessageId) {
                        clark.updateContext({
                            chatmessageId,
                        });
                    }
                }
                void sendUserGenerationState({
                    state: AiGenerationState.IDLE,
                    hasError: false,
                });
                break;
            case APP_RUNTIME_UPDATED_WITHOUT_EDITS:
                getLogger().warn("[idle] APP_RUNTIME_UPDATED_WITHOUT_EDITS - generationCompleted will NOT be emitted", {
                    hasSuggestions: clark.context.hasSuggestions,
                });
                await clarkProfiler?.endSession();
                void sendUserGenerationState({
                    state: AiGenerationState.IDLE,
                    hasError: false,
                });
                break;
            case AGENT_CANCELED: {
                const { advice } = event;
                getLogger().info(`[ai-service] Agent canceled with advice: ${advice}`);
                await clarkProfiler.endSession();
                void sendUserMessage({
                    text: advice ?? "",
                    type: "error",
                }, 0, true);
                void sendUserGenerationState({
                    state: AiGenerationState.CANCELLED,
                    hasError: true,
                });
                break;
            }
            case USER_CANCELED: {
                const isInterrupt = clark.context.isInterruptAbort === true;
                getLogger().info(`[ai-service] User canceled (isInterrupt: ${isInterrupt})`);
                await clarkProfiler.endSession();
                if (!isInterrupt) {
                    await sendUserMessage({
                        text: "Canceled.",
                        type: "appGenerationCanceled",
                    }, 0, true);
                    void sendUserGenerationState({
                        state: AiGenerationState.CANCELLED,
                        hasError: false,
                    });
                }
                break;
            }
            default:
                throw new Error(`Unhandled event: ${event}`);
        }
        // reset the context and metadata
        clark.resetContext();
        clark.resetAllMetadata();
    };
};
//# sourceMappingURL=idle.js.map