import Script from "next/script"

export default function VwoScript() {
  return (
    <Script id="initialize-vwo" data-cookieconsent="statistics">{`
        // VWO initialization code
        // Note ------------
        // Any update in this file should also update the cache burst parameter manually in the layout files
        // Updated snippet for cookie consent handling - 07-Mar-2023

        var _vwo_clicks = 50;
        console.log('firing!!!!!');
        debugger;
        //DO NOT make any changes to smart code except for entering your account id
        window._vwo_code = window._vwo_code || (function() {
            var account_id = 373511,
            settings_tolerance = 2500,
            library_tolerance = 3000,
            use_existing_jquery = false,
            /* DO NOT EDIT BELOW THIS LINE */
            f = false,
            d = document,
            filterToleranceTimer,
            code = {
                use_existing_jquery: function() {
                    return use_existing_jquery;
                },
                library_tolerance: function() {
                    return library_tolerance;
                },
                finish: function() {
                    if (!f) {
                        f = true;
                        var a = d.getElementById('_vis_opt_path_hides');
                        if (a) a.parentNode.removeChild(a);
                    }
                },
                finished: function() {
                    return f;
                },
                load: function(a) {
                    var b = d.createElement('script');
                    b.src = a;
                    b.type = 'text/javascript';
                    b.innerText;
                    b.onerror = function() {
                        _vwo_code.finish();
                    };
                    d.getElementsByTagName('head')[0].appendChild(b);
                },
                init: function() {
                    window.settings_timer = setTimeout(function() {
                        _vwo_code.finish();
                    }, settings_tolerance);

                    var filterConfig = this.filterConfig;
                    if (!filterConfig) {
                        var a = d.createElement('style'),
                            b = hide_element ? hide_element + '{opacity:0 !important;filter:alpha(opacity=0) !important;background:none !important;}' : '',
                            h = d.getElementsByTagName('head')[0];
                        a.setAttribute('id', '_vis_opt_path_hides');
                        a.setAttribute('type', 'text/css');
                        if (a.styleSheet) a.styleSheet.cssText = b;
                        else a.appendChild(d.createTextNode(b));
                        h.appendChild(a);
                    }
                    this.load('https://dev.visualwebsiteoptimizer.com/j.php?a=' + account_id + '&u=' + encodeURIComponent(d.URL) + '&r=' + Math.random());
                    return settings_timer;
                },
                setFilterConfigAndApplyFilter: function(config) {
                    if (!config) {
                        // No config will lead to early return and no addition of smart code
                        return;
                    }
                    var state = this.isNonLiveMode(account_id) || config.isConsentGiven();
                    if (state === "1") {
                        // if cookie consent was already given and the cookies were already accepted, execute out normal flow
                        hide_element = 'body';
                        window._vwo_settings_timer = _vwo_code.init();
                        return;
                    } else if (state === "2") {
                        return;
                    }

                    this.filterConfig = config;
                    var timer = setInterval(function() {
                        var state = config.isConsentGiven();
                        var shouldClearAllIntervals;
                        if (state === "1") {
                            window._vwo_settings_timer = _vwo_code.init();
                            shouldClearAllIntervals = true;
                        } else if (state === "2") {
                            _vwo_code.filterConfig = undefined;
                            shouldClearAllIntervals = true;
                        }
                        if (shouldClearAllIntervals) {
                            clearInterval(timer);
                            clearInterval(filterToleranceTimer);
                        }
                    }, 100)
                },
                isNonLiveMode: function(accountId) {
                    var wName = window.name;
                    if (!wName) {
                        return;
                    }
                    return (
                        (wName.indexOf('_vis_editor') > -1 ||
                            wName.indexOf('_vis_preview_' + accountId) > -1 ||
                            wName.indexOf('_vis_heatmap_' + accountId) > -1) &&
                            '1'
                    );
                }
            };
            console.log('return 👇');
            return code;
        }());

        _vwo_code.setFilterConfigAndApplyFilter({
            popupSelector: '#CybotCookiebotDialog',
            isConsentGiven: function() {
            /** your custom Code */
            if (document.cookie.indexOf("CookieConsent") != -1) {
            var ccValue = decodeURIComponent(document.cookie.replace(new RegExp("(?:(?:^|.*;)\\s*" + encodeURIComponent("CookieConsent").replace(/[\-\.\+\*]/g, "\\$&") + "\\s*\\=\\s*([^;]*).*$)|^.*$"), "$1")) || null;
            let consentValue = "2";
            if (ccValue) {
                ccValue = ccValue.substring(ccValue.indexOf(",statistics")+12);
                ccValue = ccValue.substring(0, ccValue.indexOf(","));
                consentValue = ccValue === "true" ? "1" : "2";
            }
                return consentValue;
            } else {
                return "3";
            }
        }
    });
    `}</Script>
  )
}
