import { useEnvironment as k, useModuleDictionary as w, useModuleSkeleton as E } from "@m4l/core";
import { useState as f, useEffect as _ } from "react";
import { ASSETS_URL as A, ASSETS as M } from "../../constants.js";
import { getDynamicSortDictionary as b, DICCTIONARY as x } from "../../dictionary.js";
import { useDynamicSortStore as i } from "../../store/useDynamicSortStore.js";
function T() {
  const [t, l] = f(-1), [o, c] = f([]), { host_static_assets: S, environment_assets: m } = k(), { getLabel: a } = w(), F = E(), s = i((e) => e.isOpenPopoverMenuFields), u = i((e) => e.availableFields), r = i((e) => e.inputData), v = i((e) => !!e.popoverSortData), {
    closePopoverMenuFields: p,
    hidePopoverSort: D,
    openPopoverMenuFields: d,
    setInputData: h,
    showPopoverSortForAdd: y,
    setElementRef: I
  } = i((e) => e.actions);
  _(() => {
    s ? (c(
      u.filter(
        (e) => (e.label ?? a(e.dictionaryId)).includes(r)
      )
    ), l(-1)) : c([]);
  }, [s, r, u, a]);
  function P(e) {
    p(), y(e);
  }
  const g = {
    /**
     * Evento de foco del inputSort
     */
    onFocus: (e) => {
      d();
    },
    /**
     * Evento de cambio del inputSort
     */
    onChange: (e) => {
      h(e.target.value), s || d(), D(!0);
    },
    /**
     * Evento de teclado del inputSort
     */
    onKeyDown: (e) => {
      if (e.key === "ArrowDown") {
        let n = -1;
        o.length > 0 && (t < o.length - 1 ? n = t + 1 : n = o.length - 1), l(n);
      }
      if (e.key === "ArrowUp") {
        let n = -1;
        o.length > 0 && t > -1 && (n = t - 1), l(n);
      }
      if (e.key === "Escape" && p(), e.key === "Enter")
        if (t === -1) {
          if (r.trim() === "") {
            p();
            return;
          }
        } else
          P(o[t]);
    }
  };
  return {
    /**
     * URL del icono del inputSort  `${host_static_assets}/${environment_assets}/${ASSETS_URL}/${ASSETS.close}`;
     */
    iconUrl: `${S}/${m}/${A}/${M.list_bulleted}`,
    /**
     * Si es esqueleto mostrar la versión de esqueleton
     */
    isSkeleton: F,
    /**
     * Texto de placeholder del inputSort
     */
    inputPlaceHolder: a(b(x.input_placeholder)),
    /**
     * Texto del inputSort
     */
    inputData: r,
    /**
     * Props para el handler de eventos del inputSort
     */
    inputHandlersProps: g,
    /**
     * Campos filtrados por el inputSort
     */
    sortFields: o,
    /**
     * Indice del campo seleccionado
     */
    selectedFieldIndex: t,
    /**
     * Actualiza la referencia del elemento (inputSort)
     */
    setElementRef: I,
    /**
     * Indica si el menú de campos está abierto
     */
    isOpenPopoverMenuFields: s,
    /**
     * Indica si el popoverSort está abierto
     */
    isOpenPopoverSort: v
  };
}
export {
  T as default
};
