(function () {
    try {
        function findTabsWrapper() {
            var selectors = ['.woocommerce-tabs', '.wc-tabs-wrapper', '.woocommerce-tabs-wrapper'];

            for (var i = 0; i < selectors.length; i++) {
                var el = document.querySelector(selectors[i]);

                if (el) {
                    return el;
                }
            }

            return null;
        }

        function findAttributesTables(scope) {
            var root = scope || document;

            return Array.prototype.slice.call(
                root.querySelectorAll(
                    'table.shop_attributes, .woocommerce-product-attributes, table.woocommerce-product-attributes'
                )
            );
        }

        function findPanel(wrapper, selectors) {
            if (!wrapper) {
                return null;
            }

            var els = wrapper.querySelectorAll(selectors);

            return els.length ? els[0] : null;
        }

        function ensureTabsOrder(wrapper) {
            if (!wrapper) {
                return;
            }

            var list = wrapper.querySelector('ul.wc-tabs, ul.tabs, .woocommerce-tabs ul');

            if (!list) {
                return;
            }

            var items = Array.prototype.slice.call(list.querySelectorAll('li'));
            var descLi = null;
            var addLi = null;

            items.forEach(function (li) {
                var a = li.querySelector('a[href^="#"]');

                if (!a) {
                    return;
                }

                var href = (a.getAttribute('href') || '').toLowerCase();

                if (href.indexOf('description') !== -1) {
                    descLi = li;
                } else if (href.indexOf('additional') !== -1) {
                    addLi = li;
                }
            });

            if (descLi && list.firstElementChild !== descLi) {
                list.insertBefore(descLi, list.firstElementChild);
            }

            if (addLi && descLi && addLi.previousElementSibling !== descLi) {
                list.insertBefore(addLi, descLi.nextElementSibling);
            }

            items.forEach(function (li) {
                li.classList.remove('active');

                var a = li.querySelector('a[href^="#"]');

                if (!a) {
                    return;
                }

                var panel = document.querySelector(a.getAttribute('href'));

                if (panel) {
                    panel.classList.remove('active');
                }
            });

            if (descLi) {
                descLi.classList.add('active');

                var aDesc = descLi.querySelector('a[href^="#"]');

                if (aDesc) {
                    var descPanel = document.querySelector(aDesc.getAttribute('href'));

                    if (descPanel) {
                        descPanel.classList.add('active');
                    }
                }
            }
        }

        function normalizeAttributesLocation() {
            var wrapper = findTabsWrapper();

            if (!wrapper) {
                return;
            }

            var descPanel = findPanel(wrapper, '.woocommerce-Tabs-panel--description, #tab-description');
            var addPanel = findPanel(wrapper, '.woocommerce-Tabs-panel--additional_information, #tab-additional_information');
            var tables = findAttributesTables(document);

            if (!tables.length) {
                return;
            }

            var canonical = tables[0];

            if (descPanel && canonical && descPanel.contains(canonical) && addPanel) {
                addPanel.appendChild(canonical);
            }

            tables.forEach(function (tbl) {
                if (tbl !== canonical && tbl.parentNode) {
                    tbl.parentNode.removeChild(tbl);
                }
            });

            if (descPanel) {
                findAttributesTables(descPanel).forEach(function (tbl) {
                    if (tbl.parentNode) {
                        tbl.parentNode.removeChild(tbl);
                    }
                });
            }

            ensureTabsOrder(wrapper);
        }

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

        if (window.MutationObserver) {
            new MutationObserver(normalizeAttributesLocation)
                .observe(document.documentElement, { childList: true, subtree: true });
        }
    } catch (e) {
        if (window.console && console.error) {
            console.error('[REEID WC ATTRS]', e);
        }
    }
})();