/**
 * Gestion du module cookie_privacy
 * @returns {{openCnilPanel: openCnilPanel, manageGoogleMapsMap: manageGoogleMapsMap}}
 * @author ABT 23/03/2021
 */
var cookiePrivacy = function () {
    'use strict';

    return {
        module: '',

        /**
         * Initialise le module de gestion des cookies
         * @param {string} asModule Nom du module
         * @author ABT 23/03/2021
         */
        init: function (asModule) {
            this.module = asModule;
        },

        /**
         * Ouvre le panneau de gestion des cookies
         * @author ABT 23/03/2021
         */
        openCnilPanel: function () {
            switch (this.module) {
                case 'tarteaucitron':
                    window.location = window.tarteaucitron.hashtag;
                    break;
                default:
                    break;
            }
        },

        /**
         * Gère l'affichage des cartes Google Maps
         * @author ABT 23/03/2021
         */
        manageGoogleMapsMap: function (afBuildFunction, asCallbackName) {
            switch (this.module) {
                case 'tarteaucitron':
                    window.tarteaucitron.user.mapscallback = asCallbackName;

                    window[asCallbackName] = afBuildFunction();

                    if (!window.tarteaucitron.state.googlemaps) {
                        window.tarteaucitron.services.googlemaps.fallback();
                    } else {
                        if (typeof asCallbackName === "function") {
                            window[asCallbackName]();
                        }
                    }
                    break;
                default:
                    afBuildFunction();
                    break;
            }
        },
        sendGAPageView: function (a) {
            var checkExist = setInterval(function () {
                if ($('.TAC-done').length) {
                    ga('send', 'pageview', a);
                    clearInterval(checkExist);
                }
            }, 100);
        },
        sendGAEvent: function (a, b, c, d) {
            var checkExist = setInterval(function () {
                if ($('.TAC-done').length) {
                    ga('send', 'event', a, b, c, d);
                    clearInterval(checkExist);
                }
            }, 100);
        },
        sendGAVar: function (a, b) {
            var checkExist = setInterval(function () {
                if ($('.TAC-done').length) {
                    ga('set', a, b);
                    clearInterval(checkExist);
                }
            }, 100);
        },
        sendGAAddTrans: function (a) {
            var checkExist = setInterval(function () {
                if ($('.TAC-done').length) {
                    ga('ecommerce:addTransaction', a);
                    clearInterval(checkExist);
                }
            }, 100);
        },
        sendGAAddItem: function (a) {
            var checkExist = setInterval(function () {
                if ($('.TAC-done').length) {
                    ga('ecommerce:addItem', a);
                    clearInterval(checkExist);
                }
            }, 100);
        },
        sendGATrackTrans: function () {
            var checkExist = setInterval(function () {
                if ($('.TAC-done').length) {
                    ga('ecommerce:send');
                    clearInterval(checkExist);
                }
            }, 100);
        },
    }
};

window.goCookiePrivacy = new cookiePrivacy();
