var Wick = Wick || {};

Wick.HandleAccordions = {
  el: {
    $body: $('body'),
    $accordions: $(".account-nav .accordion"),
    accordionContent: '.accordion__content',
    accordionHeader: '.accordion__header',
    accordionCollapsed: 'accordion_collapsed',
  },
  handleOutsideClick() {
    document.addEventListener("click", function (event) {
      Wick.HandleAccordions.el.$accordions.each(function () {
        const $accordion = $(this);
        const $content = $accordion.find(Wick.HandleAccordions.el.accordionContent);
        const $toggle = $accordion.find(Wick.HandleAccordions.el.accordionHeader);

        const toggleButton = $toggle[0];
        const content = $content[0];

        if (!toggleButton || !content) {
          return;
        }

        if (!toggleButton.contains(event.target) && !content.contains(event.target)) {
          if ($content.is(":visible")) {
            $content.hide();
            $accordion.addClass(Wick.HandleAccordions.el.accordionCollapsed);
            $accordion.attr("aria-expanded", "false");
          }
        }
      });
    });
  }
};
