import { isNullOrUndefined } from '@coveo/bueno';
import { createSelector } from '@reduxjs/toolkit';
import { selectAgentId } from '../configuration/configuration-selectors.js';
export const generativeQuestionAnsweringIdSelector = (state) => {
    // If using the AnswerApi or the AgentApi, we return the answerId first.
    if (isGeneratedAnswerFeatureEnabledWithAnswerAPI(state) ||
        isGeneratedAnswerFeatureEnabledWithAgentAPI(state)) {
        return state.generatedAnswer?.answerId;
    }
    // Used for type narrowing.
    if (isSearchSection(state)) {
        return state.search?.response?.extendedResults
            ?.generativeQuestionAnsweringId;
    }
    return undefined;
};
const isGeneratedAnswerFeatureEnabledWithAnswerAPI = (state) => 'answer' in state &&
    'generatedAnswer' in state &&
    !isNullOrUndefined(state.generatedAnswer?.answerConfigurationId);
export const isGeneratedAnswerFeatureEnabledWithAgentAPI = (state) => {
    const agentId = selectAgentId(state);
    return ('generatedAnswer' in state &&
        typeof agentId === 'string' &&
        agentId.trim().length > 0);
};
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));
