import _extends from "@babel/runtime/helpers/esm/extends";
import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
import _objectSpread from "@babel/runtime/helpers/esm/objectSpread";
import React, { cloneElement } from 'react';
import classNames from 'classnames';
import { Col, Row } from '../grid';
import Error from '../icons/internals/Error';
import CheckCircle from '../icons/internals/CheckCircle';
import Rolling from '../icons/internals/Rolling';
var defaultPrefixCls = 'ne-easy-form-field';
var defaultErrorIcon = React.createElement(Error, {
  className: "".concat(defaultPrefixCls, "-status-icon ne-icon-error")
});
var defaultLoadingIcon = React.createElement(Rolling, {
  className: "".concat(defaultPrefixCls, "-status-icon ne-icon-loading")
});
var defaultSuccessIcon = React.createElement(CheckCircle, {
  className: "".concat(defaultPrefixCls, "-status-icon ne-icon-success")
});
/**
 *
 * @ignore
 */

function fieldRender(_ref) {
  var _classNames;

  var children = _ref.children,
      id = _ref.id,
      error = _ref.error,
      validating = _ref.validating,
      label = _ref.label,
      size = _ref.size,
      style = _ref.style,
      _ref$prefixCls = _ref.prefixCls,
      prefixCls = _ref$prefixCls === void 0 ? defaultPrefixCls : _ref$prefixCls,
      dataBindProps = _ref.dataBindProps,
      helperText = _ref.helperText,
      className = _ref.className,
      inline = _ref.inline,
      _ref$labelLayout = _ref.labelLayout,
      labelLayout = _ref$labelLayout === void 0 ? {
    size: 10
  } : _ref$labelLayout,
      _ref$contentLayout = _ref.contentLayout,
      contentLayout = _ref$contentLayout === void 0 ? {
    size: 8
  } : _ref$contentLayout,
      nativeStatusIcon = _ref.nativeStatusIcon,
      _ref$errorIcon = _ref.errorIcon,
      errorIcon = _ref$errorIcon === void 0 ? defaultErrorIcon : _ref$errorIcon,
      _ref$validatingIcon = _ref.validatingIcon,
      validatingIcon = _ref$validatingIcon === void 0 ? defaultLoadingIcon : _ref$validatingIcon,
      _ref$successIcon = _ref.successIcon,
      successIcon = _ref$successIcon === void 0 ? defaultSuccessIcon : _ref$successIcon;

  /* eslint-disable */
  var input;

  try {
    React.Children.only(children);
    input = cloneElement(children, Object.assign(_objectSpread({
      id: id
    }, dataBindProps, {
      status: status,
      size: size
    }), nativeStatusIcon ? {
      showStatusIcon: true,
      errorIcon: errorIcon,
      loadingIcon: validatingIcon,
      successIcon: successIcon
    } : null));
  } catch (_unused) {
    console.warn('Field must have one valid child. E.g input, Select');
    input = children;
  }

  var status = validating ? 'loading' : error && error.length > 0 ? 'error' : null;
  var Label = label ? 'label' : 'span';
  var classes = classNames(prefixCls, className, (_classNames = {}, _defineProperty(_classNames, "".concat(prefixCls, "-").concat(size), size), _defineProperty(_classNames, "".concat(prefixCls, "-").concat(status), status), _defineProperty(_classNames, "".concat(prefixCls, "-inline"), inline), _classNames));
  var statusIcon = {
    error: errorIcon,
    loading: validatingIcon,
    success: successIcon
  };
  var finalLabelLayout = inline ? {
    size: 8
  } : labelLayout;
  var finalContentLayout = inline ? {
    size: 16
  } : contentLayout;
  return React.createElement(Row, {
    className: classes,
    style: style
  }, React.createElement(Col, _extends({}, finalLabelLayout, {
    className: "".concat(prefixCls, "-col ").concat(prefixCls, "-label-container")
  }), React.createElement(Label, {
    htmlFor: id
  }, label)), React.createElement(Col, _extends({}, finalContentLayout, {
    className: "".concat(prefixCls, "-content-container")
  }), input, React.createElement("div", {
    className: "".concat(prefixCls, "-status")
  }, nativeStatusIcon ? null : statusIcon[status]), React.createElement("span", {
    className: "".concat(prefixCls, "-helper-text")
  }, status === 'error' && error ? error[0] : helperText)));
  /* eslint-enable */
}

export default fieldRender;