"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
exports.withMomentProps = withMomentProps;
exports.withSingleChild = withSingleChild;
exports.withSingleFunctionChild = withSingleFunctionChild;
exports.withWrap = withWrap;
var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
var _react = _interopRequireWildcard(require("react"));
var _moment = _interopRequireDefault(require("moment"));
var _lodash = require("lodash");
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(e) { return e ? t : r; })(e); }
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { "default": e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && Object.prototype.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n["default"] = e, t && t.set(e, n), n; }
function convertProps(props, list, mapper) {
  var out = {};
  list.forEach(function (key) {
    if ((0, _lodash.has)(props, key)) {
      (0, _lodash.set)(out, key, mapper((0, _lodash.get)(props, key), key));
    }
  });
  return out;
}

/**
 * 简单包装，不做任何处理
 * 部分组件ref比较特殊，包一层会解决这个问题
 */
function withWrap(Comp) {
  return /*#__PURE__*/(0, _react.forwardRef)(function (props, ref) {
    return /*#__PURE__*/_react["default"].createElement(Comp, (0, _extends2["default"])({}, props, {
      ref: ref
    }));
  });
}

/**
 * 某些组件会用React.Children.only检查子节点
 * 需要做处理避免报错
 */
function withSingleChild(Comp, needsConvert) {
  if (needsConvert === void 0) {
    needsConvert = ['children'];
  }
  return function (props) {
    var convertedProps = convertProps(props, needsConvert, function (prop) {
      var node = _react["default"].Children.toArray(prop)[0];
      if (node === null || typeof node !== 'object') {
        node = /*#__PURE__*/_react["default"].createElement("div", null, node);
      }
      return node;
    });
    return /*#__PURE__*/_react["default"].createElement(Comp, (0, _extends2["default"])({}, props, convertedProps));
  };
}
function withSingleFunctionChild(Comp) {
  return function (props) {
    var children = props.children;
    var node;
    if (typeof children === 'function') {
      node = children;
    }
    if (Array.isArray(children) && children.length === 1 && typeof children[0] === 'function') {
      node = children[0];
    }
    if (node) {
      return /*#__PURE__*/_react["default"].createElement(Comp, props, node);
    }
    return /*#__PURE__*/_react["default"].createElement("div", null, children);
  };
}

/**
 * moment对象在序列化后会被转为字符串
 * 需要让日期类组件支持接受字符串值
 */
function withMomentProps(Comp, needsConvert) {
  if (needsConvert === void 0) {
    needsConvert = ['value', 'defaultValue'];
  }
  return function (props) {
    var convertedProps = convertProps(props, needsConvert, function (prop) {
      if (prop) {
        if (Array.isArray(prop)) {
          return prop.map(function (v) {
            return _moment["default"].isMoment(v) ? v : (0, _moment["default"])(v);
          });
        }
        return _moment["default"].isMoment(prop) ? prop : (0, _moment["default"])(prop);
      }
    });
    return /*#__PURE__*/_react["default"].createElement(Comp, (0, _extends2["default"])({}, props, convertedProps));
  };
}