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

    callback();
  }

  onReady(function () {
    if (!document.body || !document.body.classList.contains('afb-app')) {
      return;
    }

    var collapseKey = 'afb_sidebar_collapsed';
    var body = document.body;

    var collapseToggle = document.getElementById('afb-sidebar-toggle');
    var collapseIcon = document.getElementById('afb-sidebar-toggle-icon');

    var mobileToggle = document.getElementById('afb-mobile-toggle');
    var overlay = document.getElementById('afb-shell-overlay');

    var desktopQuery = window.matchMedia('(min-width: 1025px)');

    function setCollapseIcon(collapsed) {
      if (!collapseIcon) {
        return;
      }

      collapseIcon.className = collapsed
        ? 'dashicons dashicons-arrow-right-alt2'
        : 'dashicons dashicons-arrow-left-alt2';
    }

    function setCollapsed(collapsed, persist) {
      body.classList.toggle('afb-sidebar-collapsed', collapsed);
      setCollapseIcon(collapsed);

      if (collapseToggle) {
        collapseToggle.setAttribute('aria-pressed', collapsed ? 'true' : 'false');
      }

      if (persist !== false) {
        try {
          window.localStorage.setItem(collapseKey, collapsed ? '1' : '0');
        } catch (error) {
          // Ignore localStorage failures.
        }
      }
    }

    function readCollapsedState() {
      try {
        return window.localStorage.getItem(collapseKey) === '1';
      } catch (error) {
        return false;
      }
    }

    function openMobileSidebar() {
      body.classList.add('afb-sidebar-open');
      if (mobileToggle) {
        mobileToggle.setAttribute('aria-expanded', 'true');
      }
    }

    function closeMobileSidebar() {
      body.classList.remove('afb-sidebar-open');
      if (mobileToggle) {
        mobileToggle.setAttribute('aria-expanded', 'false');
      }
    }

    if (desktopQuery.matches) {
      setCollapsed(readCollapsedState(), false);
    } else {
      setCollapseIcon(false);
    }

    if (collapseToggle) {
      collapseToggle.addEventListener('click', function () {
        if (!desktopQuery.matches) {
          return;
        }

        setCollapsed(!body.classList.contains('afb-sidebar-collapsed'));
      });
    }

    if (mobileToggle) {
      mobileToggle.addEventListener('click', function () {
        if (body.classList.contains('afb-sidebar-open')) {
          closeMobileSidebar();
          return;
        }

        openMobileSidebar();
      });
    }

    if (overlay) {
      overlay.addEventListener('click', closeMobileSidebar);
    }

    window.addEventListener('resize', function () {
      if (desktopQuery.matches) {
        closeMobileSidebar();
        setCollapsed(readCollapsedState(), false);
      } else {
        body.classList.remove('afb-sidebar-collapsed');
        setCollapseIcon(false);
      }
    });
  });
})();
