function debounce(callBack, timeoutMs) {
   return function perform(...args) {
      let previousCall = this.lastCall;

      this.lastCall = Date.now();

      if (previousCall && this.lastCall - previousCall <= timeoutMs) {
         clearTimeout(this.lastCallTimer);
      }

      this.lastCallTimer = setTimeout(() => callBack(...args), timeoutMs);
   };
}

function isNodeList(field) {
   return NodeList.prototype.isPrototypeOf(field);
}

function removeSelector(nodeList, className = "active") {
   nodeList.forEach((node) => {
      node.classList.remove(className);
   });
}

function showLoader(wrapper = document.body) {
   let loader = wrapper.querySelector(".loader");

   if (!loader) {
      loader = document.createElement("div");
      loader.classList.add("loader");
      wrapper.append(loader);
   }

   loader.classList.add("active");
}

function hideLoader(wrapper = document.body) {
   let loader = wrapper.querySelector(".loader");

   if (loader) {
      loader.classList.remove("active");
   }
}
export { debounce, hideLoader, isNodeList, removeSelector, showLoader };
