import { citationDocumentIdentifier, makeInsightAnalyticsActionFactory, partialCitationInformation, } from '../analytics/analytics-utils.js';
import { SearchPageEvents } from '../analytics/search-action-cause.js';
import { getCaseContextAnalyticsMetadata } from '../case-context/case-context-state.js';
import { parseEvaluationDetails, } from './generated-answer-analytics-actions.js';
import { citationSourceSelector, generativeQuestionAnsweringIdSelector, } from './generated-answer-selectors.js';
//TODO: SFINT-5435
export const logRetryGeneratedAnswer = () => makeInsightAnalyticsActionFactory(SearchPageEvents.retryGeneratedAnswer)('analytics/generatedAnswer/retry', (client, state) => client.logRetryGeneratedAnswer(getCaseContextAnalyticsMetadata(state.insightCaseContext)));
export const logOpenGeneratedAnswerSource = (citationId) => makeInsightAnalyticsActionFactory(SearchPageEvents.openGeneratedAnswerSource)({
    prefix: 'analytics/generatedAnswer/openAnswerSource',
    __legacy__getBuilder: (client, state) => {
        const generativeQuestionAnsweringId = generativeQuestionAnsweringIdSelector(state);
        const citation = citationSourceSelector(state, citationId);
        if (!generativeQuestionAnsweringId || !citation) {
            return null;
        }
        return client.logGeneratedAnswerCitationClick(partialCitationInformation(citation, state), {
            generativeQuestionAnsweringId,
            citationId: citation.id,
            documentId: citationDocumentIdentifier(citation),
        }, getCaseContextAnalyticsMetadata(state.insightCaseContext));
    },
    analyticsType: 'Rga.CitationClick',
    analyticsPayloadBuilder: (state) => {
        const citation = citationSourceSelector(state, citationId);
        const generativeQuestionAnsweringId = generativeQuestionAnsweringIdSelector(state);
        return {
            answerId: generativeQuestionAnsweringId ?? '',
            citationId,
            itemMetadata: {
                uniqueFieldName: 'permanentid',
                uniqueFieldValue: citation?.permanentid ?? '',
                title: citation?.title,
                url: citation?.clickUri,
            },
        };
    },
});
export const logHoverCitation = (citationId, citationHoverTimeInMs) => makeInsightAnalyticsActionFactory(SearchPageEvents.generatedAnswerSourceHover)({
    prefix: 'analytics/generatedAnswer/hoverCitation',
    __legacy__getBuilder: (client, state) => {
        const generativeQuestionAnsweringId = generativeQuestionAnsweringIdSelector(state);
        const citation = citationSourceSelector(state, citationId);
        if (!generativeQuestionAnsweringId || !citation) {
            return null;
        }
        return client.logGeneratedAnswerSourceHover({
            generativeQuestionAnsweringId,
            permanentId: citation.permanentid,
            citationId: citation.id,
            citationHoverTimeMs: citationHoverTimeInMs,
        }, getCaseContextAnalyticsMetadata(state.insightCaseContext));
    },
    analyticsType: 'Rga.CitationHover',
    analyticsPayloadBuilder: (state) => {
        const citation = citationSourceSelector(state, citationId);
        const generativeQuestionAnsweringId = generativeQuestionAnsweringIdSelector(state);
        return {
            answerId: generativeQuestionAnsweringId ?? '',
            citationId,
            itemMetadata: {
                uniqueFieldName: 'permanentid',
                uniqueFieldValue: citation?.permanentid ?? '',
                title: citation?.title,
                url: citation?.clickUri,
            },
            citationHoverTimeInMs,
        };
    },
});
export const logLikeGeneratedAnswer = () => makeInsightAnalyticsActionFactory(SearchPageEvents.likeGeneratedAnswer)({
    prefix: 'analytics/generatedAnswer/like',
    __legacy__getBuilder: (client, state) => {
        const generativeQuestionAnsweringId = generativeQuestionAnsweringIdSelector(state);
        if (!generativeQuestionAnsweringId) {
            return null;
        }
        return client.logLikeGeneratedAnswer({
            generativeQuestionAnsweringId,
        }, getCaseContextAnalyticsMetadata(state.insightCaseContext));
    },
    analyticsType: 'Rga.AnswerAction',
    analyticsPayloadBuilder: (state) => {
        const generativeQuestionAnsweringId = generativeQuestionAnsweringIdSelector(state);
        return {
            answerId: generativeQuestionAnsweringId ?? '',
            action: 'like',
        };
    },
});
export const logDislikeGeneratedAnswer = () => makeInsightAnalyticsActionFactory(SearchPageEvents.dislikeGeneratedAnswer)({
    prefix: 'analytics/generatedAnswer/dislike',
    __legacy__getBuilder: (client, state) => {
        const generativeQuestionAnsweringId = generativeQuestionAnsweringIdSelector(state);
        if (!generativeQuestionAnsweringId) {
            return null;
        }
        return client.logDislikeGeneratedAnswer({
            generativeQuestionAnsweringId,
        }, getCaseContextAnalyticsMetadata(state.insightCaseContext));
    },
    analyticsType: 'Rga.AnswerAction',
    analyticsPayloadBuilder: (state) => {
        const generativeQuestionAnsweringId = generativeQuestionAnsweringIdSelector(state);
        return {
            answerId: generativeQuestionAnsweringId ?? '',
            action: 'dislike',
        };
    },
});
export const logGeneratedAnswerFeedback = (feedback) => makeInsightAnalyticsActionFactory(SearchPageEvents.generatedAnswerFeedbackSubmit)({
    prefix: 'analytics/generatedAnswer/sendFeedback',
    __legacy__getBuilder: (client, state) => {
        const generativeQuestionAnsweringId = generativeQuestionAnsweringIdSelector(state);
        if (!generativeQuestionAnsweringId) {
            return null;
        }
        return client.logGeneratedAnswerFeedbackSubmitV2({
            generativeQuestionAnsweringId,
            ...feedback,
        }, getCaseContextAnalyticsMetadata(state.insightCaseContext));
    },
    analyticsType: 'Rga.SubmitFeedback',
    analyticsPayloadBuilder: (state) => {
        const generativeQuestionAnsweringId = generativeQuestionAnsweringIdSelector(state);
        const { helpful, readable, documented, hallucinationFree, correctTopic, documentUrl, details, } = feedback;
        return {
            answerId: generativeQuestionAnsweringId ?? '',
            helpful,
            details: {
                readable: parseEvaluationDetails(readable),
                documented: parseEvaluationDetails(documented),
                correctTopic: parseEvaluationDetails(correctTopic),
                hallucinationFree: parseEvaluationDetails(hallucinationFree),
            },
            additionalNotes: details,
            correctAnswerUrl: documentUrl,
        };
    },
});
//TODO: SFINT-5435
export const logGeneratedAnswerStreamEnd = (answerGenerated) => makeInsightAnalyticsActionFactory(SearchPageEvents.generatedAnswerStreamEnd)({
    prefix: 'analytics/generatedAnswer/streamEnd',
    __legacy__getBuilder: (client, state) => {
        const generativeQuestionAnsweringId = generativeQuestionAnsweringIdSelector(state);
        const answerTextIsEmpty = answerGenerated
            ? !state.generatedAnswer?.answer ||
                !state.generatedAnswer?.answer.length
            : undefined;
        if (!generativeQuestionAnsweringId) {
            return null;
        }
        return client.logGeneratedAnswerStreamEnd({
            generativeQuestionAnsweringId,
            answerGenerated,
            answerTextIsEmpty,
        }, getCaseContextAnalyticsMetadata(state.insightCaseContext));
    },
    analyticsType: 'Rga.AnswerReceived',
    analyticsPayloadBuilder: (state) => {
        const generativeQuestionAnsweringId = generativeQuestionAnsweringIdSelector(state);
        return {
            answerId: generativeQuestionAnsweringId ?? '',
            answerGenerated,
        };
    },
});
export const logGeneratedAnswerShowAnswers = () => makeInsightAnalyticsActionFactory(SearchPageEvents.generatedAnswerShowAnswers)({
    prefix: 'analytics/generatedAnswer/show',
    __legacy__getBuilder: (client, state) => {
        const generativeQuestionAnsweringId = generativeQuestionAnsweringIdSelector(state);
        if (!generativeQuestionAnsweringId) {
            return null;
        }
        return client.logGeneratedAnswerShowAnswers({
            generativeQuestionAnsweringId,
        }, getCaseContextAnalyticsMetadata(state.insightCaseContext));
    },
    analyticsType: 'Rga.AnswerAction',
    analyticsPayloadBuilder: (state) => {
        const generativeQuestionAnsweringId = generativeQuestionAnsweringIdSelector(state);
        return {
            answerId: generativeQuestionAnsweringId ?? '',
            action: 'show',
        };
    },
});
export const logGeneratedAnswerHideAnswers = () => makeInsightAnalyticsActionFactory(SearchPageEvents.generatedAnswerHideAnswers)({
    prefix: 'analytics/generatedAnswer/hide',
    __legacy__getBuilder: (client, state) => {
        const generativeQuestionAnsweringId = generativeQuestionAnsweringIdSelector(state);
        if (!generativeQuestionAnsweringId) {
            return null;
        }
        return client.logGeneratedAnswerHideAnswers({
            generativeQuestionAnsweringId,
        }, getCaseContextAnalyticsMetadata(state.insightCaseContext));
    },
    analyticsType: 'Rga.AnswerAction',
    analyticsPayloadBuilder: (state) => {
        const generativeQuestionAnsweringId = generativeQuestionAnsweringIdSelector(state);
        return {
            answerId: generativeQuestionAnsweringId ?? '',
            action: 'hide',
        };
    },
});
export const logGeneratedAnswerExpand = () => makeInsightAnalyticsActionFactory(SearchPageEvents.generatedAnswerExpand)({
    prefix: 'analytics/generatedAnswer/expand',
    __legacy__getBuilder: (client, state) => {
        const generativeQuestionAnsweringId = generativeQuestionAnsweringIdSelector(state);
        if (!generativeQuestionAnsweringId) {
            return null;
        }
        return client.logGeneratedAnswerExpand({
            generativeQuestionAnsweringId,
        }, getCaseContextAnalyticsMetadata(state.insightCaseContext));
    },
    analyticsType: 'Rga.AnswerAction',
    analyticsPayloadBuilder: (state) => {
        const generativeQuestionAnsweringId = generativeQuestionAnsweringIdSelector(state);
        return {
            answerId: generativeQuestionAnsweringId ?? '',
            action: 'expand',
        };
    },
});
export const logGeneratedAnswerCollapse = () => makeInsightAnalyticsActionFactory(SearchPageEvents.generatedAnswerCollapse)({
    prefix: 'analytics/generatedAnswer/collapse',
    __legacy__getBuilder: (client, state) => {
        const generativeQuestionAnsweringId = generativeQuestionAnsweringIdSelector(state);
        if (!generativeQuestionAnsweringId) {
            return null;
        }
        return client.logGeneratedAnswerCollapse({
            generativeQuestionAnsweringId,
        }, getCaseContextAnalyticsMetadata(state.insightCaseContext));
    },
    analyticsType: 'Rga.AnswerAction',
    analyticsPayloadBuilder: (state) => {
        const generativeQuestionAnsweringId = generativeQuestionAnsweringIdSelector(state);
        return {
            answerId: generativeQuestionAnsweringId ?? '',
            action: 'collapse',
        };
    },
});
export const logCopyGeneratedAnswer = () => makeInsightAnalyticsActionFactory(SearchPageEvents.generatedAnswerCopyToClipboard)({
    prefix: 'analytics/generatedAnswer/copy',
    __legacy__getBuilder: (client, state) => {
        const generativeQuestionAnsweringId = generativeQuestionAnsweringIdSelector(state);
        if (!generativeQuestionAnsweringId) {
            return null;
        }
        return client.logGeneratedAnswerCopyToClipboard({
            generativeQuestionAnsweringId,
        }, getCaseContextAnalyticsMetadata(state.insightCaseContext));
    },
    analyticsType: 'Rga.AnswerAction',
    analyticsPayloadBuilder: (state) => {
        const generativeQuestionAnsweringId = generativeQuestionAnsweringIdSelector(state);
        return {
            answerId: generativeQuestionAnsweringId ?? '',
            action: 'copyToClipboard',
        };
    },
});
export const generatedAnswerInsightAnalyticsClient = {
    logCopyGeneratedAnswer,
    logGeneratedAnswerHideAnswers,
    logGeneratedAnswerShowAnswers,
    logGeneratedAnswerStreamEnd,
    logGeneratedAnswerFeedback,
    logDislikeGeneratedAnswer,
    logLikeGeneratedAnswer,
    logHoverCitation,
    logOpenGeneratedAnswerSource,
    logRetryGeneratedAnswer,
    logGeneratedAnswerExpand,
    logGeneratedAnswerCollapse,
};
