/**
 * @file
 * NodeHive Admin Sidebar behavior.
 */

(function (Drupal, drupalSettings, once) {
  'use strict';

  Drupal.behaviors.nodehiveAdminSidebar = {
    attach: function (context) {
      once('nodehive-sidebar', '#nodehive-sidebar', context).forEach(function () {
        requestAnimationFrame(function () {
          requestAnimationFrame(function () {
            document.documentElement.classList.add('nh-sidebar-ready');
          });
        });
      });

      once('nh-space-screenshot', '.nh-sidebar__space-screenshot', context).forEach(function (img) {
        var preview = img.closest('.nh-sidebar__space-preview');
        if (!preview) return;
        if (img.complete && img.naturalWidth > 0) {
          preview.classList.add('is-loaded');
        } else {
          img.addEventListener('load', function () {
            preview.classList.add('is-loaded');
          });
        }
      });
    }
  };

})(Drupal, drupalSettings, once);
