"use client";
import * as React from "react";
import { hasAllFeatures } from "@open-mercato/shared/security/features";
import { useInjectionDataWidgets } from "../../backend/injection/useInjectionDataWidgets.js";
import { apiCall } from "../../backend/utils/apiCall.js";
import { usePortalContext } from "../PortalContext.js";
function useOptionalOrgSlug() {
  try {
    return usePortalContext().orgSlug;
  } catch {
    return "";
  }
}
function collectRequiredFeatures(items) {
  const set = /* @__PURE__ */ new Set();
  for (const item of items) {
    for (const feature of item.features ?? []) {
      if (!feature || feature.trim().length === 0) continue;
      set.add(feature);
    }
  }
  return Array.from(set);
}
async function readPortalGrantedFeatures(features) {
  if (features.length === 0) return /* @__PURE__ */ new Set();
  try {
    const { ok, result: data } = await apiCall("/api/customer_accounts/portal/feature-check", {
      method: "POST",
      headers: { "content-type": "application/json" },
      body: JSON.stringify({ features })
    });
    if (!ok || !data?.ok) return /* @__PURE__ */ new Set();
    return new Set(data.granted ?? []);
  } catch {
    return /* @__PURE__ */ new Set();
  }
}
function usePortalInjectedMenuItems(surfaceId) {
  const { widgets, isLoading } = useInjectionDataWidgets(surfaceId);
  const orgSlug = useOptionalOrgSlug();
  const [grantedFeatures, setGrantedFeatures] = React.useState(/* @__PURE__ */ new Set());
  const grantedFeatureList = React.useMemo(() => Array.from(grantedFeatures), [grantedFeatures]);
  const rawItems = React.useMemo(() => {
    const entries = [];
    for (const widget of widgets) {
      if (!("menuItems" in widget)) continue;
      const metadataFeatures = widget.metadata.features ?? [];
      for (const menuItem of widget.menuItems) {
        const features = [...metadataFeatures, ...menuItem.features ?? []];
        const normalizedLabelKey = menuItem.labelKey ?? (typeof menuItem.label === "string" && menuItem.label.includes(".") ? menuItem.label : void 0);
        entries.push({
          ...menuItem,
          labelKey: normalizedLabelKey,
          features
        });
      }
    }
    return entries;
  }, [widgets]);
  React.useEffect(() => {
    let mounted = true;
    const run = async () => {
      const features = collectRequiredFeatures(rawItems);
      const next = await readPortalGrantedFeatures(features);
      if (!mounted) return;
      setGrantedFeatures(next);
    };
    void run();
    return () => {
      mounted = false;
    };
  }, [rawItems]);
  const items = React.useMemo(
    () => rawItems.filter((item) => {
      const features = item.features ?? [];
      return features.length === 0 || hasAllFeatures(grantedFeatureList, features);
    }).map((item) => ({
      ...item,
      href: item.href && orgSlug && item.href.startsWith("/portal/") ? `/${orgSlug}${item.href}` : item.href
    })),
    [rawItems, grantedFeatureList, orgSlug]
  );
  return { items, isLoading };
}
export {
  usePortalInjectedMenuItems
};
//# sourceMappingURL=usePortalInjectedMenuItems.js.map
