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