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