"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.debounceFilter = debounceFilter;
exports.default = debouncedWatch;
exports.watchWithFilter = watchWithFilter;
var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
var _vueDemi = require("vue-demi");
// copy from https://github.dev/vueuse/vueuse
var __rest = void 0 && (void 0).__rest || function (s, e) {
  var t = {};
  for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p];
  if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
    if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]];
  }
  return t;
};
const bypassFilter = invoke => {
  return invoke();
};
/**
 * Create an EventFilter that debounce the events
 *
 * @param ms
 */
function debounceFilter(ms) {
  let timer;
  const filter = invoke => {
    const duration = (0, _vueDemi.unref)(ms);
    if (timer) clearTimeout(timer);
    if (duration <= 0) return invoke();
    timer = setTimeout(invoke, duration);
  };
  return filter;
}
/**
 * @internal
 */
function createFilterWrapper(filter, fn) {
  function wrapper() {
    for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
      args[_key] = arguments[_key];
    }
    filter(() => fn.apply(this, args), {
      fn,
      thisArg: this,
      args
    });
  }
  return wrapper;
}
// implementation
function watchWithFilter(source, cb) {
  let options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
  const {
      eventFilter = bypassFilter
    } = options,
    watchOptions = __rest(options, ["eventFilter"]);
  return (0, _vueDemi.watch)(source, createFilterWrapper(eventFilter, cb), watchOptions);
}
// implementation
function debouncedWatch(source, cb) {
  let options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
  const {
      debounce = 0
    } = options,
    watchOptions = __rest(options, ["debounce"]);
  return watchWithFilter(source, cb, (0, _extends2.default)((0, _extends2.default)({}, watchOptions), {
    eventFilter: debounceFilter(debounce)
  }));
}