import { insertByInjectionPlacement } from "@open-mercato/shared/modules/widgets/injection-position";
function toMergedInjectedItem(item) {
  return {
    id: item.id,
    label: item.label,
    labelKey: item.labelKey,
    icon: item.icon,
    href: item.href,
    onClick: item.onClick,
    separator: item.separator,
    badge: item.badge,
    groupId: item.groupId,
    groupLabel: item.groupLabel,
    groupLabelKey: item.groupLabelKey,
    groupOrder: item.groupOrder,
    children: item.children,
    source: "injected"
  };
}
function mergeMenuItems(builtIn, injected) {
  let merged = builtIn.map((item) => ({
    ...item,
    id: item.id,
    source: "built-in"
  }));
  const existingIds = new Set(builtIn.map((i) => i.id));
  const existingHrefs = new Set(builtIn.map((i) => i.href).filter(Boolean));
  for (const item of injected) {
    if (existingIds.has(item.id)) continue;
    if (item.href && existingHrefs.has(item.href)) continue;
    const nextItem = toMergedInjectedItem(item);
    existingIds.add(item.id);
    if (item.href) existingHrefs.add(item.href);
    if (!item.placement && item.groupId) {
      const existingGroupIndexes = merged.map((entry, index) => ({ entry, index })).filter(({ entry }) => entry.groupId === item.groupId);
      if (existingGroupIndexes.length > 0) {
        const insertAfter = existingGroupIndexes[existingGroupIndexes.length - 1]?.index ?? -1;
        merged.splice(insertAfter + 1, 0, nextItem);
        continue;
      }
    }
    merged = insertByInjectionPlacement(merged, nextItem, item.placement, (entry) => entry.id);
  }
  return merged;
}
export {
  mergeMenuItems
};
//# sourceMappingURL=mergeMenuItems.js.map
