(function ($, Drupal) {
  Drupal.behaviors.tantalum = {
    attach(context) {
      // Show/hide share buttons.
      $("#share-social-media", context)
        .once()
        .each(function () {
          $(this).click(function () {
            $(".block-simple-blockshare-buttons").toggle();
            if ($("#share-social-media").attr("aria-expanded") === "true") {
              $("#share-social-media").attr("aria-expanded", "false");
              $(".block-simple-blockshare-buttons").attr("aria-hidden", "true");
            } else {
              $("#share-social-media").attr("aria-expanded", "true");
              $(".block-simple-blockshare-buttons").attr("aria-hidden", "false");
              $(".a2a_button_facebook").focus();
            }
          });
        });

      var custom_interval = setInterval(function () {
        $(".layerTile").each(function (index) {
          if ($(this).attr("alt") == "" || typeof $(this).attr("alt") === "undefined") {
            $(this).attr("alt", `layer ${index}`);
            clearInterval(custom_interval);
          }
        });
      }, 500);

      $(".diff-controls__item").each(function () {
        const title = $("label", this).text();
        if (title === "View mode") {
          $(this).hide();
        }
      });

      // Any time WYSIWYG W/Media is present, look for embedded media and grab
      // the view mode to add to the figure wrapper. Way easier to handle styling.
      const embeddedMedia = $(".field-text-with-media figure > .d-media");
      if ($(embeddedMedia).length !== 0) {
        $(embeddedMedia).each(function () {
          const media = $(this);
          const classes = $(media).attr("class").split(" ");
          $.each(classes, function (index, item) {
            if (item.includes("view-mode")) {
              const viewModeClass = item.replace("media--view-mode--", "");
              $(media).closest("figure").addClass(viewModeClass);
            }
          });
        });
      }
    },
  };

  // Toggles Explore Search section if there are more than 5 terms
  // Special logic needed for layouts with tabs/accordions
  const tabScienceTerms = $(".usgs-tabs-wrapper .c-usgs-explore-science ul").find("li");
  const accordionScienceTerms = $(".usgs-accordion-wrapper .c-usgs-explore-science ul").find("li");
  const scienceTerms = $(".c-usgs-explore-science ul").find("li");
  const tabWrapper = ".node--type--science .usgs-tabs-wrapper";
  const accordionWrapper = ".node--type--science .usgs-accordion-wrapper";
  const wrapper = "";

  function hideKeywords($element, $type) {
    if ($element.length > 5) {
      $(".d-usgs-keyword-more").show();
      $element.each(function (i) {
        if (i > 4) {
          $(this).hide();
        }
      });

      $(`${$type} .d-usgs-keyword-more`).click(function (event) {
        event.preventDefault();
        $element.each(function (i) {
          if (i > 4) {
            $(this).toggle();
          }
        });
        if ($(this).find("i").hasClass("fa-arrow-down")) {
          $(this).find("i").removeClass("fa-arrow-down").addClass("fa-arrow-up");
        } else if ($(this).find("i").hasClass("fa-arrow-up")) {
          $(this).find("i").removeClass("fa-arrow-up").addClass("fa-arrow-down");
        }
      });
    }
  }

  if ($(tabWrapper).length || $(accordionWrapper).length) {
    // Hide keywords on desktop tabs
    hideKeywords(tabScienceTerms, tabWrapper);
    // Hide keywords on mobile tabs
    hideKeywords(accordionScienceTerms, accordionWrapper);
  } else {
    // Hide keywords on mobile tabs
    hideKeywords(scienceTerms, wrapper);
  }

  $(".fas.fa-share-alt").click(function () {
    $(".block-addtoany-block").toggle();
  });

  // On a group homepage, if there is nothing in the group body, then there shouldn't be any bottom border on info-link block
  if ($(".group-home-content").length) {
    if ($.trim($(".group-home-content").text()).length == 0 && $(".info-links").length) {
      $(".info-links").css("border-bottom", "none");
      // Mobile
      $(".info-links .field-last .paragraph").css("border-bottom", "none");
    }
  }

  // If LP does not have info links and the view is empty, hide the bottom boder
  if ($(".node--type--landing-page").length) {
    if (!$(".node-intro").hasClass(".info-links") && $(".view-group-lp.view-empty").length) {
      $(".intro-cta").css("border-bottom", "none");
    }
  }

  // Placeholder test for sidebar map
  function openModal(modalUrl) {
    const ajaxSettings = {
      url: modalUrl,
      dialogType: "modal",
      dialog: { width: 1000 },
    };
    Drupal.ajax(ajaxSettings).execute();
  }
  $(".modal-popup-link").on("click", function () {
    const modalUrl = $(this).attr("data-link");
    if (modalUrl) {
      openModal(modalUrl);
    }
  });
})(jQuery, Drupal);
