function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
import React from 'react';
import { useTimer } from './Timer';

/* TYPES */

function noop() {}
function returnTrue() {
  return true;
}

/**
 * @deprecatedSince 9
 * @deprecated Never officially documented/supported
 */
export function isPromise(obj) {
  return _typeof(obj) === 'object' && typeof obj.then === 'function';
}

/**
 * @deprecatedSince 9
 * @deprecated Never officially documented/supported
 */
export function wrapPromise(result) {
  if (isPromise(result)) {
    return result;
  }
  if (typeof result === 'boolean') {
    return Promise.resolve(result);
  }
  return Promise.resolve(true);
}

/* HOOK */

/**
 * @deprecatedSince 9
 * @deprecated Never officially documented/supported
 */
export function useDelayedCallback(_ref) {
  var _ref$after = _ref.after,
    after = _ref$after === void 0 ? noop : _ref$after,
    _ref$before = _ref.before,
    before = _ref$before === void 0 ? returnTrue : _ref$before,
    _ref$delay = _ref.delay,
    delay = _ref$delay === void 0 ? 0 : _ref$delay,
    timer = _ref.timer;
  var defaultTimer = useTimer({});
  var t = timer || defaultTimer;
  React.useEffect(function () {
    return function () {
      // cancel timeout on unmount
      t.cancel();
    };
  }, []);
  return React.useCallback(function (event) {
    return wrapPromise(before(event)).then(function (shouldUpdate) {
      return shouldUpdate && t.setTimer(function () {
        return after(event);
      }, delay);
    });
  }, [delay, before, after, t]);
}
//# sourceMappingURL=DelayedCallback.js.map