import { useRef as s, useCallback as u, useEffect as R } from "react";
import { getMFObservableLoader as C, cleanUpMFInstance as b } from "../../helpers/mfWindowRegistry/mfWindowRegistry.js";
import { emitFrontendTelemetry as l } from "../../helpers/emitFrontendTelemetry/emitFrontendTelemetry.js";
function E({
  cache: f,
  prefix: p,
  mountedScriptRefBridge: y,
  requestedScriptRefBridge: I,
  setStatusLoaderRef: S,
  statusLoaderRef: v
}) {
  const a = s(null), w = s(!0), o = s(p), T = s(f);
  o.current = p, T.current = f;
  const n = u(
    (t, e) => ({
      surface: "host",
      runtimeKind: "web",
      moduleId: t,
      moduleNamespace: e
    }),
    []
  ), m = u(
    (t, e) => {
      const r = C(
        o.current,
        t.moduleId
      )?.consumePendingLoadTelemetry();
      r && l({
        type: "module_load_failed",
        context: n(t.moduleId, o.current),
        loadSource: r.loadSource,
        attempt: r.attempt,
        totalDurationMs: performance.now() - r.loadStartedAt,
        failureReason: e
      });
    },
    [n]
  ), F = u(
    (t, e, r) => {
      a.current = {
        fromModuleId: t,
        toModuleId: e,
        startedAt: performance.now()
      }, l({
        type: "module_swap_started",
        context: n(e, r),
        fromModuleId: t,
        toModuleId: e
      });
    },
    [n]
  ), c = u(
    (t, e) => {
      const r = a.current;
      r && (l({
        type: "module_swap_failed",
        context: n(r.toModuleId, o.current),
        fromModuleId: r.fromModuleId,
        toModuleId: r.toModuleId,
        totalDurationMs: performance.now() - r.startedAt,
        failureReason: t,
        ...e ? { failureDetail: e } : {}
      }), a.current = null);
    },
    [n]
  ), x = u(
    (t, e) => {
      console.error(t, e), w.current && S.current?.("error");
    },
    [S]
  ), i = u(() => ({
    mountedScriptRef: y.current,
    requestedScriptRef: I.current
  }), [y, I]);
  return R(() => () => {
    w.current = !1;
    const { mountedScriptRef: t, requestedScriptRef: e } = i();
    if (!t || !e) {
      c("component_unmounted");
      return;
    }
    const r = [t.current];
    e.current.divContainerId !== t.current.divContainerId && r.push(e.current);
    const _ = /* @__PURE__ */ new Set();
    r.forEach((d) => {
      if (_.has(d.divContainerId))
        return;
      _.add(d.divContainerId), m(d, "component_unmounted"), l({
        type: "module_unmounted",
        context: n(d.moduleId, o.current),
        reason: "host-dispose"
      });
      const M = b(d, o.current, T.current);
      M && console.error(M);
    }), c("component_unmounted");
  }, [
    n,
    m,
    c,
    i
  ]), R(() => {
    if (v.current !== "error")
      return;
    const { requestedScriptRef: t } = i();
    if (!t)
      return;
    const e = C(
      o.current,
      t.current.moduleId
    )?.consumeLastFailureTelemetry();
    e && c(
      e.failureReason,
      e.failureDetail
    );
  }), {
    activeSwapRef: a,
    createTelemetryContext: n,
    emitAbandonedLoadTelemetry: m,
    emitSwapStartedTelemetry: F,
    emitSwapFailedTelemetry: c,
    reportLoaderError: x
  };
}
export {
  E as useMFTelemetry
};
