import { updateChatHistory } from "./ctaHelpers";
export const ctaEventHandlers = {
    handleKeyDown(ev, openModal, closeModal, allowClose) {
        if (ev.metaKey && ev.key === "k") {
            openModal();
        }
        if (ev.key === "Escape") {
            allowClose ? closeModal() : "";
        }
    },
    handleSurvey(_state, handlers) {
        if (_state.detail.type === "button") {
            handlers.formData.displayText = "";
            handlers.handleSubmit(null, _state.detail.results, _state.detail.type, false, null);
            handlers.buttonClicked.emit(_state.detail.results);
        }
        else if (_state.detail.type === "carousel") {
            handlers.chatHistory = [...handlers.chatHistory, _state.detail.results];
        }
        else if (_state.detail.type === "update") {
            const idx = handlers.chatHistory.findIndex(item => item.id === _state.detail.results.id);
            handlers.chatHistory = updateChatHistory(handlers.chatHistory, _state.detail.results, idx);
        }
        else if (_state.detail.type === "sendMessage") {
            if (_state.detail.results.length > 1) {
                handlers.sendMessage(_state.detail.results[0], _state.detail.results[1]);
            }
            else {
                handlers.sendMessage(_state.detail.results[0]);
            }
        }
        else if (_state.detail.type === "sendJSON") {
            handlers.sendJSON(_state.detail.results[0], _state.detail.results[1]);
        }
        else {
            const surveyData = _state.detail;
            const jsonData = `This JSON is from a form submission [${surveyData.keyName}]: ${JSON.stringify(surveyData.results, null, 2)}. There is no need to show the form [${surveyData.keyName}] again.`;
            handlers.formData.search = jsonData;
            handlers.formData.displayText = surveyData.completeText;
            handlers.handleSubmit(null, handlers.formData.search, "ask-ai", false, null);
        }
    },
    handleFormSubmitted(event, formSubmitted) {
        event.stopPropagation();
        formSubmitted.emit(event.detail.data);
    },
};
