"use strict";

var _sliceInstanceProperty = require("@babel/runtime-corejs3/core-js-stable/instance/slice");
var _Array$from = require("@babel/runtime-corejs3/core-js-stable/array/from");
var _Symbol = require("@babel/runtime-corejs3/core-js-stable/symbol");
var _getIteratorMethod = require("@babel/runtime-corejs3/core-js/get-iterator-method");
var _interopRequireDefault = require("@babel/runtime-corejs3/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.useMergedState = useMergedState;
exports.useOnClickOutside = useOnClickOutside;
exports.usePrevious = usePrevious;
exports.useResizeObserver = useResizeObserver;
var _extends2 = _interopRequireDefault(require("@babel/runtime-corejs3/helpers/extends"));
var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime-corejs3/helpers/slicedToArray"));
var _react = require("react");
var _resizeObserverPolyfill = _interopRequireDefault(require("resize-observer-polyfill"));
var _devwarning = _interopRequireDefault(require("./devwarning"));
function _createForOfIteratorHelper(r, e) { var t = "undefined" != typeof _Symbol && _getIteratorMethod(r) || r["@@iterator"]; if (!t) { if (Array.isArray(r) || (t = _unsupportedIterableToArray(r)) || e && r && "number" == typeof r.length) { t && (r = t); var _n = 0, F = function F() {}; return { s: F, n: function n() { return _n >= r.length ? { done: !0 } : { done: !1, value: r[_n++] }; }, e: function e(r) { throw r; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var o, a = !0, u = !1; return { s: function s() { t = t.call(r); }, n: function n() { var r = t.next(); return a = r.done, r; }, e: function e(r) { u = !0, o = r; }, f: function f() { try { a || null == t.return || t.return(); } finally { if (u) throw o; } } }; }
function _unsupportedIterableToArray(r, a) { if (r) { var _context; if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = _sliceInstanceProperty(_context = {}.toString.call(r)).call(_context, 8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? _Array$from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } }
function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; }
/**
 * @description 点击指定区域外执行回调
 *
 * @export
 * @param {RefObject<HTMLElement>[]} refs
 * @param {(e: Event) => void} handler
 */
function useOnClickOutside(refs, handler) {
  (0, _react.useEffect)(function () {
    var listener = function listener(event) {
      var _iterator = _createForOfIteratorHelper(refs),
        _step;
      try {
        for (_iterator.s(); !(_step = _iterator.n()).done;) {
          var ref = _step.value;
          if (!ref.current || ref.current.contains(event.target)) {
            return;
          }
        }
      } catch (err) {
        _iterator.e(err);
      } finally {
        _iterator.f();
      }
      handler(event);
    };
    document.addEventListener('mousedown', listener);
    document.addEventListener('touchstart', listener);
    return function () {
      document.removeEventListener('mousedown', listener);
      document.removeEventListener('touchstart', listener);
    };
  }, [refs, handler]);
}
/**
 * @description 合并状态
 *
 * @export
 * @template T
 * @template R
 * @param {(T | (() => T))} defaultStateValue
 * @param {({
 *     defaultValue?: T | (() => T)
 *     value?: T
 *     onChange?: (value: T, prevValue: T) => void
 *     postState?: (value: T) => T
 *   })} [option]
 * @returns {[R, (value: T) => void]}
 */
function useMergedState(defaultStateValue,
// 初始state
option) {
  var _ref = option || {},
    defaultValue = _ref.defaultValue,
    value = _ref.value,
    onChange = _ref.onChange,
    postState = _ref.postState;
  var _useState = (0, _react.useState)(function () {
      if (value !== undefined) {
        return value;
      }
      if (defaultValue !== undefined) {
        return typeof defaultValue === 'function' ? defaultValue() : defaultValue;
      }
      return typeof defaultStateValue === 'function' ? defaultStateValue() : defaultStateValue;
    }),
    _useState2 = (0, _slicedToArray2.default)(_useState, 2),
    innerValue = _useState2[0],
    setInnerValue = _useState2[1];
  var mergedValue = value !== undefined ? value : innerValue;
  if (postState) {
    mergedValue = postState(mergedValue);
  }
  function triggerChange(newValue) {
    setInnerValue(newValue);
    if (mergedValue !== newValue && onChange) {
      onChange(newValue, mergedValue);
    }
  }
  var firstRenderRef = (0, _react.useRef)(true);
  (0, _react.useEffect)(function () {
    if (firstRenderRef.current) {
      firstRenderRef.current = false;
      return;
    }
    if (value === undefined) {
      setInnerValue(value);
    }
  }, [value]);
  return [mergedValue, triggerChange];
}
/**
 * @description 获取之前的值
 *
 * @export
 * @param {P} value
 */
function usePrevious(value) {
  var ref = (0, _react.useRef)(value);
  (0, _react.useEffect)(function () {
    ref.current = value;
  }, [value]);
  return ref.current;
}
function useResizeObserver(element, handler) {
  (0, _react.useEffect)(function () {
    if (!element) {
      (0, _devwarning.default)(!element && element !== null, 'useResizeMeasure', 'useResizeMeasure指定的元素不存在');
      return;
    }
    var measure = function measure(entries) {
      if (!entries[0] || !entries[0].contentRect) {
        return;
      }
      var contentRect = entries[0].contentRect;
      var hide = contentRect.width === 0 && contentRect.height === 0; // 隐藏条件：高宽都为0
      var rect = (0, _extends2.default)({
        hide: hide
      }, contentRect);
      handler && handler(rect);
    };
    var resizeObserver = new _resizeObserverPolyfill.default(measure);
    resizeObserver && resizeObserver.observe(element);
    return function () {
      resizeObserver && resizeObserver.disconnect();
    };
  }, [element, handler]);
}