var _excluded = ["children"],
  _excluded2 = ["children"];
function _objectWithoutProperties(e, t) { if (null == e) return {}; var o, r, i = _objectWithoutPropertiesLoose(e, t); if (Object.getOwnPropertySymbols) { var n = Object.getOwnPropertySymbols(e); for (r = 0; r < n.length; r++) o = n[r], -1 === t.indexOf(o) && {}.propertyIsEnumerable.call(e, o) && (i[o] = e[o]); } return i; }
function _objectWithoutPropertiesLoose(r, e) { if (null == r) return {}; var t = {}; for (var n in r) if ({}.hasOwnProperty.call(r, n)) { if (-1 !== e.indexOf(n)) continue; t[n] = r[n]; } return t; }
import React from 'react';
import { useDelayedCallback } from './DelayedCallback';
import { useTimer } from './Timer';

/* TYPES */

/**
 * @deprecatedSince 9
 * @deprecated Never officially documented/supported
 */

/**
 * @deprecatedSince 9
 * @deprecated Never officially documented/supported
 */

/**
 * @deprecatedSince 9
 * @deprecated Never officially documented/supported
 */

/**
 * @deprecatedSince 9
 * @deprecated Never officially documented/supported
 */

/* HOOK */

/**
 * @deprecatedSince 9
 * @deprecated Never officially documented/supported
 */
export function useDelayedToggle(_ref) {
  var _ref$beforeDisable = _ref.beforeDisable,
    beforeDisable = _ref$beforeDisable === void 0 ? function () {
      return true;
    } : _ref$beforeDisable,
    _ref$beforeEnable = _ref.beforeEnable,
    beforeEnable = _ref$beforeEnable === void 0 ? function () {
      return true;
    } : _ref$beforeEnable,
    _ref$disableDelay = _ref.disableDelay,
    disableDelay = _ref$disableDelay === void 0 ? 0 : _ref$disableDelay,
    _ref$enableDelay = _ref.enableDelay,
    enableDelay = _ref$enableDelay === void 0 ? 0 : _ref$enableDelay,
    _ref$isEnabled = _ref.isEnabled,
    isEnabled = _ref$isEnabled === void 0 ? false : _ref$isEnabled,
    onDisable = _ref.onDisable,
    onEnable = _ref.onEnable,
    _ref$timer = _ref.timer,
    timer = _ref$timer === void 0 ? useTimer({}) : _ref$timer;
  var handleEnable = React.useCallback(function () {
    timer.reset();
    return true;
  }, [timer]);
  var handleCancel = React.useCallback(function () {
    timer.cancel();
    return false;
  }, [timer]);
  var enable = useDelayedCallback({
    after: onEnable,
    before: isEnabled ? handleEnable : beforeEnable,
    delay: enableDelay,
    timer: timer
  });
  var disable = useDelayedCallback({
    after: onDisable,
    before: isEnabled ? beforeDisable : handleCancel,
    delay: disableDelay,
    timer: timer
  });
  return {
    disable: disable,
    enable: enable,
    toggle: isEnabled ? disable : enable
  };
}

/* RENDER PROP */

/**
 * @deprecatedSince 9
 * @deprecated Never officially documented/supported
 */
export var DelayedToggle = /*#__PURE__*/React.forwardRef(function DelayedToggle(props, ref) {
  var children = props.children,
    config = _objectWithoutProperties(props, _excluded);
  var toggle = useDelayedToggle(config);
  var element = typeof children === 'function' ? children(toggle) : children;
  return element && /*#__PURE__*/React.cloneElement(element, {
    ref: ref
  });
});

/* CONTEXT */

var noop = function noop(method) {
  return function () {
    return console.warn("Could not call `".concat(method, "`, please wrap your component in an `<DelayedToggleProvider>`"));
  };
};

/**
 * @deprecatedSince 9
 * @deprecated Never officially documented/supported
 */
export var initDelayedToggle = {
  disable: noop('disable'),
  enable: noop('enable'),
  toggle: noop('toggle')
};

/**
 * @deprecatedSince 9
 * @deprecated Never officially documented/supported
 */
export var DelayedToggleContext = /*#__PURE__*/React.createContext(initDelayedToggle);

/**
 * @deprecatedSince 9
 * @deprecated Never officially documented/supported
 */
export function DelayedToggleProvider(_ref2) {
  var children = _ref2.children,
    props = _objectWithoutProperties(_ref2, _excluded2);
  return /*#__PURE__*/React.createElement(DelayedToggleContext.Provider, {
    value: useDelayedToggle(props)
  }, children);
}

/**
 * @deprecatedSince 9
 * @deprecated Never officially documented/supported
 */
export function useDelayedToggleContext() {
  return React.useContext(DelayedToggleContext);
}
//# sourceMappingURL=DelayedToggle.js.map