'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});

var _extends2 = require('babel-runtime/helpers/extends');

var _extends3 = _interopRequireDefault(_extends2);

var _react = require('react');

var React = _interopRequireWildcard(_react);

var _classnames = require('classnames');

var _classnames2 = _interopRequireDefault(_classnames);

var _icon = require('../../icon');

var _icon2 = _interopRequireDefault(_icon);

var _col = require('../../col');

var _col2 = _interopRequireDefault(_col);

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

var _ErrorList = require('./ErrorList');

var _ErrorList2 = _interopRequireDefault(_ErrorList);

function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj['default'] = obj; return newObj; } }

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

var iconMap = {
  success: React.createElement(_icon2['default'], { type: 'check-circle', theme: 'filled' }),
  warning: React.createElement(_icon2['default'], { type: 'exclamation-circle', theme: 'filled' }),
  error: React.createElement(_icon2['default'], { type: 'close-circle', theme: 'filled' }),
  validating: React.createElement(_icon2['default'], { type: 'loading' })
};
var FormItemInput = function FormItemInput(_ref) {
  var prefixCls = _ref.prefixCls,
      status = _ref.status,
      wrapperCol = _ref.wrapperCol,
      children = _ref.children,
      help = _ref.help,
      errors = _ref.errors,
      onDomErrorVisibleChange = _ref.onDomErrorVisibleChange,
      hasFeedback = _ref.hasFeedback,
      validateStatus = _ref.validateStatus,
      extra = _ref.extra;

  var baseClassName = prefixCls + '-item';
  var formContext = React.useContext(_context.FormContext);
  var mergedWrapperCol = wrapperCol || formContext.wrapperCol || {};
  var className = (0, _classnames2['default'])(baseClassName + '-control', mergedWrapperCol.className);
  React.useEffect(function () {
    return function () {
      onDomErrorVisibleChange(false);
    };
  }, []);
  // Should provides additional icon if `hasFeedback`
  var IconNode = validateStatus && iconMap[validateStatus];
  var icon = hasFeedback && IconNode ? React.createElement(
    'span',
    { className: baseClassName + '-children-icon' },
    IconNode
  ) : null;
  // Pass to sub FormItem should not with col info
  var subFormContext = (0, _extends3['default'])({}, formContext);
  delete subFormContext.labelCol;
  delete subFormContext.wrapperCol;
  return React.createElement(
    _context.FormContext.Provider,
    { value: subFormContext },
    React.createElement(
      _col2['default'],
      (0, _extends3['default'])({}, mergedWrapperCol, { className: className }),
      React.createElement(
        'div',
        { className: baseClassName + '-control-input' },
        React.createElement(
          'div',
          { className: baseClassName + '-control-input-content' },
          children
        ),
        icon
      ),
      React.createElement(
        _context.FormItemPrefixContext.Provider,
        { value: { prefixCls: prefixCls, status: status } },
        React.createElement(_ErrorList2['default'], { errors: errors, help: help, onDomErrorVisibleChange: onDomErrorVisibleChange })
      ),
      extra && React.createElement(
        'div',
        { className: baseClassName + '-extra' },
        extra
      )
    )
  );
};
exports['default'] = FormItemInput;
module.exports = exports['default'];