import _useEffectState3 from "parsec-hooks/lib/effectStateHooks";
var _excluded = ["label", "name", "strLabel", "className", "after", "rules", "vertical", "children", "requiredMark", "readOnly", "requiredMarkCls", "labelStyle", "noStyle", "labelCls", "afterCls", "cell", "colon", "childrenCls", "labelWidth", "style", "labelJustify", "childrenAlign", "renderReadOnlyValue"];

function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }

function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }

function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }

function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }

function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }

function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }

function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }

function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }

function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }

function _iterableToArrayLimit(arr, i) { var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]; if (_i == null) return; var _arr = []; var _n = true; var _d = false; var _s, _e; try { for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }

function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }

import React, { useMemo, useRef } from 'react';
import { View } from 'remax/one';
import Form, { Field } from 'rc-field-form';
import styles from './index.module.less';
import { FormStore } from '../form';
import NeedWrap from '../need-wrap';
import Input from '../re-input';
import classNames from 'classnames';
import Icon from '../icon';
import formRules from '../form-rules';
import platform from '../get-platform';

var LazyUpdate = function LazyUpdate(props) {
  var _useEffectState = _useEffectState3(props, {
    wait: 0
  }),
      _useEffectState2 = _slicedToArray(_useEffectState, 1),
      myProps = _useEffectState2[0];

  return /*#__PURE__*/React.createElement(View, _objectSpread(_objectSpread({}, myProps), {}, {
    children: platform === 'web' ? props.children : myProps.children
  }));
};

export default (function (_ref) {
  var _rules;

  var label = _ref.label,
      name = _ref.name,
      _ref$strLabel = _ref.strLabel,
      strLabel = _ref$strLabel === void 0 ? typeof label === 'string' ? label : name + '' || '' : _ref$strLabel,
      className = _ref.className,
      after = _ref.after,
      _ref$rules = _ref.rules,
      rules = _ref$rules === void 0 ? [] : _ref$rules,
      outVertical = _ref.vertical,
      children = _ref.children,
      outRequiredMark = _ref.requiredMark,
      outReadOnly = _ref.readOnly,
      outRequiredMarkCls = _ref.requiredMarkCls,
      outLabelStyle = _ref.labelStyle,
      outNoStyle = _ref.noStyle,
      outLabelCls = _ref.labelCls,
      outAfterCls = _ref.afterCls,
      outCell = _ref.cell,
      outColon = _ref.colon,
      outChildrenCls = _ref.childrenCls,
      outLabelWidth = _ref.labelWidth,
      style = _ref.style,
      _ref$labelJustify = _ref.labelJustify,
      outLabelJustify = _ref$labelJustify === void 0 ? 'right' : _ref$labelJustify,
      _ref$childrenAlign = _ref.childrenAlign,
      outChildrenAlign = _ref$childrenAlign === void 0 ? 'right' : _ref$childrenAlign,
      renderReadOnlyValue = _ref.renderReadOnlyValue,
      props = _objectWithoutProperties(_ref, _excluded);

  var store = FormStore.useContainer();

  var _ref2 = store || {},
      _ref2$labelWidth = _ref2.labelWidth,
      labelWidth = _ref2$labelWidth === void 0 ? outLabelWidth : _ref2$labelWidth,
      itemCls = _ref2.itemCls,
      _ref2$childrenCls = _ref2.childrenCls,
      childrenCls = _ref2$childrenCls === void 0 ? outChildrenCls : _ref2$childrenCls,
      _ref2$colon = _ref2.colon,
      colon = _ref2$colon === void 0 ? outColon : _ref2$colon,
      _ref2$cell = _ref2.cell,
      cell = _ref2$cell === void 0 ? outCell : _ref2$cell,
      _ref2$afterCls = _ref2.afterCls,
      afterCls = _ref2$afterCls === void 0 ? outAfterCls : _ref2$afterCls,
      _ref2$requiredMark = _ref2.requiredMark,
      requiredMark = _ref2$requiredMark === void 0 ? outRequiredMark === undefined ? true : outRequiredMark : _ref2$requiredMark,
      _ref2$requiredMarkCls = _ref2.requiredMarkCls,
      requiredMarkCls = _ref2$requiredMarkCls === void 0 ? outRequiredMarkCls : _ref2$requiredMarkCls,
      _ref2$vertical = _ref2.vertical,
      vertical = _ref2$vertical === void 0 ? outVertical : _ref2$vertical,
      _ref2$readOnly = _ref2.readOnly,
      readOnly = _ref2$readOnly === void 0 ? outReadOnly : _ref2$readOnly,
      _ref2$labelCls = _ref2.labelCls,
      labelCls = _ref2$labelCls === void 0 ? outLabelCls : _ref2$labelCls,
      _ref2$labelStyle = _ref2.labelStyle,
      labelStyle = _ref2$labelStyle === void 0 ? outLabelStyle : _ref2$labelStyle,
      _ref2$noStyle = _ref2.noStyle,
      noStyle = _ref2$noStyle === void 0 ? outNoStyle : _ref2$noStyle,
      _ref2$childrenAlign = _ref2.childrenAlign,
      childrenAlign = _ref2$childrenAlign === void 0 ? outChildrenAlign : _ref2$childrenAlign,
      card = _ref2.card,
      itemStyle = _ref2.itemStyle,
      itemChildrenStyle = _ref2.itemChildrenStyle,
      values = _ref2.values,
      errorFields = _ref2.errorFields;

  var labelJustify = store.labelJustify || labelWidth ? 'justify' : outLabelJustify;
  var required = outRequiredMark || false;
  rules = (_rules = rules) === null || _rules === void 0 ? void 0 : _rules.map(function (item) {
    if (item instanceof Function) return item;
    item = _objectSpread({}, item);

    if (item.type && formRules[item.type]) {
      item.validator = formRules[item.type];
      delete item.type;
    }

    if (item.required) {
      required = true;

      if (!item.validator) {
        item.message = item.message || "".concat(strLabel, "\u662F\u5FC5\u586B\u7684");
      }
    }

    return item;
  });
  var inputFocus = useRef(false);
  children = useMemo(function () {
    return children === undefined ? name ? /*#__PURE__*/React.createElement(Input, {
      placeholder: "\u8BF7\u8F93\u5165".concat(strLabel)
    }) : /*#__PURE__*/React.createElement(View, null) : children;
  }, [children, name, strLabel]);

  var renderField = function renderField() {
    var node = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : children;
    return /*#__PURE__*/React.createElement(NeedWrap, {
      need: !!name || node instanceof Function,
      wrap: Field,
      wrapProps: _objectSpread({
        rules: rules,
        name: name
      }, props)
    }, node);
  };

  return /*#__PURE__*/React.createElement(NeedWrap, {
    wrap: Form,
    wrapProps: {
      component: false
    },
    need: !store
  }, noStyle ? renderField() : renderField(function (control, meta, form) {
    var _errorFields$, _classNames;

    var showError = !!meta.errors.length && (!errorFields.length || ((_errorFields$ = errorFields[0]) === null || _errorFields$ === void 0 ? void 0 : _errorFields$.name.includes(name))) && !inputFocus.current;
    var childNode = typeof children === 'function' ? children(control, meta, form) : /*#__PURE__*/React.isValidElement(children) ? /*#__PURE__*/React.cloneElement(children, _objectSpread(_objectSpread({}, control), {}, {
      onFocus: function onFocus() {
        var _children$props$onFoc, _children$props;

        inputFocus.current = true;

        for (var _len = arguments.length, e = new Array(_len), _key = 0; _key < _len; _key++) {
          e[_key] = arguments[_key];
        }

        /*#__PURE__*/
        React.isValidElement(children) && ((_children$props$onFoc = (_children$props = children.props).onFocus) === null || _children$props$onFoc === void 0 ? void 0 : _children$props$onFoc.call.apply(_children$props$onFoc, [_children$props].concat(e)));
      },
      onBlur: function onBlur() {
        var _children$props$onBlu, _children$props2;

        inputFocus.current = false;

        for (var _len2 = arguments.length, e = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
          e[_key2] = arguments[_key2];
        }

        /*#__PURE__*/
        React.isValidElement(children) && ((_children$props$onBlu = (_children$props2 = children.props).onBlur) === null || _children$props$onBlu === void 0 ? void 0 : _children$props$onBlu.call.apply(_children$props$onBlu, [_children$props2].concat(e)));
      },
      onChange: function onChange() {
        for (var _len3 = arguments.length, arg = new Array(_len3), _key3 = 0; _key3 < _len3; _key3++) {
          arg[_key3] = arguments[_key3];
        }

        control.onChange.apply(control, arg);

        if ( /*#__PURE__*/React.isValidElement(children)) {
          var _children$props$onCha, _children$props3;

          (_children$props$onCha = (_children$props3 = children.props).onChange) === null || _children$props$onCha === void 0 ? void 0 : _children$props$onCha.call.apply(_children$props$onCha, [_children$props3].concat(arg));
        }
      }
    })) : /*#__PURE__*/React.createElement(View, null, children);
    var errIcon = /*#__PURE__*/React.createElement(LazyUpdate, {
      className: classNames(styles.after, afterCls, outAfterCls),
      style: {
        display: after || showError ? undefined : 'none'
      }
    }, showError ? /*#__PURE__*/React.createElement(Icon, {
      size: 32,
      name: 'tip',
      color: '#ED4E56'
    }) : after);
    return /*#__PURE__*/React.createElement(LazyUpdate, _objectSpread({
      className: classNames(styles.item, itemCls, showError && styles.error, className, (_classNames = {}, _defineProperty(_classNames, styles.cell, cell), _defineProperty(_classNames, styles.card, card), _defineProperty(_classNames, styles.vertical, vertical), _classNames)),
      style: _objectSpread(_objectSpread({}, style), itemStyle)
    }, props), label && /*#__PURE__*/React.createElement(View, {
      className: styles.labelWrap
    }, requiredMark && (!vertical || required) && /*#__PURE__*/React.createElement(View, {
      className: classNames(styles.mark, requiredMarkCls, outRequiredMarkCls),
      style: {
        opacity: +(required && requiredMark)
      }
    }, "*"), /*#__PURE__*/React.createElement(View, {
      style: _objectSpread({
        minWidth: labelWidth,
        justifyContent: labelJustify === 'right' ? 'flex-end' : labelJustify === 'justify' ? 'space-between' : 'flex-start'
      }, labelStyle),
      className: classNames(styles.label, labelCls, outLabelCls)
    }, typeof label === 'string' ? _toConsumableArray(label).map(function (item, index) {
      return /*#__PURE__*/React.createElement(View, {
        key: index
      }, item);
    }) : label), (colon === undefined ? !cell : colon === true) ? /*#__PURE__*/React.createElement(View, {
      className: styles.colon
    }, ":") : colon, vertical && errIcon), /*#__PURE__*/React.createElement(View, {
      className: classNames(styles.children, childrenAlign === 'left' && styles.left, childrenCls, outChildrenCls),
      style: _objectSpread({
        justifyContent: label && childrenAlign !== 'left' ? 'flex-end' : 'flex-start'
      }, itemChildrenStyle)
    }, readOnly ? /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(Field, {
      shouldUpdate: true
    }, function (_, __, _ref3) {
      var getFieldValue = _ref3.getFieldValue,
          getFieldsValue = _ref3.getFieldsValue;
      return name && (renderReadOnlyValue ? renderReadOnlyValue(getFieldValue(name), getFieldsValue()) : getFieldValue(name) || (values === null || values === void 0 ? void 0 : values[name + ''])) || children;
    }), /*#__PURE__*/React.createElement(View, {
      style: {
        visibility: 'hidden',
        position: 'absolute',
        pointerEvents: 'none'
      }
    }, childNode)) : childNode), !vertical && errIcon);
  }));
});
export { Field } from 'rc-field-form';