1/* global bootstrap */
2// noinspection ES6ConvertVarToLetConst
3window.combos = (function (module) {
4    module.popover = {
5        getDataNamespace: function () {
6            let defaultNamespace = "-bs";
7            let bootstrapVersion = 5;
8            if (typeof bootstrap.Popover.VERSION !== 'undefined') {
9                bootstrapVersion = parseInt(bootstrap.Popover.VERSION.substr(0, 1), 10);
10                if (bootstrapVersion < 5) {
11                    return "";
12                }
13                return defaultNamespace;
14            }
15            if (typeof jQuery != 'undefined' && typeof jQuery.fn.tooltip.constructor.VERSION !== 'undefined') {
16                bootstrapVersion = parseInt(jQuery.fn.tooltip.constructor.VERSION.substr(0, 1), 10);
17                if (bootstrapVersion < 5) {
18                    return "";
19                }
20                return defaultNamespace;
21            }
22            return defaultNamespace;
23        },
24        /**
25         *
26         * @param cssSelector - the popovers css selector to enable, if undefined, all
27         * element found with the `data[-bs]-toggle=popover` selector will be used
28         */
29        enable: function (cssSelector) {
30            let options = {};
31            if (typeof cssSelector === 'undefined') {
32                let namespace = this.getDataNamespace();
33                cssSelector = `[data${namespace}-toggle="popover"]`;
34            }
35            options.sanitize = false;
36            if (typeof bootstrap.Popover.VERSION !== 'undefined') {
37                document.querySelectorAll(cssSelector)
38                    .forEach(el => {
39                        let popover = bootstrap.Popover.getInstance(el);
40                        if (popover === null) {
41                            new bootstrap.Popover(el, options);
42                            // to not navigate on `a` anchor
43                            el.onclick = (event => event.preventDefault());
44                        }
45                    });
46                return;
47            }
48            if (typeof jQuery != 'undefined' && typeof jQuery.fn.tooltip.constructor.VERSION !== 'undefined') {
49                jQuery(cssSelector).popover(options);
50            }
51        }
52    }
53    return module;
54})(window.combos || {});
55