"use strict";

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

var _react = _interopRequireWildcard(require("react"));

var _one = require("remax/one");

var _classnames = _interopRequireDefault(require("classnames"));

var _cell = _interopRequireDefault(require("../cell"));

var _context = require("./context");

var _common = require("./../common");

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }

function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

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 _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }

var prefixCls = (0, _common.getPrefixCls)('form-item'); // const count = 0;

var FormItem = function FormItem(props) {
  var label = props.label,
      _props$border = props.border,
      border = _props$border === void 0 ? false : _props$border,
      icon = props.icon,
      required = props.required,
      _props$valueAlign = props.valueAlign,
      valueAlign = _props$valueAlign === void 0 ? 'right' : _props$valueAlign,
      name = props.name,
      rules = props.rules,
      style = props.style,
      noStyle = props.noStyle,
      _props$valuePropName = props.valuePropName,
      valuePropName = _props$valuePropName === void 0 ? 'value' : _props$valuePropName,
      _props$trigger = props.trigger,
      trigger = _props$trigger === void 0 ? 'onChange' : _props$trigger,
      children = props.children;

  var _useContext = (0, _react.useContext)(_context.FormContext),
      onChange = _useContext.onChange,
      onChangeError = _useContext.onChangeError,
      formInstance = _useContext.formInstance;

  var _ref = formInstance || {},
      getFieldsValue = _ref.getFieldsValue,
      getFieldsError = _ref.getFieldsError;

  var _ref2 = (formInstance === null || formInstance === void 0 ? void 0 : formInstance.getInternalHooks(_context.HOOK_KEY)) || {},
      registerField = _ref2.registerField;

  var store = getFieldsValue === null || getFieldsValue === void 0 ? void 0 : getFieldsValue();
  var errors = getFieldsError === null || getFieldsError === void 0 ? void 0 : getFieldsError();
  (0, _react.useEffect)(function () {
    if (name) {
      registerField === null || registerField === void 0 ? void 0 : registerField(props);
    }
  }, []);

  var handleChange = function handleChange(e) {
    var error = name ? errors[name] || {} : {};

    if (Array.isArray(rules) && Object.keys(error).length > 0) {
      onChangeError({}, name);
    }

    onChange === null || onChange === void 0 ? void 0 : onChange(e, name);
  };

  var handleBlur = function handleBlur(e) {
    var value = e.target.value;

    if (Array.isArray(rules)) {
      var errorFlag = false;
      var errorObj = {};

      for (var i = 0; i < rules.length; i += 1) {
        var rule = rules[i] || {};
        var _required = rule.required,
            pattern = rule.pattern,
            validator = rule.validator,
            _rule$message = rule.message,
            message = _rule$message === void 0 ? "".concat(name, " is required") : _rule$message;

        if (_required) {
          if (!value && value !== 0) {
            errorObj = {
              error: true,
              message: message
            };
            errorFlag = true;
            break;
          }
        }

        if (pattern) {
          var isPass = pattern.test(value);

          if (!isPass) {
            errorObj = {
              error: true,
              message: message
            };
            errorFlag = true;
            break;
          }
        }

        if (validator && typeof validator === 'function') {
          var result = validator(value);

          if (!result) {
            errorObj = {
              error: true,
              message: message
            };
            errorFlag = true;
            break;
          }
        }
      }

      if (errorFlag) {
        onChangeError(errorObj, name);
      }

      if (!errorFlag) {
        onChangeError({}, name);
      }
    }
  };

  var childrenNode = children;

  if (_typeof(children) === 'object' && children !== null) {
    var childrenProps = children.props;

    if (childrenProps && name) {
      var _Object$assign;

      var onTrigger = function onTrigger(e) {
        var _a;

        handleChange(e);
        (_a = childrenProps === null || childrenProps === void 0 ? void 0 : childrenProps[trigger]) === null || _a === void 0 ? void 0 : _a.call(childrenProps, e);
      }; // 这里给input的 value 赋值为 undefined 是不会改变显示的值，必须要设置为空字符串 ''


      childrenNode = Object.assign(Object.assign({}, children), {
        props: Object.assign(Object.assign({}, childrenProps), (_Object$assign = {}, _defineProperty(_Object$assign, valuePropName, store[name]), _defineProperty(_Object$assign, trigger, onTrigger), _defineProperty(_Object$assign, "onBlur", handleBlur), _Object$assign))
      });
    }
  }

  var renderErrorNode = function renderErrorNode() {
    var _a;

    var height = '0';
    var errorMessage = null;

    if (name && errors[name] && errors[name].error) {
      height = '28px';
      errorMessage = (_a = errors === null || errors === void 0 ? void 0 : errors[name]) === null || _a === void 0 ? void 0 : _a.message;
    }

    return /*#__PURE__*/_react.default.createElement(_one.View, {
      className: "".concat(prefixCls, "-explain"),
      style: {
        height: height
      }
    }, errorMessage);
  }; // count += 1;


  if (noStyle) {
    return (
      /*#__PURE__*/
      // <React.Fragment key={count}>
      _react.default.createElement(_one.View, {
        className: (0, _classnames.default)(prefixCls),
        style: style
      }, childrenNode, renderErrorNode()) // </React.Fragment>

    );
  }

  return /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, /*#__PURE__*/_react.default.createElement(_cell.default, {
    style: style,
    label: label,
    border: border,
    icon: icon,
    required: required,
    field: true,
    labelStyle: {
      width: '180px'
    },
    valueAlign: valueAlign
  }, childrenNode), renderErrorNode());
};

var _default = FormItem;
exports.default = _default;