import { useState, useCallback } from "react";

/**
 * @example
 * const [value, toggle, toggleOn, toggleOff]  = useToggle(false);
 *
 * @param {Boolean} initialValue
 */

export default (function (initialValue, _temp) {
  var _ref = _temp === void 0 ? {} : _temp,
    on = _ref.on,
    off = _ref.off,
    onToggle = _ref.toggle;
  var _useState = useState(!!initialValue),
    value = _useState[0],
    setToggle = _useState[1];
  var toggle = useCallback(function (val) {
    return setToggle(function (oldValue) {
      var nextValue = typeof val === "boolean" ? val : !oldValue;
      if (onToggle) onToggle(nextValue);
      if (on && nextValue) on();
      if (off && !nextValue) off();
      return nextValue;
    });
  }, [onToggle, on, off]);
  var toggleOn = useCallback(function () {
    setToggle(true);
    if (on) on();
  }, [on]);
  var toggleOff = useCallback(function () {
    setToggle(false);
    if (off) off();
  }, [off]);
  return [value, toggle, toggleOn, toggleOff];
});