{"version":3,"file":"ThirdPartyLoader.js","names":["ThirdPartyLoader","a","setters","Component","default","deepMerge","stringify","loadjs","Event","execute","constructor","element","options","arguments","length","url","bundleId","params","dataAttributes","loadScriptTimeout","checkConsent","consentGroup","path","initState","state","loaded","loading","data","loadScript","window","OnetrustActiveGroups","includes","on","Promise","resolve","reject","Error","isDefined","ready","onScriptLoaded","bind","callbacks","success","error","onScriptLoadFailed","before","scriptEl","onBeforeScriptLoad","console","message","Object","keys","forEach","key","setAttribute","afterInit","setTimeout"],"sources":["components/global/ThirdPartyLoader.js"],"sourcesContent":["import Component from 'core/Component';\nimport { deepMerge } from 'toolbox/deepMerge';\nimport { stringify } from 'toolbox/queryString';\nimport loadjs from 'loadjs';\nimport { Event } from 'services/EventEmitter';\n\n/**\n * @class\n * @classdesc This component is intented to be used as third party component wrapper\n * @extends Component\n */\nexport default class ThirdPartyLoader extends Component {\n    /**\n     * Constructor of the class that mainly merge the options of the components\n     * @param {HTMLElement} element HTMLElement of the component\n     * @param {Object} options options that belongs to the component\n     */\n    constructor(element, options = {}) {\n        super(element, deepMerge({\n            url: null, // url of the third-party service to call\n            bundleId: null, // id used to avoid duplicate scripts. If null, no duplicate check will be done.\n            params: null, // params object to be transformed into a query string\n            dataAttributes: null, // Extra data attributes to be passed to the thirdParty script\n            loadScriptTimeout: 0, // timeout for the loadScript function call\n            checkConsent: false, // check if the consent is given before loading the script\n            consentGroup: null, // consent group to check\n        }, options));\n    }\n\n    /**\n     * Get path\n     */\n    get path() {\n        const { url, params } = this.options;\n        let path = url;\n\n        // If params is set and has to be an object\n        if (params && typeof params === 'object') {\n            // we stringify the params and concat with the url\n            path += `?${stringify(params)}`;\n        }\n\n        return path;\n    }\n\n    /**\n     * Init the different state of the component\n     * It helps to avoid heavy DOM manipulation\n     */\n    initState() {\n        this.state.loaded = false;\n        this.state.loading = false;\n        this.state.data = null;\n    }\n\n    /**\n     * Load the third-party script\n     * @returns {Promise} - promise object\n     */\n    loadScript() {\n        if (!this.path) {\n            return Promise.reject(new Error('URL is missing'));\n        }\n\n        if (this.options.checkConsent && !window.OnetrustActiveGroups.includes(this.options.consentGroup)) {\n            // add listener to load the script when the consent will be given\n            Event.on('Onetrust.activegroups.updated', () => {\n                if (window.OnetrustActiveGroups.includes(this.options.consentGroup) && !this.state.loaded) {\n                    this.loadScript();\n                }\n            }, this);\n            return Promise.resolve(this.state.data);\n        }\n\n        if (this.state.loading) {\n            return Promise.reject(new Error('Script already loading'));\n        }\n\n        if (this.state.loaded) {\n            return Promise.resolve(this.state.data);\n        }\n\n        if (this.options.bundleId && loadjs.isDefined(this.options.bundleId)) {\n            loadjs.ready(this.options.bundleId, this.onScriptLoaded.bind(this));\n            return Promise.resolve(this.state.data);\n        }\n\n        this.state.loading = true;\n\n        return new Promise((resolve, reject) => {\n            const callbacks = {\n                success: (data) => {\n                    this.onScriptLoaded(data);\n                    resolve(data);\n                },\n                error: (error) => {\n                    this.onScriptLoadFailed(error);\n                    reject(error);\n                },\n                before: (path, scriptEl) => this.onBeforeScriptLoad(path, scriptEl),\n            };\n            if (this.options.bundleId) {\n                loadjs(this.path, this.options.bundleId, callbacks);\n            } else {\n                loadjs(this.path, callbacks);\n            }\n        });\n    }\n\n    /**\n     * On script load handler, called when the third-party is loaded\n     * @param {Object} data - Response of the provider\n     */\n    onScriptLoaded(data) {\n        this.state.loaded = true;\n        this.state.loading = false;\n        this.state.data = data;\n    }\n\n    /**\n     * On script loading failed handler\n     * @param { Object } error - error object\n     */\n    onScriptLoadFailed(error) {\n        this.state.loading = false;\n\n        if (error) {\n            console.error(error.message);\n        }\n    }\n\n    /**\n     * Before load script\n     * @param {String} path script path\n     * @param {Object} scriptEl script dom element\n     * @returns {Boolean} callback result\n     */\n    onBeforeScriptLoad(path, scriptEl) {\n        if (this.options.dataAttributes) {\n            Object.keys(this.options.dataAttributes).forEach((key) => {\n                scriptEl.setAttribute(key, this.options.dataAttributes[key]);\n            });\n        }\n\n        return true;\n    }\n\n    /**\n     * After init\n     * Run any script after the component is fully initialized\n     */\n    afterInit() {\n        if (this.options.loadScriptTimeout) {\n            setTimeout(() => {\n                this.loadScript();\n            }, this.options.loadScriptTimeout);\n        } else {\n            this.loadScript();\n        }\n    }\n}\n"],"mappings":"kKAWqBA,CAAgB,QAAAC,CAAA,oBAAAC,OAAA,WAAAD,CAAA,EAX9BE,CAAS,CAAAF,CAAA,CAAAG,OAAA,WAAAH,CAAA,EACPI,CAAS,CAAAJ,CAAA,CAATI,SAAS,WAAAJ,CAAA,EACTK,CAAS,CAAAL,CAAA,CAATK,SAAS,WAAAL,CAAA,EACXM,CAAM,CAAAN,CAAA,CAAAG,OAAA,WAAAH,CAAA,EACJO,CAAK,CAAAP,CAAA,CAALO,KAAK,GAAAC,OAAA,SAAAA,CAAA,EAAAR,CAAA,WAOOD,CAAgB,CAAtB,aAA+B,CAAAG,CAAU,CAMpDO,WAAWA,CAACC,CAAO,CAAgB,IAAd,CAAAC,CAAO,GAAAC,SAAA,CAAAC,MAAA,WAAAD,SAAA,IAAAA,SAAA,IAAG,CAAC,CAAC,CAC7B,KAAK,CAACF,CAAO,CAAEN,CAAS,CAAC,CACrBU,GAAG,CAAE,IAAI,CACTC,QAAQ,CAAE,IAAI,CACdC,MAAM,CAAE,IAAI,CACZC,cAAc,CAAE,IAAI,CACpBC,iBAAiB,CAAE,CAAC,CACpBC,YAAY,GAAO,CACnBC,YAAY,CAAE,IAClB,CAAC,CAAET,CAAO,CAAC,CACf,CAKA,GAAI,CAAAU,IAAIA,CAAA,CAAG,CACP,KAAM,CAAEP,GAAG,CAAHA,CAAG,CAAEE,MAAM,CAANA,CAAO,CAAC,CAAG,IAAI,CAACL,OAAO,CACpC,GAAI,CAAAU,CAAI,CAAGP,CAAG,CAQd,MALI,CAAAE,CAAM,EAAsB,QAAQ,EAA1B,MAAO,CAAAA,CAAmB,GAEpCK,CAAI,EAAK,IAAGhB,CAAS,CAACW,CAAM,CAAE,EAAC,EAG5BK,CACX,CAMAC,SAASA,CAAA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