(function () {
    var activateScripts = function () {
        if (window.CookieFirst !== undefined) {
            jQuery('[data-cfconsent-inlinescript]').each((i, element) => {
                var requiredCategories = jQuery(element).data('cfconsentCategories')
                var elementAllowed = true;

                if (requiredCategories) {
                    requiredCategories = requiredCategories.split(",");

                    jQuery.each(requiredCategories, function(i, value) {
                        if (!window.CookieFirst.consent || !window.CookieFirst.consent[value.trim()]) {
                            elementAllowed = false;
                        }
                    });
                } else {
                    elementAllowed = false;
                }

                if (elementAllowed) {
                    let $script = jQuery('<script />').attr('type', 'text/javascript').text(jQuery(element).text());
                    jQuery(element).replaceWith($script);
                }
            });

            jQuery('[data-cfconsent-src]').each((i, element) => {
                jQuery(element).attr('src', jQuery(element).data('consentSrc'));
            });
        }
    };

    var init = function () {
        activateScripts();
    };

    window.addEventListener('cf_consent_loaded', function() {
        window.setTimeout(function () {
            init();
        }, 250);
    });

    window.addEventListener('cf_consent', function() {
        window.setTimeout(function () {
            init();
        }, 250);
    });

    init();
}());