import { jsxs as _, jsx as o } from "@emotion/react/jsx-runtime";
import { useRef as d, useEffect as ee } from "react";
import { useNetwork as te, useEnvironment as oe, useHostTools as re } from "@m4l/core";
import { useHostTheme as ne, useFormatter as se, useLocales as ie } from "@m4l/graphics";
import { ProggessLoadingMF as de } from "./subcomponents/ProggessLoadingMF/index.js";
import { LoadingError as ce } from "../LoadingError/LoadingError.js";
import { useHostRuntime as ae } from "../../contexts/HostRuntimeContext/HostRuntimeContext.js";
import { useShellObservability as le } from "../../contexts/ShellObservabilityContext/ShellObservabilityContext.js";
import { projectMFHostRuntimeTools as me } from "./helpers/projectMFHostRuntimeTools/projectMFHostRuntimeTools.js";
import { useMFTelemetry as ue } from "./hooks/useMFTelemetry/useMFTelemetry.js";
import { useMFSwap as pe } from "./hooks/useMFSwap/useMFSwap.js";
import { useMFLifecycle as fe } from "./hooks/useMFLifecycle/useMFLifecycle.js";
import { useMFRender as he } from "./hooks/useMFRender/useMFRender.js";
import { MFLoaderRootStyled as Se, MFLoaderMountContainerStyled as T, MFLoaderOverlayStyled as I } from "./slots/MFLoaderSlots.js";
function Ee(O) {
  const {
    prefix: r,
    moduleId: C,
    cache: c = !0,
    debugPort: n,
    hostRuntime: H,
    observability: x,
    dynamicMFStore: E,
    windowTools: q,
    emergeType: P,
    loggedUser: j
  } = O, G = ae(), V = le(), $ = me(H ?? G), k = x ?? V, { hostThemeOptions: A, fnComponentsOverrides: B } = ne(), { networkOperation: N } = te(), s = oe(), D = re(), U = se(), z = { hostThemeOptions: A, fnComponentsOverrides: B }, p = d(null), f = d(null), h = d("progress"), S = d(
    null
  ), { currentLocale: J } = ie(), K = !s.isLocalhost || Number.isInteger(n) && n > 0, {
    activeSwapRef: L,
    createTelemetryContext: a,
    emitAbandonedLoadTelemetry: Q,
    emitSwapStartedTelemetry: g,
    emitSwapFailedTelemetry: l,
    reportLoaderError: R
  } = ue({
    cache: c,
    prefix: r,
    mountedScriptRefBridge: p,
    requestedScriptRefBridge: f,
    setStatusLoaderRef: S,
    statusLoaderRef: h
  }), {
    mountedScript: m,
    requestedScript: e,
    isSwappingModule: W,
    mountedScriptRef: v,
    requestedScriptRef: y,
    promoteRequestedScript: b
  } = pe({
    activeSwapRef: L,
    cache: c,
    createTelemetryContext: a,
    emitAbandonedLoadTelemetry: Q,
    emitSwapFailedTelemetry: l,
    emitSwapStartedTelemetry: g,
    moduleId: C,
    prefix: r,
    reportLoaderError: R
  });
  p.current = v, f.current = y;
  const X = s.isLocalhost ? `https://localhost:${n}` : `https://${window.location.host}/mf/${e.moduleId}`, {
    statusLoader: t,
    setStatusLoader: F,
    loadingProgress: Y,
    tryAgainOnError: Z,
    loadGeneration: i,
    completedGeneration: M,
    renderedGeneration: u
  } = fe({
    activeHost: X,
    cache: c,
    createTelemetryContext: a,
    environmentIsLocalhost: s.isLocalhost,
    hasValidLocalDebugPort: K,
    mountedScriptRef: v,
    networkOperation: N,
    prefix: r,
    requestedScript: e,
    requestedScriptRef: y,
    activeSwapRef: L,
    emitSwapFailedTelemetry: l,
    emitSwapStartedTelemetry: g
  });
  h.current = t, S.current = F;
  const { renderMicroFrontend: w } = he({
    prefix: r,
    debugPort: n,
    dynamicMFStore: E,
    windowTools: q,
    emergeType: P,
    loggedUser: j,
    environment: s,
    hostTools: D,
    currentLocale: J,
    hostTheme: z,
    formatter: U,
    resolvedHostRuntime: $,
    resolvedObservability: k,
    reportLoaderError: R,
    emitSwapFailedTelemetry: l,
    setStatusLoader: F,
    createTelemetryContext: a
  });
  return ee(() => {
    if (t === "complete" && M.current === i.current && u.current !== i.current) {
      if (!w(e))
        return;
      u.current = i.current, b();
    }
  }, [
    M,
    i,
    m,
    r,
    b,
    w,
    u,
    e,
    t
  ]), /* @__PURE__ */ _(Se, { children: [
    /* @__PURE__ */ o(
      T,
      {
        id: m.divContainerId,
        ownerState: { isVisible: !0 }
      },
      m.divContainerId
    ),
    W ? /* @__PURE__ */ o(
      T,
      {
        id: e.divContainerId,
        ownerState: { isVisible: !1 }
      },
      e.divContainerId
    ) : null,
    t === "progress" ? /* @__PURE__ */ o(I, { children: /* @__PURE__ */ o(de, { loadingProgress: Y }) }) : null,
    t === "error" ? /* @__PURE__ */ o(I, { children: /* @__PURE__ */ o(ce, { status: t, action: Z }) }) : null
  ] });
}
export {
  Ee as MFLoader,
  Ee as default
};
