import { citationDocumentIdentifier, makeAnalyticsAction, partialCitationInformation, } from '../analytics/analytics-utils.js';
import { SearchPageEvents } from '../analytics/search-action-cause.js';
import { citationSourceSelector, generativeQuestionAnsweringIdSelector, } from './generated-answer-selectors.js';
export const parseEvaluationDetails = (detail) => {
    if (detail === 'yes') {
        return true;
    }
    if (detail === 'no') {
        return false;
    }
    return undefined;
};
//TODO: KIT-2859
export const logRetryGeneratedAnswer = () => makeAnalyticsAction('analytics/generatedAnswer/retry', (client) => client.makeRetryGeneratedAnswer());
export function logOpenGeneratedAnswerSource(citationId, answerId) {
    return makeAnalyticsAction({
        prefix: 'analytics/generatedAnswer/openAnswerSource',
        __legacy__getBuilder: (client, state) => {
            const resolvedAnswerId = answerId ?? generativeQuestionAnsweringIdSelector(state);
            const citation = citationSourceSelector(state, citationId);
            if (!resolvedAnswerId || !citation) {
                return null;
            }
            return client.makeGeneratedAnswerCitationClick(partialCitationInformation(citation, state), {
                generativeQuestionAnsweringId: resolvedAnswerId,
                citationId: citation.id,
                documentId: citationDocumentIdentifier(citation),
            });
        },
        analyticsType: 'Rga.CitationClick',
        analyticsPayloadBuilder: (state) => {
            const citation = citationSourceSelector(state, citationId);
            const resolvedAnswerId = answerId ?? generativeQuestionAnsweringIdSelector(state);
            return {
                answerId: resolvedAnswerId ?? '',
                citationId,
                itemMetadata: {
                    uniqueFieldName: 'permanentid',
                    uniqueFieldValue: citation?.permanentid ?? '',
                    title: citation?.title,
                    url: citation?.clickUri,
                },
            };
        },
    });
}
export function logHoverCitation(citationId, citationHoverTimeInMs, answerId) {
    return makeAnalyticsAction({
        prefix: 'analytics/generatedAnswer/hoverCitation',
        __legacy__getBuilder: (client, state) => {
            const resolvedAnswerId = answerId ?? generativeQuestionAnsweringIdSelector(state);
            const citation = citationSourceSelector(state, citationId);
            if (!resolvedAnswerId || !citation) {
                return null;
            }
            return client.makeGeneratedAnswerSourceHover({
                generativeQuestionAnsweringId: resolvedAnswerId,
                permanentId: citation.permanentid,
                citationId: citation.id,
                citationHoverTimeMs: citationHoverTimeInMs,
            });
        },
        analyticsType: 'Rga.CitationHover',
        analyticsPayloadBuilder: (state) => {
            const citation = citationSourceSelector(state, citationId);
            const resolvedAnswerId = answerId ?? generativeQuestionAnsweringIdSelector(state);
            return {
                answerId: resolvedAnswerId ?? '',
                citationId,
                itemMetadata: {
                    uniqueFieldName: 'permanentid',
                    uniqueFieldValue: citation?.permanentid ?? '',
                    title: citation?.title,
                    url: citation?.clickUri,
                },
                citationHoverTimeInMs,
            };
        },
    });
}
export function logLikeGeneratedAnswer(answerId) {
    return makeAnalyticsAction({
        prefix: 'analytics/generatedAnswer/like',
        __legacy__getBuilder: (client, state) => {
            const resolvedAnswerId = answerId ?? generativeQuestionAnsweringIdSelector(state);
            if (!resolvedAnswerId) {
                return null;
            }
            return client.makeLikeGeneratedAnswer({
                generativeQuestionAnsweringId: resolvedAnswerId,
            });
        },
        analyticsType: 'Rga.AnswerAction',
        analyticsPayloadBuilder: (state) => {
            const resolvedAnswerId = answerId ?? generativeQuestionAnsweringIdSelector(state);
            return {
                answerId: resolvedAnswerId ?? '',
                action: 'like',
            };
        },
    });
}
export function logDislikeGeneratedAnswer(answerId) {
    return makeAnalyticsAction({
        prefix: 'analytics/generatedAnswer/dislike',
        __legacy__getBuilder: (client, state) => {
            const resolvedAnswerId = answerId ?? generativeQuestionAnsweringIdSelector(state);
            if (!resolvedAnswerId) {
                return null;
            }
            return client.makeDislikeGeneratedAnswer({
                generativeQuestionAnsweringId: resolvedAnswerId,
            });
        },
        analyticsType: 'Rga.AnswerAction',
        analyticsPayloadBuilder: (state) => {
            const resolvedAnswerId = answerId ?? generativeQuestionAnsweringIdSelector(state);
            return {
                answerId: resolvedAnswerId ?? '',
                action: 'dislike',
            };
        },
    });
}
export const logGeneratedAnswerFeedback = (feedback) => makeAnalyticsAction({
    prefix: 'analytics/generatedAnswer/sendFeedback',
    __legacy__getBuilder: (client, state) => {
        const generativeQuestionAnsweringId = generativeQuestionAnsweringIdSelector(state);
        if (!generativeQuestionAnsweringId) {
            return null;
        }
        return client.makeGeneratedAnswerFeedbackSubmitV2({
            generativeQuestionAnsweringId,
            ...feedback,
        });
    },
    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) => makeAnalyticsAction({
    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.makeGeneratedAnswerStreamEnd({
            generativeQuestionAnsweringId,
            answerGenerated,
            answerTextIsEmpty,
        });
    },
    analyticsType: 'Rga.AnswerReceived',
    analyticsPayloadBuilder: (state) => {
        const generativeQuestionAnsweringId = generativeQuestionAnsweringIdSelector(state);
        return {
            answerId: generativeQuestionAnsweringId ?? '',
            answerGenerated: answerGenerated ?? false,
        };
    },
});
export const logGeneratedAnswerResponseLinked = () => makeAnalyticsAction({
    prefix: 'analytics/generatedAnswer/responseLinked',
    __legacy__getBuilder: () => {
        return null;
    },
    analyticsType: 'Rga.ResponseLinked',
    analyticsPayloadBuilder: (state) => {
        const generativeQuestionAnsweringId = generativeQuestionAnsweringIdSelector(state);
        return {
            answerId: generativeQuestionAnsweringId ?? '',
            responseId: state.search?.searchResponseId ||
                state.search?.response.searchUid ||
                '',
        };
    },
});
export const logGeneratedAnswerShowAnswers = () => makeAnalyticsAction({
    prefix: 'analytics/generatedAnswer/show',
    __legacy__getBuilder: (client, state) => {
        const generativeQuestionAnsweringId = generativeQuestionAnsweringIdSelector(state);
        if (!generativeQuestionAnsweringId) {
            return null;
        }
        return client.makeGeneratedAnswerShowAnswers({
            generativeQuestionAnsweringId,
        });
    },
    analyticsType: 'Rga.AnswerAction',
    analyticsPayloadBuilder: (state) => {
        const generativeQuestionAnsweringId = generativeQuestionAnsweringIdSelector(state);
        return {
            answerId: generativeQuestionAnsweringId ?? '',
            action: 'show',
        };
    },
});
export const logGeneratedAnswerHideAnswers = () => makeAnalyticsAction({
    prefix: 'analytics/generatedAnswer/hide',
    __legacy__getBuilder: (client, state) => {
        const generativeQuestionAnsweringId = generativeQuestionAnsweringIdSelector(state);
        if (!generativeQuestionAnsweringId) {
            return null;
        }
        return client.makeGeneratedAnswerHideAnswers({
            generativeQuestionAnsweringId,
        });
    },
    analyticsType: 'Rga.AnswerAction',
    analyticsPayloadBuilder: (state) => {
        const generativeQuestionAnsweringId = generativeQuestionAnsweringIdSelector(state);
        return {
            answerId: generativeQuestionAnsweringId ?? '',
            action: 'hide',
        };
    },
});
export const logGeneratedAnswerExpand = () => makeAnalyticsAction({
    prefix: 'analytics/generatedAnswer/expand',
    __legacy__getBuilder: (client, state) => {
        const generativeQuestionAnsweringId = generativeQuestionAnsweringIdSelector(state);
        if (!generativeQuestionAnsweringId) {
            return null;
        }
        return client.makeGeneratedAnswerExpand({
            generativeQuestionAnsweringId,
        });
    },
    analyticsType: 'Rga.AnswerAction',
    analyticsPayloadBuilder: (state) => {
        const generativeQuestionAnsweringId = generativeQuestionAnsweringIdSelector(state);
        return {
            answerId: generativeQuestionAnsweringId ?? '',
            action: 'expand',
        };
    },
});
export const logGeneratedAnswerCollapse = () => makeAnalyticsAction({
    prefix: 'analytics/generatedAnswer/collapse',
    __legacy__getBuilder: (client, state) => {
        const generativeQuestionAnsweringId = generativeQuestionAnsweringIdSelector(state);
        if (!generativeQuestionAnsweringId) {
            return null;
        }
        return client.makeGeneratedAnswerCollapse({
            generativeQuestionAnsweringId,
        });
    },
    analyticsType: 'Rga.AnswerAction',
    analyticsPayloadBuilder: (state) => {
        const generativeQuestionAnsweringId = generativeQuestionAnsweringIdSelector(state);
        return {
            answerId: generativeQuestionAnsweringId ?? '',
            action: 'collapse',
        };
    },
});
export function logCopyGeneratedAnswer(answerId) {
    return makeAnalyticsAction({
        prefix: 'analytics/generatedAnswer/copy',
        __legacy__getBuilder: (client, state) => {
            const resolvedAnswerId = answerId ?? generativeQuestionAnsweringIdSelector(state);
            if (!resolvedAnswerId) {
                return null;
            }
            return client.makeGeneratedAnswerCopyToClipboard({
                generativeQuestionAnsweringId: resolvedAnswerId,
            });
        },
        analyticsType: 'Rga.AnswerAction',
        analyticsPayloadBuilder: (state) => {
            const resolvedAnswerId = answerId ?? generativeQuestionAnsweringIdSelector(state);
            return {
                answerId: resolvedAnswerId ?? '',
                action: 'copyToClipboard',
            };
        },
    });
}
export const retryGeneratedAnswer = () => ({
    actionCause: SearchPageEvents.retryGeneratedAnswer,
});
export const generatedAnswerAnalyticsClient = {
    logCopyGeneratedAnswer,
    logGeneratedAnswerHideAnswers,
    logGeneratedAnswerShowAnswers,
    logGeneratedAnswerStreamEnd,
    logGeneratedAnswerFeedback,
    logDislikeGeneratedAnswer,
    logLikeGeneratedAnswer,
    logHoverCitation,
    logOpenGeneratedAnswerSource,
    logRetryGeneratedAnswer,
    logGeneratedAnswerExpand,
    logGeneratedAnswerCollapse,
};
