import { isNullOrUndefined } from '@coveo/bueno';
import { createSelector } from '@reduxjs/toolkit';
export const generativeQuestionAnsweringIdSelector = (state) => {
    // If using the AnswerApi, we return the answerId first.
    if (isGeneratedAnswerSection(state)) {
        // Read the answerId directly from the state instead of RTK Query to prevent circular dependency chain
        const getAnswerData = Object.values(state.answer.queries).pop()?.data;
        const generativeQuestionAnsweringId = typeof getAnswerData === 'object' &&
            getAnswerData !== null &&
            'answerId' in getAnswerData
            ? getAnswerData.answerId
            : undefined;
        return generativeQuestionAnsweringId;
    }
    // Used for type narrowing.
    if (isSearchSection(state)) {
        return state.search?.response?.extendedResults
            ?.generativeQuestionAnsweringId;
    }
    return undefined;
};
const isGeneratedAnswerSection = (state) => 'answer' in state &&
    'generatedAnswer' in state &&
    !isNullOrUndefined(state.generatedAnswer?.answerConfigurationId);
const isSearchSection = (state) => 'search' in state &&
    state.search !== undefined &&
    typeof state.search === 'object';
export const selectFieldsToIncludeInCitation = (state) => state.generatedAnswer?.fieldsToIncludeInCitations;
export const citationSourceSelector = createSelector((state) => state.generatedAnswer?.citations, (_state, citationId) => citationId, (citations, citationId) => citations?.find((citation) => citation.id === citationId));
