import { ArrayValue, BooleanValue, RecordValue, StringValue, Value, } from '@coveo/bueno';
import { createAction } from '@reduxjs/toolkit';
import { nonEmptyString, optionalNonEmptyVersionString, optionalTrackingId, requiredNonEmptyString, validatePayload, } from '../../utils/validate-payload.js';
import { COVEO_FRAMEWORK } from '../../utils/version.js';
const originSchemaOnConfigUpdate = () => nonEmptyString;
const originSchemaOnUpdate = () => requiredNonEmptyString;
export const updateBasicConfiguration = createAction('configuration/updateBasicConfiguration', (payload) => validatePayload(payload, {
    accessToken: nonEmptyString,
    environment: new StringValue({
        required: false,
        constrainTo: ['prod', 'hipaa', 'stg', 'dev'],
    }),
    organizationId: nonEmptyString,
}));
export const updateSearchConfiguration = createAction('configuration/updateSearchConfiguration', (payload) => {
    return validatePayload(payload, {
        proxyBaseUrl: new StringValue({ required: false, url: true }),
        pipeline: new StringValue({ required: false, emptyAllowed: true }),
        searchHub: nonEmptyString,
        timezone: nonEmptyString,
        locale: nonEmptyString,
        authenticationProviders: new ArrayValue({
            required: false,
            each: requiredNonEmptyString,
        }),
    });
});
export const analyticsConfigurationSchema = {
    enabled: new BooleanValue({ default: true }),
    originContext: originSchemaOnConfigUpdate(),
    originLevel2: originSchemaOnConfigUpdate(),
    originLevel3: originSchemaOnConfigUpdate(),
    proxyBaseUrl: new StringValue({ required: false, url: true }),
    runtimeEnvironment: new Value(),
    anonymous: new BooleanValue({ default: false }),
    deviceId: nonEmptyString,
    userDisplayName: nonEmptyString,
    documentLocation: nonEmptyString,
    trackingId: optionalTrackingId,
    analyticsMode: new StringValue({
        constrainTo: ['legacy', 'next'],
        required: false,
        default: 'next',
    }),
    source: new RecordValue({
        options: { required: false },
        values: COVEO_FRAMEWORK.reduce((acc, framework) => {
            acc[framework] = optionalNonEmptyVersionString;
            return acc;
        }, {}),
    }),
};
export const updateAnalyticsConfiguration = createAction('configuration/updateAnalyticsConfiguration', (payload) => {
    return validatePayload(payload, analyticsConfigurationSchema);
});
export const disableAnalytics = createAction('configuration/analytics/disable');
export const enableAnalytics = createAction('configuration/analytics/enable');
export const setOriginLevel2 = createAction('configuration/analytics/originlevel2', (payload) => validatePayload(payload, { originLevel2: originSchemaOnUpdate() }));
export const setOriginLevel3 = createAction('configuration/analytics/originlevel3', (payload) => validatePayload(payload, { originLevel3: originSchemaOnUpdate() }));
