function _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
import React from 'react';
import { get, set, has } from 'lodash';
import dayjs from "dayjs";
function convertProps(props, list, mapper) {
  var out = {};
  list.forEach(function (key) {
    if (has(props, key)) {
      set(out, key, mapper(get(props, key), key));
    }
  });
  return out;
}

/**
 * 简单包装，不做任何处理
 * 部分组件ref比较特殊，包一层会解决这个问题
 */
export function withWrap(Comp) {
  // return forwardRef((props: React.ComponentProps<T>, ref: Ref<any>) => {
  //   // @ts-ignore
  //   return <Comp {...props} ref={ref} />;
  // });
  return function (props) {
    return /*#__PURE__*/React.createElement(Comp, props);
  };
}

/**
 * 某些组件会用React.Children.only检查子节点
 * 需要做处理避免报错
 */
export function withSingleChild(Comp) {
  var needsConvert = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : ['children'];
  return function (props) {
    var convertedProps = convertProps(props, needsConvert, function (prop) {
      var node = React.Children.toArray(prop)[0];
      if (node === null || _typeof(node) !== 'object') {
        node = /*#__PURE__*/React.createElement("div", null, node);
      }
      return node;
    });
    return /*#__PURE__*/React.createElement(Comp, _extends({}, props, convertedProps));
  };
}
export 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.createElement(Comp, props, node);
    }
    return /*#__PURE__*/React.createElement("div", null, children);
  };
}

/**
 * moment对象在序列化后会被转为字符串
 * 需要让日期类组件支持接受字符串值
 */
export function withMomentProps(Comp) {
  var needsConvert = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : ['value', 'defaultValue'];
  return function (props) {
    var convertedProps = convertProps(props, needsConvert, function (prop) {
      if (prop) {
        if (Array.isArray(prop)) {
          return prop.map(function (v) {
            return dayjs.isDayjs(v) ? v : dayjs(v);
          });
        }
        return dayjs.isDayjs(prop) ? prop : dayjs(prop);
      }
    });
    return /*#__PURE__*/React.createElement(Comp, _extends({}, props, convertedProps));
  };
}