<script>
    var ga_tracking_id = '<?php echo $ga_tracking_id; ?>';
    var font_provider = '<?php echo $font_provider; ?>';
    var share_addtoany = <?php echo ($current_map['enable_share'] || $current_map['enable_share_map']) ? 1 : 0; ?>;
    var show_cookie_policy = <?php echo ((!empty($cookie_policy) && $cookie_policy!='<p></p>') ? 1 : 0); ?>;
    var cookie_link = '<?php echo (strpos($cookie_policy, 'http') === 0) ? $cookie_policy : ''; ?>';
    var footer = '';
    var categories = {
        necessary: {
            readOnly: true
        }
    };
    var sections = [
        {
            title: `<?php echo _("Usage"); ?>`,
            description: `<?php echo _("We use cookies to help you navigate efficiently and perform certain functions. You will find detailed information about all cookies under each consent category below."); ?>`
        },
        {
            title: `<?php echo _("Strictly Necessary <span class=\"pm__badge\">Always Enabled</span>"); ?>`,
            description: `<?php echo _("Necessary cookies help make a website usable by enabling basic functions like page navigation and access to secure areas of the website. The website cannot function properly without these cookies."); ?>`,
            linkedCategory: "necessary"
        }
    ];
    if(ga_tracking_id!='') {
        categories.analytics = {
            enabled: false
        };
        sections.push({
            title: `<?php echo _("Analytics"); ?>`,
            description: `<?php echo _("Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics such as the number of visitors, bounce rate, traffic source, etc."); ?>`,
            linkedCategory: "analytics"
        });
    }
    if(font_provider=='google' || window.comments!=0 || share_addtoany==1) {
        categories.functionality = {
            enabled: false
        }
        sections.push({
            title: `<?php echo _("Functional"); ?>`,
            description: `<?php echo _("Functional cookies help perform certain functionalities like sharing the content of the website on social media platforms, collecting feedback, and other third-party features."); ?>`,
            linkedCategory: "functionality"
        });
    }
    var cookie_name = 'cc_map_sn';
    if(ga_tracking_id!='')  cookie_name+='ga';
    if(font_provider=='google') cookie_name+='fg';
    if(share_addtoany==1) cookie_name+='as';

    var handleConsentChange = function(data){
        var categories = (data && data.changedCategories)
            ? data.changedCategories
            : CookieConsent.getUserPreferences().acceptedCategories;
        var services = (data && data.changedServices)
            ? data.changedServices
            : CookieConsent.getUserPreferences().acceptedServices;
        if(categories.includes('functionality')){
            if(services['functionality'].includes('Google Fonts')){
                if(!CookieConsent.acceptedService('Google Fonts', 'functionality')){
                    $('#font_viewer_link').remove();
                }
            }
            if(services['functionality'].includes('Social Share (AddToAny)')){
                if(!CookieConsent.acceptedService('Social Share (AddToAny)', 'functionality')){
                    $('.btn_share_map').addClass('disabled_cookie');
                    $('.btn_share_cookie_denied').show();
                    $('.share_section .share-buttons').hide();
                } else {
                    $('.btn_share_map').removeClass('disabled_cookie');
                    $('.btn_share_cookie_denied').hide();
                    $('.share_section .share-buttons').show();
                }
            }
        }
        if(categories.includes('analytics')){
            if(services['analytics'].includes('Google Analytics')){
                if(!CookieConsent.acceptedService('Google Analytics', 'analytics')){
                    CookieConsent.eraseCookies(['_gid', /^_ga/], '/', location.hostname);
                }
            }
        }
    };

    CookieConsent.run({
        onChange: handleConsentChange,
        onFirstConsent: handleConsentChange,
        onConsent: handleConsentChange,
        onModalShow: function() {
            $('#cookie_consent_preferences').hide();
        },
        onModalReady: function() {
            $('#cookie_consent_preferences').hide();
        },
        onModalHide: function() {
            $('#cookie_consent_preferences').show();
            $('#cookie_consent_preferences span').css('display','inline-block');
            setTimeout(function() {
                $('#cookie_consent_preferences span').css('display','');
            },1000);
        },
        cookie: {
            name: cookie_name
        },
        guiOptions: {
            consentModal: {
                layout: "box",
                position: "bottom right",
                equalWeightButtons: false,
                flipButtons: false
            },
            preferencesModal: {
                layout: "box",
                position: "right",
                equalWeightButtons: false,
                flipButtons: false
            }
        },
        categories: categories,
        language: {
            default: "en",
            translations: {
                en: {
                    consentModal: {
                        title: `<?php echo _("We value your privacy!"); ?>`,
                        description: `<?php echo _("We use cookies to enhance your browsing experience, serve personalized content, and analyze our traffic. By clicking \"Accept All\", you consent to our use of cookies."); ?>`,
                        acceptAllBtn: `<?php echo _("Accept all"); ?>`,
                        acceptNecessaryBtn: `<?php echo _("Reject all"); ?>`,
                        showPreferencesBtn: `<?php echo _( "Manage preferences"); ?>`,
                        footer: footer
                    },
                    preferencesModal: {
                        title: `<?php echo _("Cookie Preferences"); ?>`,
                        acceptAllBtn: `<?php echo _("Accept all"); ?>`,
                        acceptNecessaryBtn: `<?php echo _("Reject all"); ?>`,
                        savePreferencesBtn: `<?php echo _("Save preferences"); ?>`,
                        closeIconLabel: `<?php echo _("Close modal"); ?>`,
                        serviceCounterLabel: `<?php echo _("Service|Services"); ?>`,
                        sections: sections
                    }
                }
            }
        }
    });
    if(!CookieConsent.acceptedService('Social Share (AddToAny)', 'functionality')){
        $('.btn_share_map').addClass('disabled_cookie');
        $('.btn_share_cookie_denied').show();
        $('.share_section .share-buttons').hide();
    }
</script>