import _objectSpread from "@babel/runtime/helpers/objectSpread2";
import _defineProperty from "@babel/runtime/helpers/defineProperty";
import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties";
import React, { useEffect } from "react";
import { connect } from "react-redux";
import classnames from "classnames";
import { SimpleForm as Form } from '@whalecloud/fdx';
import LabelRender from "./LabelRender";
import { getParentFormWidthChildren, getElement } from "../selectors";

var FormItem = function FormItem(props) {
  var _element$data;

  if (!props.parentIsForm) {
    return props.children;
  }

  var id = props.id,
      label = props.label,
      className = props.className,
      macroValue = props.macroValue,
      element = props.element,
      formElement = props.formElement,
      macro = props.macro,
      style = props.style,
      formData = props.formData,
      restProps = _objectWithoutProperties(props, ["id", "label", "className", "macroValue", "element", "formElement", "macro", "style", "formData"]);

  var formInstance = formData.formInstance;

  var _element$data$setting = (_element$data = element.data) === null || _element$data === void 0 ? void 0 : _element$data.settings,
      visible = _element$data$setting.visible;

  useEffect(function () {
    if (formInstance) {
      setTimeout(function () {
        try {
          if (macroValue) {
            var defaultVal;
            var macroItem = macro.find(function (v) {
              return v.value === macroValue;
            });

            if (macroItem) {
              if (macroItem.function) {
                defaultVal = macroItem.function();
              } else {
                defaultVal = macroValue;
              }
            } else {
              defaultVal = macroValue;
            }

            formInstance.setFieldsValue(_defineProperty({}, props.name, defaultVal));
          }
        } catch (e) {
          console.error("\u5B8F\u6570\u636E\u8868\u8FBE\u5F0F: ".concat(macroValue, " \u89E3\u6790\u6709\u8BEF\uFF0C\u8BF7\u68C0\u67E5"), e);
        }
      }, 100);
    }
  }, [macroValue, formElement, formInstance]);
  var classNames = className.split(" ");
  return /*#__PURE__*/React.createElement(Form.Item, Object.assign({
    className: classnames(!label ? 'no-label' : null, classNames.join(','))
  }, restProps, {
    style: _objectSpread(_objectSpread({}, style), {}, {
      visibility: visible
    }),
    label: /*#__PURE__*/React.createElement(LabelRender, {
      label: label,
      formElement: formElement
    })
  }), props.children);
};

export default connect(function (state, props) {
  var _state$nameSpace;

  var nameSpace = props.nameSpace;
  var formElement = getParentFormWidthChildren(state, props.id, nameSpace);
  var parentIsForm = (formElement === null || formElement === void 0 ? void 0 : formElement.type) === 'form';
  return {
    parentIsForm: parentIsForm,
    formElement: formElement,
    element: getElement(state, props.id, nameSpace),
    macro: (_state$nameSpace = state[nameSpace]) === null || _state$nameSpace === void 0 ? void 0 : _state$nameSpace.macro,
    formData: parentIsForm && (formElement === null || formElement === void 0 ? void 0 : formElement.data) || {}
  };
}, null)( /*#__PURE__*/React.memo(FormItem));
//# sourceMappingURL=FormItem.js.map