import { createRelay } from '@coveo/relay';
import { createSelector } from '@reduxjs/toolkit';
import { getAnalyticsNextApiBaseUrl } from '../platform-client.js';
import { getAnalyticsSource } from './analytics-selectors.js';
export const getRelayInstanceFromState = createSelector((state) => state.configuration.organizationId, (state) => state.configuration.environment, (state) => state.configuration.accessToken, (state) => state.configuration.analytics, (state) => getAnalyticsSource(state.configuration.analytics), (_state, navigatorContextProvider) => getEnvironment(navigatorContextProvider), (organizationId, platformEnvironment, token, { trackingId, apiBaseUrl, enabled }, source, environment) => createRelay({
    mode: enabled ? 'emit' : 'disabled',
    url: apiBaseUrl ??
        getAnalyticsNextApiBaseUrl(organizationId, platformEnvironment),
    token,
    trackingId: trackingId ?? null,
    source,
    environment,
}));
const getEnvironment = (customProvider) => {
    if (!customProvider) {
        return undefined;
    }
    const customContext = customProvider();
    return {
        getLocation: () => customContext.location,
        getReferrer: () => customContext.referrer,
        getUserAgent: () => customContext.userAgent,
        generateUUID: () => customContext.clientId,
    };
};
