import { getApiBaseUrlOrOrganizationEndpoint } from '../../api/platform-client.js';
import { fromAnalyticsStateToAnalyticsParams } from '../configuration/legacy-analytics-params.js';
import { getFacetRequests } from '../facets/generic/interfaces/generic-facet-request.js';
import { maximumNumberOfResultsFromIndex } from '../pagination/pagination-constants.js';
import { mapSearchRequest, } from '../search/search-mappings.js';
export const buildInsightBaseRequest = async (state, eventDescription) => {
    const cq = buildConstantQuery(state);
    const facets = getAllFacets(state);
    return mapSearchRequest({
        accessToken: state.configuration.accessToken,
        organizationId: state.configuration.organizationId,
        url: getApiBaseUrlOrOrganizationEndpoint(state.configuration.search.apiBaseUrl, state.configuration.organizationId, state.configuration.environment),
        locale: state.configuration.search.locale,
        insightId: state.insightConfiguration.insightId,
        ...(state.configuration.analytics.enabled &&
            (await fromAnalyticsStateToAnalyticsParams(state.configuration.analytics, eventDescription))),
        q: state.query?.q,
        ...(facets.length && { facets }),
        caseContext: state.insightCaseContext?.caseContext,
        ...(cq && { cq }),
        ...(state.fields &&
            !state.fields.fetchAllFields && {
            fieldsToInclude: state.fields.fieldsToInclude,
        }),
        ...(state.didYouMean && {
            queryCorrection: {
                enabled: state.didYouMean.enableDidYouMean &&
                    state.didYouMean.queryCorrectionMode === 'next',
                options: {
                    automaticallyCorrect: state.didYouMean.automaticallyCorrectQuery
                        ? 'whenNoResults'
                        : 'never',
                },
            },
            enableDidYouMean: state.didYouMean.enableDidYouMean &&
                state.didYouMean.queryCorrectionMode === 'legacy',
        }),
        ...(state.sortCriteria && {
            sortCriteria: state.sortCriteria,
        }),
        tab: state.configuration.analytics.originLevel2,
        ...(state.folding && {
            filterField: state.folding.fields.collection,
            childField: state.folding.fields.parent,
            parentField: state.folding.fields.child,
            filterFieldRange: state.folding.filterFieldRange,
        }),
        ...(state.context && { context: state.context.contextValues }),
        ...(state.generatedAnswer && {
            pipelineRuleParameters: {
                mlGenerativeQuestionAnswering: {
                    responseFormat: state.generatedAnswer.responseFormat,
                    citationsFieldToInclude: state.generatedAnswer.fieldsToIncludeInCitations,
                },
            },
        }),
    });
};
export const buildInsightSearchRequest = async (state, eventDescription) => {
    const getNumberOfResultsWithinIndexLimit = () => {
        if (!state.pagination) {
            return undefined;
        }
        const isOverIndexLimit = state.pagination.firstResult + state.pagination.numberOfResults >
            maximumNumberOfResultsFromIndex;
        if (isOverIndexLimit) {
            return maximumNumberOfResultsFromIndex - state.pagination.firstResult;
        }
        return state.pagination.numberOfResults;
    };
    const baseRequest = await buildInsightBaseRequest(state, eventDescription);
    return {
        ...baseRequest,
        request: {
            ...baseRequest.request,
            ...(state.pagination && {
                firstResult: state.pagination.firstResult,
                numberOfResults: getNumberOfResultsWithinIndexLimit(),
            }),
        },
    };
};
export const buildInsightLoadCollectionRequest = async (state, collectionId) => {
    const baseRequest = await buildInsightBaseRequest(state);
    return {
        ...baseRequest,
        request: {
            ...baseRequest.request,
            filterFieldRange: 100,
            cq: `@${state?.folding?.fields.collection}="${collectionId}"`,
        },
    };
};
export const buildInsightFetchMoreResultsRequest = async (state, eventDescription) => {
    const mappedRequest = await buildInsightSearchRequest(state, eventDescription);
    mappedRequest.request = {
        ...mappedRequest.request,
        firstResult: (state.pagination?.firstResult ?? 0) +
            (state.pagination?.numberOfResults ?? 0),
    };
    return mappedRequest;
};
export const buildInsightFetchFacetValuesRequest = async (state, eventDescription) => {
    const mappedRequest = await buildInsightSearchRequest(state, eventDescription);
    mappedRequest.request = {
        ...mappedRequest.request,
        numberOfResults: 0,
    };
    return mappedRequest;
};
function getAllFacets(state) {
    return getFacetRequests({
        ...state.facetSet,
        ...state.numericFacetSet,
        ...state.dateFacetSet,
        ...state.categoryFacetSet,
    });
}
function buildConstantQuery(state) {
    const activeTab = Object.values(state.tabSet || {}).find((tab) => tab.isActive);
    const tabExpression = activeTab?.expression.trim() || '';
    return tabExpression;
}
