"use client";
import * as React from "react";
import { useRenderElement } from "../../utils/useRenderElement.js";
import { useComboboxRootContext } from "../root/ComboboxRootContext.js";
import { useComboboxChipContext } from "../chip/ComboboxChipContext.js";
import { selectors } from "../store.js";
import { createChangeEventDetails } from "../../utils/createBaseUIEventDetails.js";
import { findItemIndex } from "../../utils/itemEquality.js";
import { useButton } from "../../use-button/useButton.js";
import { useStore } from "../../../../utils/esm/store/useStore.js";
import { chipRemovePress } from "../../utils/reason-parts.js";
import { stopEvent } from "../../floating-ui-react/utils/event.js";
const ComboboxChipRemove = /* @__PURE__ */ React.forwardRef(function ComboboxChipRemove2(componentProps, forwardedRef) {
  const {
    render,
    className,
    nativeButton = true,
    ...elementProps
  } = componentProps;
  const store = useComboboxRootContext();
  const {
    index
  } = useComboboxChipContext();
  const disabled = useStore(store, selectors.disabled);
  const readOnly = useStore(store, selectors.readOnly);
  const selectedValue = useStore(store, selectors.selectedValue);
  const isItemEqualToValue = useStore(store, selectors.isItemEqualToValue);
  const {
    buttonRef,
    getButtonProps
  } = useButton({
    native: nativeButton,
    disabled: disabled || readOnly,
    focusableWhenDisabled: true
  });
  const state = React.useMemo(() => ({
    disabled
  }), [disabled]);
  const element = useRenderElement("button", componentProps, {
    ref: [forwardedRef, buttonRef],
    state,
    props: [{
      tabIndex: -1,
      disabled,
      "aria-readonly": readOnly || void 0,
      onClick(event) {
        if (disabled || readOnly) {
          return;
        }
        const eventDetails = createChangeEventDetails(chipRemovePress, event.nativeEvent);
        const activeIndex = store.state.activeIndex;
        const removedItem = selectedValue[index];
        const removedIndex = findItemIndex(store.state.valuesRef.current, removedItem, isItemEqualToValue);
        if (removedIndex !== -1 && activeIndex === removedIndex) {
          store.state.setIndices({
            activeIndex: null,
            type: store.state.keyboardActiveRef.current ? "pointer" : "keyboard"
          });
        }
        store.state.setSelectedValue(selectedValue.filter((_, i) => i !== index), eventDetails);
        if (!eventDetails.isPropagationAllowed) {
          event.stopPropagation();
        }
        store.state.inputRef.current?.focus();
      },
      onKeyDown(event) {
        if (disabled || readOnly) {
          return;
        }
        const eventDetails = createChangeEventDetails(chipRemovePress, event.nativeEvent);
        if (event.key === "Enter" || event.key === " ") {
          const activeIndex = store.state.activeIndex;
          const removedItem = selectedValue[index];
          const removedIndex = findItemIndex(store.state.valuesRef.current, removedItem, isItemEqualToValue);
          if (removedIndex !== -1 && activeIndex === removedIndex) {
            store.state.setIndices({
              activeIndex: null,
              type: store.state.keyboardActiveRef.current ? "pointer" : "keyboard"
            });
          }
          store.state.setSelectedValue(selectedValue.filter((_, i) => i !== index), eventDetails);
          if (!eventDetails.isPropagationAllowed) {
            stopEvent(event);
          }
          store.state.inputRef.current?.focus();
        }
      }
    }, elementProps, getButtonProps]
  });
  return element;
});
if (process.env.NODE_ENV !== "production") ComboboxChipRemove.displayName = "ComboboxChipRemove";
export {
  ComboboxChipRemove
};
//# sourceMappingURL=ComboboxChipRemove.js.map
