import { augmentAnalyticsConfigWithAtomicVersion, augmentAnalyticsConfigWithDocument, augmentAnalyticsWithAtomicVersion, augmentWithExternalMiddleware, getNextAnalyticsConfig } from "../../common/interface/analytics-config.js";
function getAnalyticsConfig(searchEngineConfig, enabled, store) {
    switch(searchEngineConfig.analytics?.analyticsMode){
        case 'next':
            return getNextAnalyticsConfig(searchEngineConfig, enabled);
        default:
            return getLegacyAnalyticsConfig(searchEngineConfig, enabled, store);
    }
}
function getLegacyAnalyticsConfig(searchEngineConfig, enabled, store) {
    const analyticsClientMiddleware = (event, payload)=>augmentAnalytics(event, payload, store, searchEngineConfig);
    const defaultConfiguration = {
        analyticsClientMiddleware,
        enabled,
        ...augmentAnalyticsConfigWithDocument()
    };
    const immutableConfiguration = {
        ...augmentAnalyticsConfigWithAtomicVersion()
    };
    if (searchEngineConfig.analytics) return {
        ...defaultConfiguration,
        ...searchEngineConfig.analytics,
        analyticsClientMiddleware,
        ...immutableConfiguration
    };
    return {
        ...defaultConfiguration,
        ...immutableConfiguration
    };
}
function augmentAnalytics(event, payload, store, config) {
    let result = augmentWithExternalMiddleware(event, payload, config);
    result = augmentAnalyticsWithAtomicVersion(result);
    result = augmentAnalyticsWithFacetTitles(result, store);
    return result;
}
function augmentAnalyticsWithFacetTitles(payload, store) {
    const allFacets = store.getAllFacets();
    const getAtomicFacetLabelOrOriginalTitle = (facetId, originalTitle)=>allFacets[facetId] ? allFacets[facetId].label() : originalTitle;
    if (payload.facetState) payload.facetState = payload.facetState.map((analyticsFacetState)=>{
        const { id, title: originalTitle } = analyticsFacetState;
        return {
            ...analyticsFacetState,
            title: getAtomicFacetLabelOrOriginalTitle(id, originalTitle)
        };
    });
    if (payload.customData?.facetTitle && payload.customData.facetId && payload.customData.facetTitle) payload.customData.facetTitle = getAtomicFacetLabelOrOriginalTitle(payload.customData.facetId, payload.customData.facetTitle);
    return payload;
}
export { getAnalyticsConfig };
