"use strict";

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

var _ahooks = require("ahooks");

var _react = require("react");

/* eslint-disable no-prototype-builtins */
function useControllableEventValue() {
  var props = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
  var defaultValue = options.defaultValue,
      _options$defaultValue = options.defaultValuePropName,
      defaultValuePropName = _options$defaultValue === void 0 ? 'defaultValue' : _options$defaultValue,
      _options$valuePropNam = options.valuePropName,
      valuePropName = _options$valuePropNam === void 0 ? 'value' : _options$valuePropNam,
      _options$trigger = options.trigger,
      trigger = _options$trigger === void 0 ? 'onChange' : _options$trigger;
  var value = props[valuePropName];
  var isControlled = props.hasOwnProperty(valuePropName);
  var initialValue = (0, _react.useMemo)(function () {
    if (isControlled) {
      return value;
    }

    if (props.hasOwnProperty(defaultValuePropName)) {
      return props[defaultValuePropName];
    }

    return defaultValue;
  }, []);
  var stateRef = (0, _react.useRef)(initialValue);

  if (isControlled) {
    stateRef.current = value;
  }

  var update = (0, _ahooks.useUpdate)();

  function setState(event) {
    if (!isControlled) {
      stateRef.current = event.target[valuePropName];
      update();
    }

    if (props[trigger]) {
      props[trigger](event);
    }
  }

  return [stateRef.current, (0, _ahooks.useMemoizedFn)(setState)];
}

var _default = useControllableEventValue;
exports.default = _default;