var Wick = Wick || {};
Wick.Footer = (function(){
  let $accordion = $('.footer-menu__inner .accordion'),
      $accordionContent = $('.footer-menu__inner .accordion__content'),
      $paymentList = $('footer .payments__list'),
      $waysToPayList = $('footer .payways__list'),
      paymentSmallListClass = 'payments__list--small',
      waysToPaySmallListClass = 'payways__list--small',
      collapsedState = "accordion_collapsed",
      windowWidth = $(window).width();

  function accordionClickHandler() {
    if ($(this).find($accordionContent).is(':visible')) {
      $accordionContent.hide()
      $accordion.not($(this)).removeClass(collapsedState)
      $(this).find($accordionContent).show()
    }
  }

  function setPaymentListClass() {
    if ($paymentList.children().length < 6) {
      $paymentList.addClass(paymentSmallListClass)
    }
  }

  function setWaysToPayListClass() {
    if ($waysToPayList.children().length < 6 && windowWidth < 576) {
      $waysToPayList.addClass(waysToPaySmallListClass)
    }
  }

  function onWindowResize() {
    windowWidth = $(window).width();

    if ($waysToPayList.children().length < 6 && windowWidth > 576) {
      $waysToPayList.removeClass(waysToPaySmallListClass);
    } else if ($waysToPayList.children().length < 6) {
      $waysToPayList.addClass(waysToPaySmallListClass);
    }
  }
  function init () {
    $accordion.click(accordionClickHandler);

    if ($paymentList.children().length) {
      setPaymentListClass();
    }

    if ($waysToPayList.children().length) {
      setWaysToPayListClass();
      $(window).resize(_.debounce(onWindowResize, 200));
    }
  }

  init();
})();