"use client";
import { jsx } from "react/jsx-runtime";
import * as React from "react";
import { getComponentEntry, getComponentOverrides } from "@open-mercato/shared/modules/widgets/component-registry";
import { useOverrideUserFeatures } from "./ComponentOverrideProvider.js";
class ReplacementErrorBoundary extends React.Component {
  constructor(props) {
    super(props);
    this.state = { hasError: false };
  }
  static getDerivedStateFromError() {
    return { hasError: true };
  }
  componentDidCatch(error) {
    this.props.onError(error);
  }
  render() {
    if (this.state.hasError) return this.props.fallback;
    return this.props.children;
  }
}
function useRegisteredComponent(componentId, fallback) {
  const userFeatures = useOverrideUserFeatures();
  return React.useMemo(() => {
    const entry = getComponentEntry(componentId);
    const original = entry?.component ?? fallback ?? null;
    if (!original) {
      if (process.env.NODE_ENV !== "production" && !fallback) {
        console.warn(`[UMES] Component "${componentId}" is not registered.`);
      }
      const Missing = () => null;
      return Missing;
    }
    const overrides = getComponentOverrides(componentId, userFeatures);
    const replacementOverrides = overrides.filter((override) => "replacement" in override);
    if (process.env.NODE_ENV !== "production" && replacementOverrides.length > 1) {
      console.warn(
        `[UMES] Multiple replacements registered for "${componentId}". Highest-priority replacement is applied.`
      );
    }
    let replacement = null;
    let replacementOverride = null;
    const wrappers = [];
    const transforms = [];
    for (const override of overrides) {
      if ("replacement" in override) {
        replacement = override.replacement;
        replacementOverride = override;
      }
      if ("wrapper" in override) wrappers.push(override.wrapper);
      if ("propsTransform" in override) transforms.push(override.propsTransform);
    }
    const base = replacement ?? original;
    const wrapped = wrappers.reduce((acc, wrapper) => wrapper(acc), base);
    const Resolved = (props) => {
      const transformed = transforms.reduce((current, transform) => transform(current), props);
      const Fallback = React.createElement(original, transformed);
      if (process.env.NODE_ENV !== "production" && replacementOverride && "replacement" in replacementOverride) {
        const validation = replacementOverride.propsSchema.safeParse(transformed);
        if (!validation.success) {
          console.error(
            `[UMES] Props schema validation failed for replacement "${componentId}" from module "${replacementOverride.metadata?.module ?? "unknown"}"`,
            validation.error.format()
          );
          return Fallback;
        }
      }
      return /* @__PURE__ */ jsx(
        ReplacementErrorBoundary,
        {
          fallback: Fallback,
          onError: (error) => {
            const replacementModule = overrides.find((override) => "replacement" in override)?.metadata?.module ?? "unknown";
            console.error(`[UMES] Component replacement failed for "${componentId}" from module "${replacementModule}"`, error);
          },
          children: React.createElement(wrapped, transformed)
        }
      );
    };
    Resolved.displayName = `RegisteredComponent(${componentId})`;
    return Resolved;
  }, [componentId, fallback, userFeatures]);
}
var useRegisteredComponent_default = useRegisteredComponent;
export {
  useRegisteredComponent_default as default,
  useRegisteredComponent
};
//# sourceMappingURL=useRegisteredComponent.js.map
