import { useModuleDictionary as w } from "@m4l/core";
import { useRef as R, useMemo as C, useCallback as y } from "react";
import { shallow as E } from "zustand/shallow";
import { useDynamicFilterStore as c } from "../../store/useDynamicFilterStore.js";
import { fieldFactory as d } from "../FieldTypes/fieldFactory.js";
function _() {
  let a, t, l, r, s;
  const { getLabel: i } = w(), o = c((e) => e.popoverFilterData), u = c((e) => e.actions.getElementRef(), E), { hidePopoverFilter: n, addFilter: h, updateFilter: v, getNewId: g } = c(
    (e) => e.actions
  ), p = R(o), S = C(() => {
    let e;
    return o && p.current === void 0 ? e = "ready" : o ? e = "reload_values_provider" : e = "initial", p.current = o, e;
  }, [o]);
  if (o?.openFor === "add") {
    a = u?.current, t = o.field;
    const e = d(t.type);
    s = e.getSchema(i, t), r = e.getFormValue(t, i), l = {
      field: t,
      id: g(),
      isSet: !0,
      fixed: !1,
      ...r
    };
  } else if (o) {
    a = u?.current, t = o?.filter.field;
    const e = d(t.type);
    s = e.getSchema(i, t), r = e.getFormValue(t, i, o.filter), l = { ...o?.filter, ...r };
  } else
    r = {}, t = {}, l = {}, a = {};
  const f = y(() => {
    n(!1);
  }, [n]), D = y((e) => {
    e.key === "Escape" && f();
  }, [f]);
  function P() {
    n(!1);
  }
  async function b(e) {
    const m = { ...r, ...e }, F = {
      ...l,
      // todo: mejorar el tipado que se le envía a getFilterFromFormValue
      ...d(m.fieldType).getFilterFromFormValue(m),
      isSet: !0
    };
    o?.openFor === "add" ? h(F) : v(F), n(!1);
  }
  const V = t?.label ?? i(t?.dictionaryId ?? "");
  return {
    field: t,
    labelField: V,
    canRender: !!o,
    anchorEl: a,
    handleKeyDownPopper: D,
    handleClosePopover: P,
    onSubmit: b,
    onClose: f,
    filterFormValue: r,
    formFilter: l,
    popupValidationSchema: s,
    statusLoad: S
  };
}
export {
  _ as default
};
