import { jsx as O } from "@emotion/react/jsx-runtime";
import P from "lodash-es/debounce";
import { useReducer as R, useCallback as r, useMemo as S, useEffect as b } from "react";
import { actionsType as i } from "./types.js";
import { autocompleteAsyncReducer as L, getInitialStateAutocompleteAsync as Y } from "./reducer/AutocompleteAsyncReducer.js";
import { useFetchOptions as _ } from "./hooks/useFetchOptions.js";
import { ASYNC_ONLY_KEYS as j } from "./constants.js";
import { AutocompleteAsyncRootStyled as w } from "./slots/AutocompleteAsyncSlots.js";
function I(l) {
  const {
    endPoint: c,
    timeout: m = 5e3,
    parms: a,
    resultField: p = "data",
    isRemote: u = !0,
    isExternalUrl: d = !1,
    onChangeFilterParms: e,
    responseToCamelCase: f,
    multiple: y
  } = l, C = Object.fromEntries(
    Object.entries(l).filter(([o]) => !j.includes(o))
  ), [s, t] = R(
    L(e),
    Y({ parms: a, onChangeFilterParms: e })
  );
  if (y && e)
    throw new Error(
      "multiple cannot be enabled when onChangeFilterParms is defined; define the filter in parms if needed."
    );
  _({
    state: s,
    dispatch: t,
    endPoint: c,
    timeout: m,
    isExternalUrl: d,
    parms: a,
    responseToCamelCase: f,
    isRemote: u,
    resultField: p
  });
  const E = r(() => t({ type: i.REFRESH }), []), h = r(() => t({ type: i.OPEN }), []), A = r(() => t({ type: i.CLOSE }), []), n = S(
    () => P(
      (o) => {
        t({
          type: i.SET_QUERY_PARAMS,
          payload: o
        });
      },
      300,
      { trailing: !0 }
    ),
    [t]
  );
  b(
    () => () => {
      n.cancel();
    },
    [n]
  );
  const g = r(
    (o, F) => {
      e && F === "input" && n(e(o));
    },
    [e, n]
  );
  return /* @__PURE__ */ O(
    w,
    {
      ...C,
      loading: s.loading,
      options: s.options,
      onChangeFilterParmsLocal: e ? g : void 0,
      refresh: E,
      onOpen: h,
      onClose: A
    }
  );
}
export {
  I as AutocompleteAsync
};
