import { jsx as $ } from "@emotion/react/jsx-runtime";
import { forwardRef as V, useRef as j, useImperativeHandle as B, useEffect as s, createContext as H } from "react";
import { useStore as r } from "zustand";
import { shallow as i } from "zustand/shallow";
import { useModuleDictionary as N, useEnvironment as q, useModuleSkeleton as G } from "@m4l/core";
import { useIsMobile as J, useFirstRender as Q } from "@m4l/graphics";
import { useComponentSize as U } from "../../../hooks/useComponentSize/index.js";
import { ALL_FIELD as W } from "../constants.js";
import { formatToRawFilter as X } from "../helpers/formatToRowFilter.js";
import { formatToInitialFilters as Y } from "../helpers/formatToInitialFilters.js";
import { createDynamicFilterStore as Z } from "./DynamicFilterStore.js";
const K = H(null), fe = V((g, S) => {
  const {
    automatic: f = !0,
    withAllField: h = !0,
    fields: v,
    initialAppliedFilters: y = [],
    onChangeFilters: C,
    onDirty: u,
    dataTestId: D = "",
    size: w,
    children: E,
    storeId: R,
    storeDevtoolsEnabled: b = !1,
    visibleRefresh: l = !0
  } = g, { currentSize: n } = U(w), { getLabel: A } = N(), e = j(null), { host_static_assets: M, environment_assets: x } = q(), a = !J(), c = G(), z = Q([a, c]);
  e.current || (e.current = Z(
    {
      allField: {
        ...W,
        urlIcon: `${M}/${x}/frontend/components/dynamic_filter/assets/icons/all_fields.svg`
      },
      withAllField: h,
      fields: v,
      automatic: f,
      getLabel: A,
      dataTestId: D,
      size: n,
      storeId: R,
      visibleRefresh: l,
      ownerState: {
        isSkeleton: c,
        isDirty: !1,
        inEdition: !1,
        isValid: !0,
        isMobile: !a,
        isEmpty: !0,
        size: n
      }
    },
    b
  ), e.current.getState().actions.init(y));
  const I = r(
    e.current,
    (t) => t.actions.fireOnChangeFilters,
    i
  ), O = r(
    e.current,
    (t) => t.actions.openPopoverMenuFields,
    i
  ), P = r(
    e.current,
    (t) => t.actions.showPopoverFilterForAdd,
    i
  ), _ = (t) => {
    try {
      const o = e.current?.getState();
      if (!o)
        return;
      const m = o.actions.getFieldByName(t), k = o.appliedFilters.some(
        (F) => F.field.name === t && F.isSet
      ), T = m.multiple;
      if (k && !T)
        return;
      P(m);
    } catch (o) {
      throw new Error(`Error al agregar el filtro externo: ${o}`);
    }
  }, L = r(
    e.current,
    (t) => () => t.appliedFilters,
    i
  );
  B(S, () => ({
    fireOnChangeFilter: I,
    openPopoverMenuFields: O,
    addExternalFilter: _,
    getCurrentFilters: L
  }));
  const d = r(
    e.current,
    (t) => t.countOnChangeFiltersApplied,
    i
  ), p = r(
    e.current,
    (t) => t.ownerState.isDirty,
    i
  );
  return s(() => {
    if (d === 0)
      return;
    const t = e.current?.getState();
    t && C?.(
      Y(t.appliedFilters),
      X(t.appliedFilters)
    );
  }, [d]), s(() => {
    z || e.current?.getState().actions.setExternalState({ isMobile: !a, isSkeleton: c });
  }, [a, c]), s(() => {
    if (f) {
      e.current?.getState().actions.fireOnChangeFilters();
      return;
    }
  }, []), s(() => {
    u && p && u();
  }, [p, u]), s(() => {
    e.current?.getState().size !== n && e.current?.getState().actions.setSize(n), e.current?.getState().visibleRefresh !== l && e.current?.getState().actions.setVisibleRefresh(l);
  }, [n, l]), /* @__PURE__ */ $(K.Provider, { value: e.current, children: E });
});
export {
  K as DynamicFilterContext,
  fe as DynamicFilterProvider
};
