import _extends from 'babel-runtime/helpers/extends';
import * as React from 'react';
import classNames from 'classnames';
import Icon from '../../icon';
import Col from '../../col';
import { FormContext, FormItemPrefixContext } from './context';
import ErrorList from './ErrorList';
var iconMap = {
  success: React.createElement(Icon, { type: 'check-circle', theme: 'filled' }),
  warning: React.createElement(Icon, { type: 'exclamation-circle', theme: 'filled' }),
  error: React.createElement(Icon, { type: 'close-circle', theme: 'filled' }),
  validating: React.createElement(Icon, { 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(FormContext);
  var mergedWrapperCol = wrapperCol || formContext.wrapperCol || {};
  var className = classNames(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 = _extends({}, formContext);
  delete subFormContext.labelCol;
  delete subFormContext.wrapperCol;
  return React.createElement(
    FormContext.Provider,
    { value: subFormContext },
    React.createElement(
      Col,
      _extends({}, mergedWrapperCol, { className: className }),
      React.createElement(
        'div',
        { className: baseClassName + '-control-input' },
        React.createElement(
          'div',
          { className: baseClassName + '-control-input-content' },
          children
        ),
        icon
      ),
      React.createElement(
        FormItemPrefixContext.Provider,
        { value: { prefixCls: prefixCls, status: status } },
        React.createElement(ErrorList, { errors: errors, help: help, onDomErrorVisibleChange: onDomErrorVisibleChange })
      ),
      extra && React.createElement(
        'div',
        { className: baseClassName + '-extra' },
        extra
      )
    )
  );
};
export default FormItemInput;