var Wick = Wick || {};
Wick.BackToTop = (function () {
  let $backToTop = $('#back-to-top'),
    scrollTrigger = 200,
    actionSpeed = 700,
    chatBotBtnTag = 'wickes-chat',
    backToTopWithChatClass = 'back-to-top--with-chat';

  function checkScrollPosition(){
    $(window).scrollTop() > scrollTrigger ? $backToTop.fadeIn(300) : $backToTop.fadeOut(300);
  }

  function clickHandler(){
    $backToTop.click(function () {
      $backToTop.prop("disabled", true);

      setTimeout(function () {
        $backToTop.prop("disabled", false);
      }, actionSpeed);

      $('html, body').animate({
        scrollTop: 0
      }, actionSpeed);
    });
  }

  function chatBotClassHandler(){
    window.onload = function(){
      let chatBotElement = $(chatBotBtnTag);
      if(chatBotElement.length) {
        $backToTop.addClass(backToTopWithChatClass);
      }
    }
  }

  function init(){
    $(window).on('scroll', _.debounce(checkScrollPosition, 200));

    clickHandler();

    chatBotClassHandler();
  }

  init();
})();
