"use strict";Object.defineProperty(exports, "__esModule", {value: true});

var _FHB7634Ocjs = require('../__chunks/FHB7634O.cjs');


var _DIWKSV34cjs = require('../__chunks/DIWKSV34.cjs');
require('../__chunks/CFNQ2EAV.cjs');
require('../__chunks/4JH5Q6AQ.cjs');
require('../__chunks/T3QB4FR3.cjs');
require('../__chunks/SS26HAQ2.cjs');




var _743QABRAcjs = require('../__chunks/743QABRA.cjs');


var _KGFTRA3Ccjs = require('../__chunks/KGFTRA3C.cjs');
require('../__chunks/XMDZRF6Y.cjs');

// src/form/form-remove.tsx
var _dom = require('@ariakit/core/utils/dom');
var _misc = require('@ariakit/core/utils/misc');
var TagName = "button";
function findNextOrPreviousField(items, name, index) {
  const fields = items == null ? void 0 : items.filter(
    (item) => item.type === "field" && item.name.startsWith(name)
  );
  const regex = new RegExp(`^${name}\\.(\\d+)`);
  const nextField = fields == null ? void 0 : fields.find((field) => {
    const fieldIndex = field.name.replace(regex, "$1");
    return Number.parseInt(fieldIndex, 10) > index;
  });
  if (nextField) return nextField;
  return fields == null ? void 0 : fields.reverse().find((field) => {
    const fieldIndex = field.name.replace(regex, "$1");
    return Number.parseInt(fieldIndex, 10) < index;
  });
}
function findPushButton(items, name) {
  return items == null ? void 0 : items.find((item) => item.type === "button" && item.name === name);
}
var useFormRemove = _743QABRAcjs.createHook.call(void 0, 
  function useFormRemove2({
    store,
    name: nameProp,
    index,
    autoFocusOnClick = true,
    ...props
  }) {
    const context = _FHB7634Ocjs.useFormContext.call(void 0, );
    store = store || context;
    _misc.invariant.call(void 0, 
      store,
      process.env.NODE_ENV !== "production" && "FormRemove must be wrapped in a Form component."
    );
    const name = `${nameProp}`;
    const onClickProp = props.onClick;
    const onClick = _KGFTRA3Ccjs.useEvent.call(void 0, (event) => {
      var _a;
      onClickProp == null ? void 0 : onClickProp(event);
      if (event.defaultPrevented) return;
      if (!store) return;
      store.removeValue(name, index);
      if (!autoFocusOnClick) return;
      const { items } = store.getState();
      const item = findNextOrPreviousField(items, name, index);
      const element = item == null ? void 0 : item.element;
      if (element) {
        element.focus();
        if (_dom.isTextField.call(void 0, element)) {
          element.select();
        }
      } else {
        const pushButton = findPushButton(items, name);
        (_a = pushButton == null ? void 0 : pushButton.element) == null ? void 0 : _a.focus();
      }
    });
    props = {
      ...props,
      onClick
    };
    props = _DIWKSV34cjs.useButton.call(void 0, props);
    return props;
  }
);
var FormRemove = _743QABRAcjs.forwardRef.call(void 0, function FormRemove2(props) {
  const htmlProps = useFormRemove(props);
  return _743QABRAcjs.createElement.call(void 0, TagName, htmlProps);
});



exports.FormRemove = FormRemove; exports.useFormRemove = useFormRemove;
