{% load static sekizai_tags %}

{% addtoblock 'js' %}
    {#<script src="https://cc.cdn.civiccomputing.com/8/cookieControl-8.x.min.js"></script>#}
    <script src="{% static 'django_cookie_control/cookie-control/cookieControl-8.x.min.js' %}"></script>
{% endaddtoblock %}

{% addtoblock 'js' %}
    {# replicates this functionality - https://cookie-script.com/how-to-block-third-party-cookies.html #}
    <script type="text/javascript">

        {# Disabled Attr, Enabled Attr, [Disabled Value], [Enabled Value]  #}
        {# (null values will be replaced by disabled attr)    #}
        var cookieAttrs = [
            ['data-src', 'src', null, null],
            ['data-href', 'href', null, null],
            ['type', 'type', 'text/plain', 'text/javascript']
            ];

        function enableCookies(attrPrefix) {
            switchCookies(attrPrefix, true);
        }
        function disableCookies(attrPrefix) {
            switchCookies(attrPrefix, false);
        }

        function switchCookies(attrPrefix, enable) {
            // Cookie selector e.g. '[marketing-]cookie-control'
            if (typeof(attrPrefix) === 'undefined') {
                attrPrefix = '';
            }
            var selector = attrPrefix + 'cookie-control';

            // Enable or disable cookie elements
            if (typeof(enable) === 'undefined') {
                enable = false;
            }

            var cookieControls = document.querySelectorAll('[' + selector + ']');
            fromAttr = enable ? 0 : 1;
            toAttr = enable ? 1 : 0;
            fromVal = enable ? 2 : 3;
            toVal = enable ? 3 : 2;
            enableStr = enable ? 'Enabling' : 'Disabling';
            cookieControls.forEach(function (cookieElem) {
                cookieAttrs.forEach(function (cookieAttr) {
                    // For iframe/embed/object with data-src
                    if (cookieElem.hasAttribute(cookieAttr[fromAttr])) {
                        console.log(enableStr + " content for: " + cookieElem.getAttribute(cookieAttr[fromAttr]));
                        var from_attr = cookieAttr[fromAttr];
                        var to_attr = cookieAttr[toAttr];
                        var to_val = cookieAttr[toVal] ? cookieAttr[toVal] : cookieElem.getAttribute(from_attr);
                        cookieElem.setAttribute(to_attr, to_val);
                        cookieElem.removeAttribute(from_attr);
                    }
                });

            });
        }
    </script>
{% endaddtoblock %}

{% addtoblock 'js' %}
    {% if COOKIE_CONTROL_CONFIG %}
    <script type="text/javascript">
        var config = {{ COOKIE_CONTROL_CONFIG|safe }};
        CookieControl.load( config );
   </script>
    {% endif %}
{% endaddtoblock %}
