"use client";
'use strict';

var UQQRIHDV = require('./UQQRIHDV.cjs');
var _6VRAQV3D = require('./6VRAQV3D.cjs');
var S43UC3DV = require('./S43UC3DV.cjs');
var YV4JVR4I = require('./YV4JVR4I.cjs');
var LMDWO4NN = require('./LMDWO4NN.cjs');
var ABQUS43J = require('./ABQUS43J.cjs');
var _3YLGPPWQ = require('./3YLGPPWQ.cjs');
var DTR5TSDJ = require('../../../../../../@ariakit_core@0.4.15/node_modules/@ariakit/core/esm/__chunks/DTR5TSDJ.cjs');
var events = require('../../../../../../@ariakit_core@0.4.15/node_modules/@ariakit/core/esm/utils/events.cjs');
var focus = require('../../../../../../@ariakit_core@0.4.15/node_modules/@ariakit/core/esm/utils/focus.cjs');
var PBFD2E7P = require('../../../../../../@ariakit_core@0.4.15/node_modules/@ariakit/core/esm/__chunks/PBFD2E7P.cjs');
var React = require('react');
var jsxRuntime = require('react/jsx-runtime');

var TagName = "div";
function isSelected(storeValue, itemValue) {
  if (itemValue == null) return;
  if (storeValue == null) return false;
  if (Array.isArray(storeValue)) {
    return storeValue.includes(itemValue);
  }
  return storeValue === itemValue;
}
function getItemRole(popupRole) {
  var _a;
  const itemRoleByPopupRole = {
    menu: "menuitem",
    listbox: "option",
    tree: "treeitem"
  };
  const key = popupRole;
  return (_a = itemRoleByPopupRole[key]) != null ? _a : "option";
}
var useComboboxItem = LMDWO4NN.createHook(
  function useComboboxItem2(_a) {
    var _b = _a, {
      store,
      value,
      hideOnClick,
      setValueOnClick,
      selectValueOnClick = true,
      resetValueOnSelect,
      focusOnHover = false,
      moveOnKeyPress = true,
      getItem: getItemProp
    } = _b, props = _3YLGPPWQ.__objRest(_b, [
      "store",
      "value",
      "hideOnClick",
      "setValueOnClick",
      "selectValueOnClick",
      "resetValueOnSelect",
      "focusOnHover",
      "moveOnKeyPress",
      "getItem"
    ]);
    var _a2;
    const context = S43UC3DV.useComboboxScopedContext();
    store = store || context;
    PBFD2E7P.invariant(
      store,
      process.env.NODE_ENV !== "production" && "ComboboxItem must be wrapped in a ComboboxList or ComboboxPopover component."
    );
    const { resetValueOnSelectState, multiSelectable, selected } = YV4JVR4I.useStoreStateObject(store, {
      resetValueOnSelectState: "resetValueOnSelect",
      multiSelectable(state) {
        return Array.isArray(state.selectedValue);
      },
      selected(state) {
        return isSelected(state.selectedValue, value);
      }
    });
    const getItem = React.useCallback(
      (item) => {
        const nextItem = _3YLGPPWQ.__spreadProps(_3YLGPPWQ.__spreadValues({}, item), { value });
        if (getItemProp) {
          return getItemProp(nextItem);
        }
        return nextItem;
      },
      [value, getItemProp]
    );
    setValueOnClick = setValueOnClick != null ? setValueOnClick : !multiSelectable;
    hideOnClick = hideOnClick != null ? hideOnClick : value != null && !multiSelectable;
    const onClickProp = props.onClick;
    const setValueOnClickProp = ABQUS43J.useBooleanEvent(setValueOnClick);
    const selectValueOnClickProp = ABQUS43J.useBooleanEvent(selectValueOnClick);
    const resetValueOnSelectProp = ABQUS43J.useBooleanEvent(
      (_a2 = resetValueOnSelect != null ? resetValueOnSelect : resetValueOnSelectState) != null ? _a2 : multiSelectable
    );
    const hideOnClickProp = ABQUS43J.useBooleanEvent(hideOnClick);
    const onClick = ABQUS43J.useEvent((event) => {
      onClickProp == null ? void 0 : onClickProp(event);
      if (event.defaultPrevented) return;
      if (events.isDownloading(event)) return;
      if (events.isOpeningInNewTab(event)) return;
      if (value != null) {
        if (selectValueOnClickProp(event)) {
          if (resetValueOnSelectProp(event)) {
            store == null ? void 0 : store.resetValue();
          }
          store == null ? void 0 : store.setSelectedValue((prevValue) => {
            if (!Array.isArray(prevValue)) return value;
            if (prevValue.includes(value)) {
              return prevValue.filter((v) => v !== value);
            }
            return [...prevValue, value];
          });
        }
        if (setValueOnClickProp(event)) {
          store == null ? void 0 : store.setValue(value);
        }
      }
      if (hideOnClickProp(event)) {
        store == null ? void 0 : store.hide();
      }
    });
    const onKeyDownProp = props.onKeyDown;
    const onKeyDown = ABQUS43J.useEvent((event) => {
      onKeyDownProp == null ? void 0 : onKeyDownProp(event);
      if (event.defaultPrevented) return;
      const baseElement = store == null ? void 0 : store.getState().baseElement;
      if (!baseElement) return;
      if (focus.hasFocus(baseElement)) return;
      const printable = event.key.length === 1;
      if (printable || event.key === "Backspace" || event.key === "Delete") {
        queueMicrotask(() => baseElement.focus());
        if (DTR5TSDJ.isTextField(baseElement)) {
          store == null ? void 0 : store.setValue(baseElement.value);
        }
      }
    });
    if (multiSelectable && selected != null) {
      props = _3YLGPPWQ.__spreadValues({
        "aria-selected": selected
      }, props);
    }
    props = ABQUS43J.useWrapElement(
      props,
      (element) => /* @__PURE__ */ jsxRuntime.jsx(S43UC3DV.ComboboxItemValueContext.Provider, { value, children: /* @__PURE__ */ jsxRuntime.jsx(S43UC3DV.ComboboxItemCheckedContext.Provider, { value: selected != null ? selected : false, children: element }) }),
      [value, selected]
    );
    const popupRole = React.useContext(S43UC3DV.ComboboxListRoleContext);
    props = _3YLGPPWQ.__spreadProps(_3YLGPPWQ.__spreadValues({
      role: getItemRole(popupRole),
      children: value
    }, props), {
      onClick,
      onKeyDown
    });
    const moveOnKeyPressProp = ABQUS43J.useBooleanEvent(moveOnKeyPress);
    props = _6VRAQV3D.useCompositeItem(_3YLGPPWQ.__spreadProps(_3YLGPPWQ.__spreadValues({
      store
    }, props), {
      getItem,
      // Dispatch a custom event on the combobox input when moving to an item
      // with the keyboard so the Combobox component can enable inline
      // autocompletion.
      moveOnKeyPress: (event) => {
        if (!moveOnKeyPressProp(event)) return false;
        const moveEvent = new Event("combobox-item-move");
        const baseElement = store == null ? void 0 : store.getState().baseElement;
        baseElement == null ? void 0 : baseElement.dispatchEvent(moveEvent);
        return true;
      }
    }));
    props = UQQRIHDV.useCompositeHover(_3YLGPPWQ.__spreadValues({ store, focusOnHover }, props));
    return props;
  }
);
var ComboboxItem = LMDWO4NN.memo(
  LMDWO4NN.forwardRef(function ComboboxItem2(props) {
    const htmlProps = useComboboxItem(props);
    return LMDWO4NN.createElement(TagName, htmlProps);
  })
);

exports.ComboboxItem = ComboboxItem;
exports.useComboboxItem = useComboboxItem;
//# sourceMappingURL=JMHAJKUZ.cjs.map
