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