import _extends from "@babel/runtime/helpers/esm/extends";
import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";

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; }

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 = /*#__PURE__*/React.createElement(Error, {
  className: "".concat(defaultPrefixCls, "-status-icon ne-icon-error")
});
var defaultLoadingIcon = /*#__PURE__*/React.createElement(Rolling, {
  className: "".concat(defaultPrefixCls, "-status-icon ne-icon-loading")
});
var defaultSuccessIcon = /*#__PURE__*/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 = /*#__PURE__*/cloneElement(children, Object.assign(_objectSpread(_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 /*#__PURE__*/React.createElement(Row, {
    className: classes,
    style: style
  }, /*#__PURE__*/React.createElement(Col, _extends({}, finalLabelLayout, {
    className: "".concat(prefixCls, "-col ").concat(prefixCls, "-label-container")
  }), /*#__PURE__*/React.createElement(Label, {
    htmlFor: id
  }, label)), /*#__PURE__*/React.createElement(Col, _extends({}, finalContentLayout, {
    className: "".concat(prefixCls, "-content-container")
  }), input, /*#__PURE__*/React.createElement("div", {
    className: "".concat(prefixCls, "-status")
  }, nativeStatusIcon ? null : statusIcon[status]), /*#__PURE__*/React.createElement("span", {
    className: "".concat(prefixCls, "-helper-text")
  }, status === 'error' && error ? error[0] : helperText)));
  /* eslint-enable */
}

export default fieldRender;