(function () {
  'use strict';

  function ready(fn) {
    if (document.readyState === 'loading') {
      document.addEventListener('DOMContentLoaded', fn);
      return;
    }

    fn();
  }

  ready(function () {
    var tabButtons = document.querySelectorAll('.blr-ba-settings__tab');
    var tabPanels = document.querySelectorAll('.blr-ba-settings__tab-panel');

    if (tabButtons.length && tabPanels.length) {
      tabButtons.forEach(function (button) {
        button.addEventListener('click', function () {
          var target = button.getAttribute('data-tab-target');

          tabButtons.forEach(function (item) {
            item.classList.remove('is-active');
            item.setAttribute('aria-selected', 'false');
          });

          tabPanels.forEach(function (panel) {
            if (panel.getAttribute('data-tab-panel') === target) {
              panel.classList.add('is-active');
              panel.removeAttribute('hidden');
            } else {
              panel.classList.remove('is-active');
              panel.setAttribute('hidden', 'hidden');
            }
          });

          button.classList.add('is-active');
          button.setAttribute('aria-selected', 'true');
        });
      });
    }

    var sidebarLogoPreview = document.getElementById('blr-ba-sidebar-logo-preview');
    var removeSidebarLogoButton = document.getElementById('blr-ba-remove-sidebar-logo');

    if (sidebarLogoPreview) {
      var fallbackLogoUrl = sidebarLogoPreview.getAttribute('data-fallback-src') || '';
      var fallbackLogoAlt = sidebarLogoPreview.getAttribute('data-fallback-alt') || 'BLR Backline logo';
      var customLogoAlt = sidebarLogoPreview.getAttribute('data-custom-alt') || 'Sidebar logo preview';

      if (fallbackLogoUrl && (!sidebarLogoPreview.getAttribute('src') || sidebarLogoPreview.getAttribute('src').indexOf('/blr-icon.png') !== -1)) {
        sidebarLogoPreview.setAttribute('src', fallbackLogoUrl);
        sidebarLogoPreview.setAttribute('alt', fallbackLogoAlt);
      }

      if (removeSidebarLogoButton) {
        removeSidebarLogoButton.addEventListener('click', function () {
          window.setTimeout(function () {
            if (fallbackLogoUrl) {
              sidebarLogoPreview.setAttribute('src', fallbackLogoUrl);
              sidebarLogoPreview.setAttribute('alt', fallbackLogoAlt);
            }
          }, 0);
        });
      }

      sidebarLogoPreview.addEventListener('load', function () {
        if (fallbackLogoUrl && sidebarLogoPreview.getAttribute('src') !== fallbackLogoUrl) {
          sidebarLogoPreview.setAttribute('alt', customLogoAlt);
        }
      });
    }

    var docsLayouts = document.querySelectorAll('.blr-ba-docs-layout');

    docsLayouts.forEach(function (layout) {
      var docTabs = layout.querySelectorAll('.blr-ba-docs-tab');
      var docPanels = layout.querySelectorAll('.blr-ba-docs-panel');

      if (!docTabs.length || !docPanels.length) {
        return;
      }

      docTabs.forEach(function (tab) {
        tab.addEventListener('click', function () {
          var target = tab.getAttribute('data-doc-target');

          docTabs.forEach(function (item) {
            item.classList.remove('is-active');
            item.setAttribute('aria-selected', 'false');
          });

          docPanels.forEach(function (panel) {
            if (panel.getAttribute('data-doc-panel') === target) {
              panel.classList.add('is-active');
              panel.removeAttribute('hidden');
            } else {
              panel.classList.remove('is-active');
              panel.setAttribute('hidden', 'hidden');
            }
          });

          tab.classList.add('is-active');
          tab.setAttribute('aria-selected', 'true');
        });
      });
    });
  });
})();
