import "core-js/modules/es.array.iterator.js";
import "core-js/modules/web.dom-collections.iterator.js";
import React, { useImperativeHandle } from 'react';
import toArray from 'rc-util/es/Children/toArray';
import Space from '../../../space';
import createField from '../../create-field';
import { jsx as _jsx } from "react/jsx-runtime";
export 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 = toArray(children).map(item => {
    if ( /*#__PURE__*/React.isValidElement(item)) {
      itemIndex += 1;
      const index = itemIndex;
      return /*#__PURE__*/React.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__*/_jsx(Space, {
    ...space,
    children: list
  });
};
const ProFormFieldSet = /*#__PURE__*/React.forwardRef(({
  children,
  space,
  valuePropName,
  ...rest
}, ref) => {
  // eslint-disable-next-line @typescript-eslint/no-empty-function
  useImperativeHandle(ref, () => {}, []);
  return /*#__PURE__*/_jsx(FieldSet, {
    space: space,
    valuePropName: valuePropName,
    ...rest.fieldProps,
    ...rest,
    children: children
  });
});
export default createField(ProFormFieldSet);