import { closeGeneratedAnswerFeedbackModal, collapseGeneratedAnswer, dislikeGeneratedAnswer, expandGeneratedAnswer, likeGeneratedAnswer, openGeneratedAnswerFeedbackModal, registerFieldsToIncludeInCitations, sendGeneratedAnswerFeedback, setIsEnabled, setIsVisible, updateResponseFormat, } from '../../../features/generated-answer/generated-answer-actions.js';
import { generatedAnswerReducer as generatedAnswer } from '../../../features/generated-answer/generated-answer-slice.js';
import { loadReducerError } from '../../../utils/errors.js';
import { buildController, } from '../../controller/headless-controller.js';
/**
 * Can be used as a basis for controllers aiming to return a `GeneratedAnswer` controller instance.
 *
 * @param engine - The headless engine.
 * @param props - The configurable `GeneratedAnswer` properties.
 * @returns A `GeneratedAnswer` controller instance.
 *
 * @group Controllers
 * @category GeneratedAnswer
 */
export function buildCoreGeneratedAnswer(engine, analyticsClient, props = {}) {
    if (!loadGeneratedAnswerReducer(engine)) {
        throw loadReducerError;
    }
    const { dispatch } = engine;
    const controller = buildController(engine);
    const getState = () => engine.state;
    const isVisible = props.initialState?.isVisible;
    if (isVisible !== undefined) {
        dispatch(setIsVisible(isVisible));
    }
    const initialResponseFormat = props.initialState?.responseFormat;
    if (initialResponseFormat) {
        dispatch(updateResponseFormat(initialResponseFormat));
    }
    const fieldsToIncludeInCitations = props.fieldsToIncludeInCitations;
    if (fieldsToIncludeInCitations) {
        dispatch(registerFieldsToIncludeInCitations(fieldsToIncludeInCitations));
    }
    const expanded = props.initialState?.expanded;
    if (expanded) {
        dispatch(expandGeneratedAnswer());
    }
    return {
        ...controller,
        get state() {
            return getState().generatedAnswer;
        },
        // TODO: SFINT-6665
        like(answerId) {
            if (!this.state.liked) {
                dispatch(likeGeneratedAnswer());
                dispatch(analyticsClient.logLikeGeneratedAnswer(answerId));
            }
        },
        // TODO: SFINT-6665
        dislike(answerId) {
            if (!this.state.disliked) {
                dispatch(dislikeGeneratedAnswer());
                dispatch(analyticsClient.logDislikeGeneratedAnswer(answerId));
            }
        },
        openFeedbackModal() {
            dispatch(openGeneratedAnswerFeedbackModal());
        },
        closeFeedbackModal() {
            dispatch(closeGeneratedAnswerFeedbackModal());
        },
        sendFeedback(feedback) {
            dispatch(analyticsClient.logGeneratedAnswerFeedback(feedback));
            dispatch(sendGeneratedAnswerFeedback());
        },
        // TODO: SFINT-6665
        logCitationClick(citationId, answerId) {
            dispatch(analyticsClient.logOpenGeneratedAnswerSource(citationId, answerId));
        },
        // TODO: SFINT-6665
        logCitationHover(citationId, citationHoverTimeMs, answerId) {
            dispatch(analyticsClient.logHoverCitation(citationId, citationHoverTimeMs, answerId));
        },
        show() {
            if (!this.state.isVisible) {
                dispatch(setIsVisible(true));
                dispatch(analyticsClient.logGeneratedAnswerShowAnswers());
            }
        },
        hide() {
            if (this.state.isVisible) {
                dispatch(setIsVisible(false));
                dispatch(analyticsClient.logGeneratedAnswerHideAnswers());
            }
        },
        expand() {
            if (!this.state.expanded) {
                dispatch(expandGeneratedAnswer());
                dispatch(analyticsClient.logGeneratedAnswerExpand());
            }
        },
        collapse() {
            if (this.state.expanded) {
                dispatch(collapseGeneratedAnswer());
                dispatch(analyticsClient.logGeneratedAnswerCollapse());
            }
        },
        enable() {
            if (!this.state.isEnabled) {
                dispatch(setIsEnabled(true));
            }
        },
        disable() {
            if (this.state.isEnabled) {
                dispatch(setIsEnabled(false));
            }
        },
        // TODO: SFINT-6665
        logCopyToClipboard(answerId) {
            dispatch(analyticsClient.logCopyGeneratedAnswer(answerId));
        },
        retry() { },
    };
}
function loadGeneratedAnswerReducer(engine) {
    engine.addReducers({ generatedAnswer });
    return true;
}
