(function (window, document) {
    'use strict';

    var pending = false;
    var lastRun = 0;

    function normalizeFeatherSvg(svg) {
        if (!svg || !svg.classList) {
            return;
        }

        svg.classList.add('basketloot-icon-ready');
        svg.setAttribute('aria-hidden', svg.getAttribute('aria-hidden') || 'true');

        if (!svg.getAttribute('focusable')) {
            svg.setAttribute('focusable', 'false');
        }
    }

    function replaceIcons(root) {
        if (!window.feather || typeof window.feather.replace !== 'function') {
            return;
        }

        root = root || document;

        try {
            window.feather.replace({
                width: 18,
                height: 18,
                'stroke-width': 2.2
            });
        } catch (error) {
            return;
        }

        root.querySelectorAll('svg.feather').forEach(normalizeFeatherSvg);
        lastRun = Date.now();
    }

    function schedule(root) {
        if (pending) {
            return;
        }

        pending = true;

        window.requestAnimationFrame(function () {
            pending = false;
            replaceIcons(root || document);
        });
    }

    function watchDom() {
        if (!window.MutationObserver || !document.body) {
            return;
        }

        var observer = new MutationObserver(function (mutations) {
            var shouldReplace = mutations.some(function (mutation) {
                if (!mutation.addedNodes || !mutation.addedNodes.length) {
                    return false;
                }

                return Array.prototype.some.call(mutation.addedNodes, function (node) {
                    if (!node || node.nodeType !== 1) {
                        return false;
                    }

                    return node.matches('[data-feather], i[class*="fa-"]')
                        || node.querySelector('[data-feather], i[class*="fa-"]');
                });
            });

            if (shouldReplace && Date.now() - lastRun > 25) {
                schedule(document);
            }
        });

        observer.observe(document.body, {
            childList: true,
            subtree: true
        });
    }

    function bindIntegrations() {
        document.addEventListener('shown.bs.modal', function (event) {
            schedule(event.target || document);
        });

        document.addEventListener('hidden.bs.modal', function () {
            schedule(document);
        });

        document.addEventListener('livewire:load', function () {
            if (window.Livewire && typeof window.Livewire.hook === 'function') {
                window.Livewire.hook('message.processed', function () {
                    schedule(document);
                });
                window.Livewire.hook('element.updated', function (el) {
                    schedule(el || document);
                });
            }
        });

        if (window.jQuery) {
            window.jQuery(document).ajaxComplete(function () {
                schedule(document);
            });
        }
    }

    function boot() {
        replaceIcons(document);
        bindIntegrations();
        watchDom();
    }

    window.BasketLootIcons = {
        boot: boot,
        refresh: schedule
    };

    if (document.readyState === 'loading') {
        document.addEventListener('DOMContentLoaded', boot);
    } else {
        boot();
    }

    window.addEventListener('load', function () {
        schedule(document);
    });
})(window, document);
