import { useRef } from "react";
import isEqualWith_1 from "../../../../../../lodash@4.17.21/node_modules/lodash/isEqualWith.js";
import get_1 from "../../../../../../lodash@4.17.21/node_modules/lodash/get.js";
import FormItem from "./form-item.js";
import { isFunction, isUndefined } from "../_util/is.js";
import { isSyntheticEvent } from "./utils.js";
import warning from "../_util/warning.js";
import { FormListContext } from "./context.js";
import { jsx } from "../../../../../../@emotion_react@11.10.4_3toe27fv7etiytxb5kxc7fxaw4/node_modules/@emotion/react/jsx-runtime/dist/emotion-react-jsx-runtime.browser.esm.js";
var __assign = globalThis && globalThis.__assign || function() {
  __assign = Object.assign || function(t) {
    for (var s, i = 1, n = arguments.length; i < n; i++) {
      s = arguments[i];
      for (var p in s)
        if (Object.prototype.hasOwnProperty.call(s, p))
          t[p] = s[p];
    }
    return t;
  };
  return __assign.apply(this, arguments);
};
var __read = globalThis && globalThis.__read || function(o, n) {
  var m = typeof Symbol === "function" && o[Symbol.iterator];
  if (!m)
    return o;
  var i = m.call(o), r, ar = [], e;
  try {
    while ((n === void 0 || n-- > 0) && !(r = i.next()).done)
      ar.push(r.value);
  } catch (error) {
    e = {
      error
    };
  } finally {
    try {
      if (r && !r.done && (m = i["return"]))
        m.call(i);
    } finally {
      if (e)
        throw e.error;
    }
  }
  return ar;
};
var __spreadArray = globalThis && globalThis.__spreadArray || function(to, from, pack) {
  if (pack || arguments.length === 2)
    for (var i = 0, l = from.length, ar; i < l; i++) {
      if (ar || !(i in from)) {
        if (!ar)
          ar = Array.prototype.slice.call(from, 0, i);
        ar[i] = from[i];
      }
    }
  return to.concat(ar || Array.prototype.slice.call(from));
};
var isIndexLegal = function(index, value) {
  return !isUndefined(index) && index >= 0 && index < value.length;
};
var List = function(props) {
  var field = props.field, children = props.children, initialValue = props.initialValue;
  var keysRef = useRef({
    id: 0,
    keys: []
  });
  var extra = "initialValue" in props ? {
    initialValue
  } : {};
  var currentKeys = keysRef.current.keys;
  return /* @__PURE__ */ jsx(FormListContext.Provider, {
    value: {
      getItemKey: function(fieldKey) {
        var keys = fieldKey === null || fieldKey === void 0 ? void 0 : fieldKey.replace(/\[|\]/g, ".").split(".");
        var startIndex = keys.indexOf(field);
        var index = keys[startIndex + 1];
        return field + "_" + currentKeys.indexOf(index) + "_" + keys.slice(startIndex + 2).join("_");
      }
    },
    children: /* @__PURE__ */ jsx(FormItem, {
      ...__assign({
        field
      }, extra, {
        isFormList: true,
        noStyle: true,
        shouldUpdate: function(prev, current, info) {
          if (info && info.isInner && !info.isFormList && info.field !== field) {
            return false;
          }
          return !isEqualWith_1(get_1(prev, field), get_1(current, field));
        }
      }),
      children: function(_, methods) {
        var getFieldValue = methods.getFieldValue, getInnerMethods = methods.getInnerMethods;
        var innerSetFieldValue = getInnerMethods(true).innerSetFieldValue;
        var values = getFieldValue(field) || [];
        var add = function(defaultValue, index) {
          if (isSyntheticEvent(defaultValue)) {
            warning(true, "Form.List: The event object cannot be used as a parameter of the add method");
            return;
          }
          var key = keysRef.current.id;
          keysRef.current.id += 1;
          var oldValue = getFieldValue(field) || [];
          var newValue = oldValue;
          if (index !== void 0 && index >= 0 && index <= oldValue.length) {
            currentKeys.splice(index, 0, key);
            newValue = __spreadArray(__spreadArray(__spreadArray([], __read(oldValue.slice(0, index)), false), [defaultValue], false), __read(oldValue.slice(index)), false);
          } else {
            currentKeys.push(key);
            newValue = __spreadArray(__spreadArray([], __read(oldValue), false), [defaultValue], false);
          }
          innerSetFieldValue(field, newValue, {
            isFormList: true,
            ignore: defaultValue === void 0
          });
        };
        var remove = function(index) {
          var value = getFieldValue(field) || [];
          var newValue = value.filter(function(_2, i) {
            return i !== index;
          });
          currentKeys.splice(index, 1);
          innerSetFieldValue(field, __spreadArray([], __read(newValue), false), {
            isFormList: true
          });
        };
        var move = function(fromIndex, toIndex) {
          var value = getFieldValue(field) || [];
          if (fromIndex === toIndex || !isIndexLegal(fromIndex, value) || !isIndexLegal(toIndex, value)) {
            return;
          }
          var fromId = currentKeys[fromIndex];
          currentKeys.splice(fromIndex, 1);
          currentKeys.splice(toIndex, 0, fromId);
          var fromItem = value[fromIndex];
          var newValue = __spreadArray([], __read(value), false);
          newValue.splice(fromIndex, 1);
          newValue.splice(toIndex, 0, fromItem);
          innerSetFieldValue(field, newValue, {
            isFormList: true
          });
        };
        return isFunction(children) && children(values.map(function(_2, index) {
          var key = currentKeys[index];
          if (key === void 0) {
            key = keysRef.current.id;
            currentKeys.push(key);
            keysRef.current.id += 1;
          }
          return {
            field: field + "[" + index + "]",
            key
          };
        }), {
          add,
          remove,
          move
        });
      }
    })
  });
};
List.displayName = "FormList";
const FormList = List;
export {
  FormList as default
};
//# sourceMappingURL=form-list.js.map
