(($, Drupal, once) => {
  Drupal.behaviors.sobkiAdminTabs = {
    attach(context) {
      $('.sobki-primary-tab-wrapper:not(#sobki-primary-tab-content)').hide();
      $('.sobki-secondary-tab-wrapper:not(#sobki-secondary-tab-block)').hide();

      // Set primary tabs behavior.
      once('sobkiAdminPrimaryTabs', 'a.sobki-primary-tab', context).forEach(
        (link) => {
          $(link).click((e) => {
            this.setActiveTab(e, 'primary');
          });
        },
      );

      // Set secondary tabs behavior.
      once('sobkiAdminSecondaryTabs', 'a.sobki-secondary-tab', context).forEach(
        (link) => {
          $(link).click((e) => {
            this.setActiveTab(e, 'secondary');
          });
        },
      );
    },
    // Show the active tab and link.
    setActiveTab(e, type) {
      e.preventDefault();
      const a = e.target;
      const id = $(a).attr('href').substring(1);

      $(`.sobki-${type}-tab-wrapper`).hide();
      $(`#${id}`).show();
      $(`a.sobki-${type}-tab`).removeClass('active');
      $(a).addClass('active');
    },
  };
})(jQuery, Drupal, once);
