"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
exports.defaultGetValueFromEvent = defaultGetValueFromEvent;
require("core-js/modules/es.array.iterator.js");
require("core-js/modules/web.dom-collections.iterator.js");
var _react = _interopRequireWildcard(require("react"));
var _toArray = _interopRequireDefault(require("rc-util/es/Children/toArray"));
var _space = _interopRequireDefault(require("../../../space"));
var _createField = _interopRequireDefault(require("../../create-field"));
var _jsxRuntime = require("react/jsx-runtime");
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
function defaultGetValueFromEvent(valuePropName, ...args) {
  const event = args[0];
  if (event && event.target && valuePropName in event.target) {
    return event.target[valuePropName];
  }
  return event;
}
const FieldSet = ({
  children,
  value = [],
  valuePropName,
  onChange,
  space
}) => {
  const fieldSetOnChange = (fileValue, index) => {
    const newValues = [...value];
    newValues[index] = defaultGetValueFromEvent(valuePropName || 'value', fileValue);
    onChange === null || onChange === void 0 ? void 0 : onChange(newValues);
  };
  let itemIndex = -1;
  const list = (0, _toArray.default)(children).map(item => {
    if ( /*#__PURE__*/_react.default.isValidElement(item)) {
      itemIndex += 1;
      const index = itemIndex;
      return /*#__PURE__*/_react.default.cloneElement(item, {
        key: index,
        ignoreFormItem: true,
        ...(item.props || {}),
        value: value[index],
        onChange: itemValue => {
          var _props$onChange, _props;
          fieldSetOnChange(itemValue, index);
          (_props$onChange = (_props = item.props).onChange) === null || _props$onChange === void 0 ? void 0 : _props$onChange.call(_props, itemValue);
        }
      });
    }
    return item;
  });
  return /*#__PURE__*/(0, _jsxRuntime.jsx)(_space.default, {
    ...space,
    children: list
  });
};
const ProFormFieldSet = /*#__PURE__*/_react.default.forwardRef(({
  children,
  space,
  valuePropName,
  ...rest
}, ref) => {
  // eslint-disable-next-line @typescript-eslint/no-empty-function
  (0, _react.useImperativeHandle)(ref, () => {}, []);
  return /*#__PURE__*/(0, _jsxRuntime.jsx)(FieldSet, {
    space: space,
    valuePropName: valuePropName,
    ...rest.fieldProps,
    ...rest,
    children: children
  });
});
var _default = (0, _createField.default)(ProFormFieldSet);
exports.default = _default;