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