import { SocketService } from "./socket-service";
import { generateUUID, createChatMessage, updateChatHistory, removeLastMessage, isMaxQuestionsReached, validateFormInput } from "./ctaHelpers";
export const chatHelpers = {
    async handleChatSubmission(params) {
        const { formData, chatHistory, questionCounter, ctaSettings, dataMode, imgFiles, onQuestionCounterIncrement, onTimerStart, onStreamStart } = params;
        // Validate input
        if (validateFormInput(formData.search, imgFiles)) {
            return { success: false, chatHistory, aiMessageId: null, thumbsId: null, imgFiles: [] };
        }
        // Increment question counter
        onQuestionCounterIncrement();
        // Start timer if needed
        if (chatHistory.length === 0 && (ctaSettings === null || ctaSettings === void 0 ? void 0 : ctaSettings.autoResetTimer) > 0) {
            onTimerStart();
        }
        // Initialize socket if needed
        if (chatHistory.length === 0) {
            const newSocketService = SocketService.createInstance(dataMode, params.dataKey);
            params.onSocketServiceCreate(newSocketService);
        }
        // Create user message
        const userMessage = this.createUserMessage(formData.search, formData.displayText, imgFiles);
        const updatedHistory = updateChatHistory(chatHistory, userMessage);
        // Create AI message IDs
        const aiMessageId = `ai-message-${generateUUID()}`;
        const thumbsId = generateUUID();
        // Add pending AI message
        const pendingAIMessage = this.createAIMessage(`<div class="animate-ping">...</div>`, aiMessageId, "pending");
        const historyWithPending = updateChatHistory(updatedHistory, pendingAIMessage);
        // Simulate AI response (replace with actual AI call)
        const response = { answer: "", formModel: null, insight_id: null };
        // Remove pending message
        const historyWithoutPending = removeLastMessage(historyWithPending);
        // Check max questions
        const maxQuestionsReached = isMaxQuestionsReached(questionCounter, ctaSettings === null || ctaSettings === void 0 ? void 0 : ctaSettings.maxQuestion);
        if (maxQuestionsReached) {
            response.answer = ctaSettings.msgLimit;
        }
        // Create final AI message
        const finalAIMessage = this.createFinalAIMessage(response, aiMessageId, thumbsId);
        const finalHistory = updateChatHistory(historyWithoutPending, finalAIMessage);
        // Start stream
        if (maxQuestionsReached) {
            onStreamStart({
                search: formData.search,
                maxQuestion: true,
                chatData: finalHistory[finalHistory.length - 1],
            });
        }
        else {
            onStreamStart({ search: formData.search, imgFiles });
        }
        return {
            success: true,
            chatHistory: finalHistory,
            aiMessageId,
            thumbsId,
            imgFiles: imgFiles || [],
        };
    },
    createUserMessage(message, displayText, imgFiles) {
        return createChatMessage(displayText !== "" ? displayText : message, "user", "success", imgFiles);
    },
    createAIMessage(message, id, status = "pending") {
        return createChatMessage(message, "ai", status, undefined, id);
    },
    createFinalAIMessage(response, aiMessageId, thumbsId) {
        return createChatMessage(response === null || response === void 0 ? void 0 : response.answer, "ai", "success", undefined, aiMessageId, response === null || response === void 0 ? void 0 : response.insight_id, thumbsId, response === null || response === void 0 ? void 0 : response.formModel);
    },
};
