import { getSearchHubInitialState } from '../../features/search-hub/search-hub-state.js';
import { VERSION } from '../../utils/version.js';
export const getLanguage = (state) => {
    const langKey = state.configuration.search.locale.split('-')[0];
    if (!langKey || langKey.length !== 2) {
        return 'en';
    }
    return langKey;
};
export class BaseAnalyticsProvider {
    getState;
    state;
    constructor(getState) {
        this.getState = getState;
        this.state = getState();
    }
    getLanguage() {
        return getLanguage(this.state);
    }
    getBaseMetadata() {
        const { context, configuration } = this.state;
        const contextValues = context?.contextValues || {};
        const formattedObject = {};
        for (const [key, value] of Object.entries(contextValues)) {
            const formattedKey = `context_${key}`;
            formattedObject[formattedKey] = value;
        }
        if (configuration.analytics.analyticsMode === 'legacy') {
            formattedObject.coveoHeadlessVersion = VERSION;
        }
        return formattedObject;
    }
    getOriginContext() {
        return this.state.configuration.analytics.originContext;
    }
    getOriginLevel1() {
        return this.state.searchHub || getSearchHubInitialState();
    }
    getOriginLevel2() {
        return this.state.configuration.analytics.originLevel2;
    }
    getOriginLevel3() {
        return this.state.configuration.analytics.originLevel3;
    }
    getIsAnonymous() {
        return this.state.configuration.analytics.anonymous;
    }
}
