"use client";
import { jsx } from "react/jsx-runtime";
import * as React from "react";
import { createContext, useContext } from "react";
import { registerComponentOverrides } from "@open-mercato/shared/modules/widgets/component-registry";
import { apiCall } from "../utils/apiCall.js";
const OverrideUserFeaturesContext = createContext([]);
function useOverrideUserFeatures() {
  return useContext(OverrideUserFeaturesContext);
}
function ComponentOverrideProvider({
  overrides,
  children
}) {
  const [userFeatures, setUserFeatures] = React.useState([]);
  registerComponentOverrides(overrides);
  React.useEffect(() => {
    return () => {
      registerComponentOverrides([]);
    };
  }, [overrides]);
  React.useEffect(() => {
    const requiredFeatures = /* @__PURE__ */ new Set();
    for (const override of overrides) {
      for (const feature of override.features ?? []) {
        if (feature && feature.trim().length > 0) requiredFeatures.add(feature);
      }
    }
    if (requiredFeatures.size === 0) return;
    let cancelled = false;
    const fetchFeatures = async () => {
      const call = await apiCall("/api/auth/feature-check", {
        method: "POST",
        headers: { "content-type": "application/json" },
        body: JSON.stringify({ features: Array.from(requiredFeatures) })
      });
      if (cancelled) return;
      if (call.ok && call.result?.granted) {
        setUserFeatures(call.result.granted);
      }
    };
    void fetchFeatures();
    return () => {
      cancelled = true;
    };
  }, [overrides]);
  return /* @__PURE__ */ jsx(OverrideUserFeaturesContext.Provider, { value: userFeatures, children });
}
var ComponentOverrideProvider_default = ComponentOverrideProvider;
export {
  ComponentOverrideProvider,
  ComponentOverrideProvider_default as default,
  useOverrideUserFeatures
};
//# sourceMappingURL=ComponentOverrideProvider.js.map
