var Wick = Wick || {};

Wick.Scrollbar = (function () {
  var $filters = $('.filter-results'),
      visibleFilterItemsNumber = 5,
      scrollBarSelector = '.custom-scroll';

  // Init simple bar on accordion filters
  if ($filters.length) {
    $filters.find(scrollBarSelector).each(function (key, value) {
      var filterItemsNumber = $(value).children().length;
      if (filterItemsNumber > visibleFilterItemsNumber) {
        new SimpleBar(value, {autoHide: false});
      }
    });
  }

  // safari-mobile bug fix for accordion filters. TODO: need to find another implementation in future
  if ($(scrollBarSelector).length) {
    $('.simplebar-scroll-content').on('touchmove', _.debounce(function () {
      if ($(document.body).hasClass('is-filters-visible')) {
        $('html, body').scrollTop(window.pageYOffset);
      }
    }, 100));
  }
})();
