saveCookiePreferences = () => {
    const state = globalObj._state;

    /**
     * Determine if categories were changed from last state (saved in the cookie)
     */
    state._lastChangedCategoryNames = globalObj._config.mode === OPT_OUT_MODE && state._invalidConsent
        ? arrayDiff(state._defaultEnabledCategories, state._acceptedCategories)
        : arrayDiff(state._acceptedCategories, state._savedCookieContent.categories);

    let categoriesWereChanged = state._lastChangedCategoryNames.length > 0;
    let servicesWereChanged = false;

    /**
     * Determine if services were changed from last state
     */
    for (const categoryName of state._allCategoryNames) {
        state._lastChangedServices[categoryName] = arrayDiff(
            state._acceptedServices[categoryName],
            state._lastEnabledServices[categoryName]
        );

        if (state._lastChangedServices[categoryName].length > 0)
            servicesWereChanged = true;
    }

    //{{START: GUI}}
    const categoryToggles = globalObj._dom._categoryCheckboxInputs;

    /**
     * If the category is accepted check checkbox,
     * otherwise uncheck it
     */
    for (const categoryName in categoryToggles) {
        categoryToggles[categoryName].checked = elContains(state._acceptedCategories, categoryName);
    }

    for (const categoryName of state._allCategoryNames) {
        const servicesToggles = globalObj._dom._serviceCheckboxInputs[categoryName];
        const enabledServices = state._acceptedServices[categoryName];

        for (const serviceName in servicesToggles) {
            const serviceInput = servicesToggles[serviceName];
            serviceInput.checked = elContains(enabledServices, serviceName);
        }
    }
    //{{END: GUI}}

    if (!state._consentTimestamp)
        state._consentTimestamp = new Date();

    if (!state._consentId)
        state._consentId = uuidv4();

    state._savedCookieContent = {
        categories: deepCopy(state._acceptedCategories),
        revision: globalObj._config.revision,
        data: state._cookieData,
        consentTimestamp: state._consentTimestamp.toISOString(),
        consentId: state._consentId,
        services: deepCopy(state._acceptedServices),
        languageCode: globalObj._state._currentLanguageCode
    };

    if (state._lastConsentTimestamp) {
        state._savedCookieContent.lastConsentTimestamp = state._lastConsentTimestamp.toISOString();
    }

    let isFirstConsent = false;
    const stateChanged = categoriesWereChanged || servicesWereChanged;

    if (state._invalidConsent || stateChanged) {
        /**
         * Set consent as valid
         */
        if (state._invalidConsent) {
            state._invalidConsent = false;
            isFirstConsent = true;
        }

        state._lastConsentTimestamp = !state._lastConsentTimestamp
            ? state._consentTimestamp
            : new Date();

        state._savedCookieContent.lastConsentTimestamp = state._lastConsentTimestamp.toISOString();

        setCookie();

        const isAutoClearEnabled = globalObj._config.autoClearCookies;
        const shouldClearCookies = isFirstConsent || stateChanged;

        if (isAutoClearEnabled && shouldClearCookies)
            autoclearCookiesHelper(isFirstConsent);

        manageExistingScripts();
    }

    if (isFirstConsent) {
        fireEvent(globalObj._customEvents._onFirstConsent);
        fireEvent(globalObj._customEvents._onConsent);

        if (globalObj._config.mode === OPT_IN_MODE)
            return;
    }

    if (stateChanged)
        fireEvent(globalObj._customEvents._onChange);

    /**
     * Reload page if needed
     */
    if (state._reloadPage) {
        state._reloadPage = false;
        location.reload();
    }
}
Clear cookies for each disabled service