"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.debounce = void 0;

var debounce = function debounce(func, wait, immediate) {
  var timeout;
  var result;

  var debounced = function debounced() {
    var context = this;
    var args = arguments;
    if (timeout) clearTimeout(timeout);

    var later = function later() {
      timeout = null;
      if (!immediate) result = func.apply(context, args);
    };

    var callNow = immediate && !timeout;
    timeout = setTimeout(later, wait);
    if (callNow) result = func.apply(this, args);
    return result;
  };

  debounced.cancel = function () {
    clearTimeout(timeout);
    timeout = null;
  };

  return debounced;
};

exports.debounce = debounce;