<commit_before>import { updateEarlyAccess } from '@proton/shared/lib/api/settings';
import { deleteCookie, getCookie, setCookie } from '@proton/shared/lib/helpers/cookies';

import useFeature from './useFeature';
import useApi from './useApi';
import useLoading from './useLoading';
import useUserSettings from './useUserSettings';
import { Feature, FeatureCode } from '../containers/features';

export type Environment = 'alpha' | 'beta';

export const getVersionCookieIsValid = (
    versionCookie: Environment | undefined,
    earlyAccessScope: Feature<Environment> | undefined
) => versionCookie === undefined || earlyAccessScope?.Options?.includes(versionCookie);

export const getTargetEnvironment = (
    earlyAccessScope: Feature<Environment> | undefined,
    earlyAccessUserSetting: boolean
): Environment | undefined => {
    if (!earlyAccessScope || !earlyAccessUserSetting) {
        return;
    }

    return earlyAccessScope.Value;
};

export const versionCookieAtLoad = getCookie('Version') as Environment | undefined;

export const updateVersionCookieHelper = (
    cookieName: string,
    environment: Environment | undefined,
    earlyAccessScopeFeature: Feature<Environment> | undefined
) => {
    if (environment) {
        setCookie({
            cookieName,
            cookieValue: environment,
            expirationDate: 'max',
            path: '/',
        });
    }

    /*
     * if there is a not-allowed cookie already set in the browser,
     * leave it be, version will not be treated as set by it
     */
    if (!getVersionCookieIsValid(getCookie(cookieName) as Environment | undefined, earlyAccessScopeFeature)) {
        return;
    }

    if (!environment) {
        deleteCookie(cookieName);
    }
};

export const updateVersionCookie = (
    environment: Environment | undefined,
    earlyAccessScopeFeature: Feature<Environment> | undefined
) => {
    updateVersionCookieHelper('Version', environment, earlyAccessScopeFeature);<commit_msg>Disable setting version cookie for unsupported browsers<commit_after>import { updateEarlyAccess } from '@proton/shared/lib/api/settings';
import { deleteCookie, getCookie, setCookie } from '@proton/shared/lib/helpers/cookies';
import { doesNotSupportEarlyAccessVersion } from '@proton/shared/lib/helpers/browser';

import useFeature from './useFeature';
import useApi from './useApi';
import useLoading from './useLoading';
import useUserSettings from './useUserSettings';
import { Feature, FeatureCode } from '../containers/features';

export type Environment = 'alpha' | 'beta';

export const getVersionCookieIsValid = (
    versionCookie: Environment | undefined,
    earlyAccessScope: Feature<Environment> | undefined
) => versionCookie === undefined || earlyAccessScope?.Options?.includes(versionCookie);

export const getTargetEnvironment = (
    earlyAccessScope: Feature<Environment> | undefined,
    earlyAccessUserSetting: boolean
): Environment | undefined => {
    if (!earlyAccessScope || !earlyAccessUserSetting) {
        return;
    }

    return earlyAccessScope.Value;
};

export const versionCookieAtLoad = getCookie('Version') as Environment | undefined;

export const updateVersionCookieHelper = (
    cookieName: string,
    environment: Environment | undefined,
    earlyAccessScopeFeature: Feature<Environment> | undefined
) => {
    if (environment) {
        setCookie({
            cookieName,
            cookieValue: environment,
            expirationDate: 'max',
            path: '/',
        });
    }

    /*
     * if there is a not-allowed cookie already set in the browser,
     * leave it be, version will not be treated as set by it
     */
    if (!getVersionCookieIsValid(getCookie(cookieName) as Environment | undefined, earlyAccessScopeFeature)) {
        return;
    }

    if (!environment) {
        deleteCookie(cookieName);
    }
};

export const updateVersionCookie = (
    environment: Environment | undefined,
    earlyAccessScopeFeature: Feature<Environment> | undefined
) => {
    if (doesNotSupportEarlyAccessVersion()) {
        return;
    }
    updateVersionCookieHelper('Version', environment, earlyAccessScopeFeature);
{'fraction_non_alphanumeric': 0.1036805388501323, 'fraction_numerical': 0.0, 'mean_word_length': 3.823665893271462, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6191413', 'n_tokens_mistral': 1045, 'n_tokens_neox': 1031, 'n_words': 284}