import { useMemo as k, useEffect as V } from "react";
import { shallow as i } from "zustand/shallow";
import { useAreasStore as n } from "../../../../../../../hooks/useAreas/index.js";
import { useHeaderActions as X } from "./useHeaderActions.js";
import { usePopupsStore as Y } from "../../../../../../../../popups/components/PopupsProvider/hooks/usePopupsStore.js";
import { useAuth as Z } from "@m4l/core";
import _ from "../../../../../../../../ToastContainer/helpers/toaster.js";
const se = (o, s) => {
  const [
    c,
    b,
    E,
    F,
    v,
    M,
    U,
    L,
    Q,
    x,
    m,
    z,
    u,
    H
  ] = n((e) => {
    const t = e.hashWindows[o], r = s ? e.hashAreas[s]?.currentLayoutId === o : !1;
    return [
      t?.emergeType,
      t?.moduleId,
      t?.winType,
      t?.title,
      t?.subTitle,
      t?.editionInfo,
      t?.iconUrl,
      t?.version,
      t?.buildTime,
      t?.moduleCount,
      r,
      t?.loading,
      t?.status,
      t?.dynamicMFStore
    ];
  }, i), j = n((e) => e.hashWindows[o]?.mfProps, i), { user: q } = Z(), B = n(
    (e) => e.hashWindows[o]?.component,
    i
  ), { setActions: l, getCookie: p, getCookies: f, setCookie: h, close: g, startProgress: a, stopProgress: A, setFnQueryClose: T, setWindowTitle: y, setWindowSubTitle: D, setWindowEditionInfo: G, loadCookiesFromApi: C } = n((e) => e.windowActions, i), { selectLayout: J } = n((e) => e.areaActions, i), { setSelectedPopupId: K } = Y((e) => e.popupsActions, i), N = () => {
    !m && c === "layout" && s && (J(s, o), K(s, void 0));
  }, O = k(
    () => ({
      getWindowId: () => o,
      /**
       * Actualizar las acciones de la ventana
       */
      setActions: (e, t, r) => {
        l(o, e, t, r);
      },
      getCookie: (e) => p(o, e),
      getCookies: (e) => f(o, e),
      setCookie: (e, t, r) => h(o, e, t, r),
      close: (e = !0) => g(o, e),
      startProgress: () => a(o),
      stopProgress: () => A(o),
      setFnQueryClose: (e) => T(o, e),
      toast: _(o),
      setWindowTitle: (e) => y(o, e),
      setWindowSubTitle: (e) => D(o, e),
      setWindowEditionInfo: (e) => G(o, e),
      hide: () => {
        throw new Error("hide not implemented");
      },
      show: () => {
        throw new Error("show not implemented");
      }
    }),
    // eslint-disable-next-line react-hooks/exhaustive-deps
    [o, l, p, f, h, g, a, A, T, y]
  );
  V(() => {
    u === "init" && C(o);
  }, [u, C, o]);
  const { leftActions: W, insideActions: P, rightActions: S } = X({
    windowId: o,
    areaId: s,
    emergeType: c
  }), R = k(() => [...W, ...P, ...S], [W, P, S]);
  return {
    emergeType: c,
    moduleId: b,
    winType: E,
    title: F,
    subTitle: v,
    editionInfo: M,
    iconUrl: U,
    version: L,
    buildTime: Q,
    moduleCount: x,
    selected: m,
    mfProps: j,
    component: B,
    loading: z,
    status: u,
    dynamicMFStore: H,
    windowTools: O,
    onTouch: N,
    memoizedActions: R,
    loggedUser: q
  };
};
export {
  se as useWindow
};
