import { useModuleDictionary as M, useModuleSkeleton as S } from "@m4l/core";
import { useState as m, useEffect as C } from "react";
import { ALL_FIELD as E } from "../../constants.js";
import { DICCTIONARY as O } from "../../dictionary.js";
import { useDynamicFilterStore as o } from "../../store/useDynamicFilterStore.js";
function _() {
  const [t, d] = m(-1), [i, f] = m([]), { getLabel: a } = M(), g = S(), l = o((e) => e.isOpenPopoverMenuFields), h = o((e) => !!e.popoverFilterData), u = o((e) => e.withAllField), p = o((e) => e.availableFields), r = o((e) => e.inputData), {
    addFilter: I,
    closePopoverMenuFields: s,
    getFieldByName: w,
    getNewId: y,
    hidePopoverFilter: D,
    openPopoverMenuFields: c,
    setInputData: F,
    showPopoverFilterForAdd: P,
    setElementRef: v
  } = o((e) => e.actions);
  C(() => {
    l ? (f(
      p.filter(
        (e) => (e.label ?? a(e.dictionaryId)).includes(r)
      )
    ), d(-1)) : f([]);
  }, [l, r, p, a]);
  function k(e) {
    s(), P(e);
  }
  function A() {
    l ? s() : c();
  }
  const x = {
    /**
     * Evento de foco del inputFilter
     */
    onFocus: (e) => {
      c();
    },
    /**
     * Evento de cambio del inputFilter
     */
    onChange: (e) => {
      F(e.target.value), l || c(), D(!0);
    },
    /**
     * Evento de teclado del inputFilter
     */
    onKeyDown: (e) => {
      if (e.key === "ArrowDown") {
        let n = -1;
        i.length > 0 && (t < i.length - 1 ? n = t + 1 : n = i.length - 1), d(n);
      }
      if (e.key === "ArrowUp") {
        let n = -1;
        i.length > 0 && t > -1 && (n = t - 1), d(n);
      }
      if (e.key === "Escape" && s(), e.key === "Enter")
        if (t === -1) {
          if (r.trim() === "") {
            s();
            return;
          }
          u && (I({
            id: y(),
            fieldType: "string",
            field: w(E.name),
            isSet: !0,
            fixed: !1,
            operator: "c",
            operand1: r
          }), F(""), s());
        } else
          k(i[t]);
    }
  };
  return {
    /**
     * Si es esqueleto mostrar la versión de esqueleton
     */
    isSkeleton: g,
    /**
     * Texto de placeholder del inputFilter
     */
    inputPlaceHolder: a(O.input_placeholder),
    /**
     * Texto del inputFilter
     */
    inputData: r,
    /**
     * Props para el handler de eventos del inputFilter
     */
    inputHandlersProps: x,
    /**
     * Si no hay campos disponibles y no se permite filtrar por todos, no se renderiza el inputFilter
     */
    canRender: !(!u && p.length === 0),
    /**
     * Campos filtrados por el inputFilter
     */
    filteredFields: i,
    /**
     * Indice del campo seleccionado
     */
    selectedFieldIndex: t,
    /**
     * Setter de la referencia del elemento (inputFilter)
     */
    setElementRef: v,
    /**
     * Indica si el menú de campos está abierto
     */
    isOpenPopoverMenuFields: l,
    /**
     * Indica si el popoverSort está abierto
     */
    isOpenPopoverFilter: h,
    /**
     * Handler del click en el IconButton trigger (abre/cierra el popover)
     */
    handleTriggerClick: A
  };
}
export {
  _ as default
};
