"use client";
import { useState, useEffect, useCallback } from "react";
import { getEnricherTimingEntries } from "@open-mercato/shared/lib/umes/enricher-timing";
import { getInterceptorActivityEntries } from "@open-mercato/shared/lib/umes/interceptor-activity";
const EMPTY_DATA = {
  extensions: [],
  conflicts: [],
  enricherTimings: [],
  interceptorActivity: [],
  eventFlow: [],
  componentReplacements: []
};
const isDev = process.env.NODE_ENV === "development";
function useUmesDevTools(isActive = true) {
  const [data, setData] = useState(EMPTY_DATA);
  const refresh = useCallback(() => {
    if (!isDev) return;
    setData(collectDevToolsData());
  }, []);
  useEffect(() => {
    if (!isDev || !isActive) return;
    refresh();
    const interval = setInterval(refresh, 3e3);
    return () => clearInterval(interval);
  }, [refresh, isActive]);
  return { ...isDev ? data : EMPTY_DATA, refresh };
}
function collectDevToolsData() {
  const extensions = [];
  const componentReplacements = [];
  try {
    const enricherEntries = globalThis.__openMercatoResponseEnrichers__ ?? [];
    for (const entry of enricherEntries) {
      extensions.push({
        type: "enricher",
        id: entry.enricher.id,
        moduleId: entry.moduleId,
        target: entry.enricher.targetEntity,
        priority: entry.enricher.priority ?? 0,
        features: entry.enricher.features,
        targetEntity: entry.enricher.targetEntity,
        timeout: entry.enricher.timeout,
        critical: entry.enricher.critical,
        hasCacheConfig: !!entry.enricher.cache,
        hasQueryEngineConfig: !!entry.enricher.queryEngine
      });
    }
  } catch {
  }
  try {
    const interceptorEntries = globalThis.__openMercatoApiInterceptors__ ?? [];
    for (const entry of interceptorEntries) {
      extensions.push({
        type: "interceptor",
        id: entry.interceptor.id,
        moduleId: entry.moduleId,
        target: entry.interceptor.targetRoute,
        priority: entry.interceptor.priority ?? 0,
        features: entry.interceptor.features,
        targetRoute: entry.interceptor.targetRoute,
        methods: entry.interceptor.methods,
        hasBefore: !!entry.interceptor.before,
        hasAfter: !!entry.interceptor.after
      });
    }
  } catch {
  }
  try {
    const globalState = globalThis.__openMercatoComponentRegistry__;
    if (globalState?.overrides) {
      for (const override of globalState.overrides) {
        const kind = override.replacement ? "replacement" : override.wrapper ? "wrapper" : "propsTransform";
        const info = {
          type: "component-override",
          id: `${override.metadata?.module ?? "unknown"}.${override.target.componentId}`,
          moduleId: override.metadata?.module ?? "unknown",
          target: override.target.componentId,
          priority: override.priority,
          features: override.features,
          componentId: override.target.componentId,
          overrideKind: kind
        };
        extensions.push(info);
        componentReplacements.push(info);
      }
    }
  } catch {
  }
  try {
    const tables = globalThis.__openMercatoCoreInjectionTables__ ?? [];
    for (const tableEntry of tables) {
      for (const [spotId, value] of Object.entries(tableEntry.table)) {
        const slots = Array.isArray(value) ? value : [value];
        for (const slot of slots) {
          const widgetId = typeof slot === "string" ? slot : slot?.widgetId;
          const priority = typeof slot === "object" ? slot?.priority ?? 0 : 0;
          if (widgetId) {
            extensions.push({
              type: "injection-widget",
              id: widgetId,
              moduleId: tableEntry.moduleId,
              target: spotId,
              priority,
              spotId,
              hasEventHandlers: false
            });
          }
        }
      }
    }
  } catch {
  }
  return {
    extensions,
    conflicts: [],
    enricherTimings: getEnricherTimingEntries(),
    interceptorActivity: getInterceptorActivityEntries(),
    eventFlow: [],
    componentReplacements
  };
}
export {
  useUmesDevTools
};
//# sourceMappingURL=useUmesDevTools.js.map
