import { jsx as o } from "@emotion/react/jsx-runtime";
import { forwardRef as z, useCallback as D } from "react";
import { useAutocomplete as q } from "./hooks/useAutocomplete/useAutocomplete.js";
import { AUTOCOMPLETE_CLASSES as K } from "./constants.js";
import N from "clsx";
import { SkeletonAutocompleteStyled as j, AutocompleteRootStyled as B } from "./slots/AutocompleteSlots.js";
import { PopperComponent as F } from "./subcomponents/PopperComponent/PopperComponent.js";
import { NoOptionsText as M } from "./subcomponents/NoOptionsText/NoOptionsText.js";
import { LoadingText as U } from "./subcomponents/LoadingText/LoadingText.js";
function _(r, u) {
  const {
    options: m,
    disabled: f,
    loading: d,
    onChangeFilterParmsLocal: l,
    getOptionDescription: i,
    multiple: p,
    className: O
  } = r, {
    // propiedades de useAutocomplete
    ownerState: a,
    isSkeleton: C,
    memoizedRenderOption: L,
    memoizedRenderInput: g,
    // propiedades de useValuesAndHandlers
    open: T,
    selectedValue: S,
    inputValue: h,
    isOptionEqualToValueLocal: V,
    handleChange: w,
    handleInputChange: x,
    onCloseLocal: A,
    onOpenLocal: I,
    getOptionLabelLocal: t,
    checkKeyDown: P,
    scrollPositionOptionsRef: c
  } = q(r), y = D((e, E) => {
    if (l)
      return e;
    const n = E.inputValue.trim().toLowerCase();
    return n ? e.filter((s) => {
      const R = s, k = t(s).toLowerCase(), b = i?.(R)?.toLowerCase() ?? "";
      return k.includes(n) || b.includes(n);
    }) : e;
  }, [i, t, l]);
  return C ? /* @__PURE__ */ o(
    j,
    {
      ownerState: a
    }
  ) : /* @__PURE__ */ o(
    B,
    {
      ownerState: a,
      multiple: p,
      onKeyDown: P,
      ref: u,
      forcePopupIcon: !1,
      className: N(O, p && K.multiple),
      autoSelect: !1,
      options: m,
      getOptionLabel: t,
      clearOnBlur: !1,
      PopperComponent: F,
      isOptionEqualToValue: V,
      value: S,
      inputValue: h,
      onOpen: I,
      onClose: A,
      open: T,
      onInputChange: x,
      onChange: w,
      loading: d,
      loadingText: /* @__PURE__ */ o(U, {}),
      disabled: f,
      noOptionsText: /* @__PURE__ */ o(M, {}),
      filterOptions: y,
      renderOption: L,
      ListboxProps: {
        //Esto para no perder el scroll de la lista de opciones, cuando es multiple, ya que MUI no lo guarda
        onScroll: (e) => {
          c.current = e.currentTarget.scrollTop;
        },
        /**
         * Ref para guardar el scroll de la lista de opciones
         */
        ref: (e) => {
          e && (e.scrollTop = c.current);
        }
      },
      renderInput: g
    }
  );
}
const v = z(_);
export {
  v as Autocomplete
};
