var _excluded = ["children", "name", "label", "mode"];
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 _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }
function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
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, { cloneElement, isValidElement, useMemo } from 'react';
import { isArr } from '@pisell/utils';
import useDataSource from "../hooks/useDataSource";
import useFormSetting from "./provider/hooks/useFormSetting";
import Form from "../../form";
import useJsonPrefixPath from "./provider/hooks/useJsonPrefixPath";
import { toArr } from "./utils";
var FormItem = function FormItem(props) {
  var _field$uiSchema;
  var children = props.children,
    name = props.name,
    propsLabel = props.label,
    propsMode = props.mode,
    other = _objectWithoutProperties(props, _excluded);
  var _useJsonPrefixPath = useJsonPrefixPath(),
    _useJsonPrefixPath$pa = _useJsonPrefixPath.path,
    prefixPath = _useJsonPrefixPath$pa === void 0 ? [] : _useJsonPrefixPath$pa;
  var _useDataSource = useDataSource(),
    dataSource = _useDataSource.dataSource;
  var fields = dataSource === null || dataSource === void 0 ? void 0 : dataSource.fields;
  var _useFormSetting = useFormSetting(),
    globalMode = _useFormSetting.renderMode;
  var field = fields === null || fields === void 0 ? void 0 : fields.find(function (item) {
    return item.name === name;
  });

  // 优先级：外部传入 > schema 中的 title > 字段名
  var label = propsLabel || (field === null || field === void 0 || (_field$uiSchema = field.uiSchema) === null || _field$uiSchema === void 0 ? void 0 : _field$uiSchema.title) || name;

  // 模式优先级：外部传入 mode > 全局模式
  var effectiveMode = propsMode || globalMode;
  var renderChildren = useMemo(function () {
    if (isArr(children) && children.every(isValidElement)) {
      return children.map(function (c) {
        return /*#__PURE__*/cloneElement(c, {
          renderMode: effectiveMode
        });
      });
    }
    return children;
  }, [children, effectiveMode]);
  var concatName = name !== undefined ? [].concat(_toConsumableArray(prefixPath), _toConsumableArray(toArr(name))) : undefined;
  return /*#__PURE__*/React.createElement(Form.Item, _extends({
    name: concatName,
    label: label
  }, other), renderChildren);
};
export default FormItem;