function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
var _excluded = ["help", "name", "extra", "inputExtra", "label", "layout", "labelCol", "mainCol", "valuePropName", "getValueProps", "shouldUpdate", "initialValue", "validateTrigger", "rules", "children"];
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); }
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
function _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
import React, { forwardRef } from 'react';
import { Field } from 'rc-field-form';

// 内库使用-start
import Typography from "../../Typography";
import getExtraNode from "../../Typography/Form/Main/getExtraNode";
// 内库使用-end

/* 测试使用-start
import { Typography } from 'seedsui-react'
测试使用-end */

var _Typography$Form = Typography.Form,
  Item = _Typography$Form.Item,
  Label = _Typography$Form.Label,
  Main = _Typography$Form.Main;
var FormItem = /*#__PURE__*/forwardRef(function (_ref, ref) {
  var help = _ref.help,
    name = _ref.name,
    _extra = _ref.extra,
    _inputExtra = _ref.inputExtra,
    label = _ref.label,
    layout = _ref.layout,
    labelCol = _ref.labelCol,
    mainCol = _ref.mainCol,
    valuePropName = _ref.valuePropName,
    getValueProps = _ref.getValueProps,
    shouldUpdate = _ref.shouldUpdate,
    initialValue = _ref.initialValue,
    validateTrigger = _ref.validateTrigger,
    rules = _ref.rules,
    children = _ref.children,
    props = _objectWithoutProperties(_ref, _excluded);
  return /*#__PURE__*/React.createElement(Field, {
    rules: rules,
    name: name,
    getValueProps: getValueProps,
    valuePropName: valuePropName,
    shouldUpdate: shouldUpdate,
    initialValue: initialValue,
    validateTrigger: validateTrigger
  }, function (control, renderMeta, context) {
    var _renderMeta$errors;
    return /*#__PURE__*/React.createElement(Item, _extends({
      ref: ref,
      name: name,
      layout: layout
    }, props), /*#__PURE__*/React.createElement(Label, _extends({}, labelCol, {
      help: help,
      required: (rules || []).some(function (rule) {
        return rule.required;
      })
    }), label), /*#__PURE__*/React.createElement(Main, _extends({}, mainCol, {
      extra: function extra() {
        return getExtraNode(_extra, {
          params: _objectSpread(_objectSpread({}, control), {}, {
            errors: renderMeta === null || renderMeta === void 0 ? void 0 : renderMeta.errors
          }),
          className: 'form-item-main-input-extra'
        });
      },
      inputExtra: function inputExtra() {
        return getExtraNode(_inputExtra, {
          params: _objectSpread(_objectSpread({}, control), {}, {
            errors: renderMeta === null || renderMeta === void 0 ? void 0 : renderMeta.errors
          }),
          className: 'form-item-main-input-extra'
        });
      },
      error: (renderMeta === null || renderMeta === void 0 || (_renderMeta$errors = renderMeta.errors) === null || _renderMeta$errors === void 0 ? void 0 : _renderMeta$errors[0]) || ''
    }), React.Children.map(children, function (child) {
      // 检查是否是一个 React 组件（函数组件或类组件），而不是原生元素
      if ( /*#__PURE__*/React.isValidElement(child) && typeof child.type !== 'string') {
        // 克隆该组件并注入新的属性
        return /*#__PURE__*/React.cloneElement(child, _objectSpread(_objectSpread({}, control), {}, {
          onChange: function onChange() {
            // 调用原有onChange（如果存在）
            if (typeof child.props.onChange === 'function') {
              var _child$props;
              (_child$props = child.props).onChange.apply(_child$props, arguments);
            }
            // 执行父组件的逻辑
            (control === null || control === void 0 ? void 0 : control.onChange) && control.onChange.apply(control, arguments);
          }
        }));
      }
      // 如果是原生元素，直接返回
      return child;
    })));
  });
});
export default FormItem;