import { useCallback as y } from "react";
import { axiosOperation as k } from "@m4l/core";
import { getMFObservableLoader as C, getMFRenderFunction as $ } from "../../helpers/mfWindowRegistry/mfWindowRegistry.js";
import { emitFrontendTelemetry as s } from "../../helpers/emitFrontendTelemetry/emitFrontendTelemetry.js";
function A(e) {
  const n = Reflect.get(e, "__version");
  return typeof n == "string" ? n : void 0;
}
function q({
  prefix: e,
  debugPort: n,
  dynamicMFStore: m,
  windowTools: f,
  emergeType: p,
  loggedUser: M,
  environment: _,
  hostTools: D,
  currentLocale: I,
  hostTheme: g,
  formatter: F,
  resolvedHostRuntime: u,
  resolvedObservability: c,
  reportLoaderError: R,
  emitSwapFailedTelemetry: i,
  setStatusLoader: S,
  createTelemetryContext: v
}) {
  return { renderMicroFrontend: y(
    (r) => {
      const l = v(r.moduleId, e), o = C(
        e,
        r.moduleId
      )?.consumeResolvedLoadTelemetry(), V = {
        containerId: r.divContainerId,
        environment: {
          ..._,
          isMicroFrontend: !0,
          mfProps: { moduleId: r.moduleId, debugPort: n }
        },
        hostTools: D,
        currentLocale: I,
        hostTheme: g,
        axiosOperation: k,
        formatter: F,
        dynamicMFStore: m,
        windowTools: f,
        ...u ? { hostRuntime: u } : {},
        ...c ? { observability: c } : {},
        loggedUser: M,
        emergeType: p
      }, a = $(e, r.moduleId);
      if (typeof a != "function")
        return o && s({
          type: "module_load_failed",
          context: l,
          loadSource: o.loadSource,
          attempt: o.attempt,
          totalDurationMs: performance.now() - o.loadStartedAt,
          failureReason: "render_function_missing"
        }), i("render_function_missing"), S("error"), !1;
      try {
        const t = performance.now();
        if (a(V), o) {
          const d = performance.now(), x = A(a) ?? "unknown";
          s({
            type: "module_load_succeeded",
            context: {
              ...l,
              moduleVersion: x
            },
            loadSource: o.loadSource,
            attempt: o.attempt,
            totalDurationMs: d - o.loadStartedAt,
            resolveDurationMs: o.resolveDurationMs,
            loadDurationMs: o.loadDurationMs,
            mountDurationMs: d - t
          });
        }
        return !0;
      } catch (t) {
        if (o) {
          const d = A(a);
          s({
            type: "module_load_failed",
            context: {
              ...l,
              ...d ? { moduleVersion: d } : {}
            },
            loadSource: o.loadSource,
            attempt: o.attempt,
            totalDurationMs: performance.now() - o.loadStartedAt,
            failureReason: "render_exception",
            ...t instanceof Error ? { failureDetail: t.message } : {}
          });
        }
        return i(
          "render_exception",
          t instanceof Error ? t.message : void 0
        ), R(
          `MFLoader render contract execution failed for ${e}/${r.moduleId}`,
          t
        ), !1;
      }
    },
    [
      v,
      I,
      n,
      m,
      i,
      p,
      _,
      F,
      g,
      D,
      M,
      e,
      R,
      c,
      u,
      S,
      f
    ]
  ) };
}
export {
  q as useMFRender
};
