import { slugifySidebarId } from "@open-mercato/shared/modules/navigation/sidebarPreferences";
function resolveGroupKey(group) {
  if (group.id && group.id.length) return group.id;
  if (group.defaultName && group.defaultName.length) return slugifySidebarId(group.defaultName);
  return slugifySidebarId(group.name);
}
function resolveItemKey(item) {
  const candidate = item.id?.trim();
  if (candidate && candidate.length > 0) return candidate;
  return item.href;
}
function cloneSidebarGroups(groups) {
  const cloneItem = (item) => ({
    id: item.id,
    href: item.href,
    title: item.title,
    defaultTitle: item.defaultTitle,
    icon: item.icon,
    iconName: item.iconName,
    iconMarkup: item.iconMarkup,
    enabled: item.enabled,
    hidden: item.hidden,
    pageContext: item.pageContext,
    children: item.children ? item.children.map((child) => cloneItem(child)) : void 0
  });
  return groups.map((group) => ({
    id: group.id,
    name: group.name,
    defaultName: group.defaultName,
    items: group.items.map((item) => cloneItem(item))
  }));
}
function mergeGroupOrder(preferred, current) {
  const seen = /* @__PURE__ */ new Set();
  const merged = [];
  for (const id of preferred) {
    const trimmed = id.trim();
    if (!trimmed || seen.has(trimmed) || !current.includes(trimmed)) continue;
    seen.add(trimmed);
    merged.push(trimmed);
  }
  for (const id of current) {
    if (seen.has(id)) continue;
    seen.add(id);
    merged.push(id);
  }
  return merged;
}
function applyItemOrder(items, keyOf, preferred) {
  if (!preferred || preferred.length === 0) return items;
  const byKey = /* @__PURE__ */ new Map();
  for (const item of items) byKey.set(keyOf(item), item);
  const seen = /* @__PURE__ */ new Set();
  const ordered = [];
  for (const key of preferred) {
    if (seen.has(key)) continue;
    const match = byKey.get(key);
    if (!match) continue;
    ordered.push(match);
    seen.add(key);
  }
  for (const item of items) {
    const key = keyOf(item);
    if (seen.has(key)) continue;
    ordered.push(item);
    seen.add(key);
  }
  return ordered;
}
function applyItemDraft(item, draft) {
  const itemKey = resolveItemKey(item);
  const baseTitle = item.defaultTitle ?? item.title;
  const override = draft.itemLabels[itemKey]?.trim();
  const children = item.children ? item.children.map((child) => applyItemDraft(child, draft)) : void 0;
  const hidden = draft.hiddenItemIds[itemKey] === true;
  return {
    ...item,
    title: override && override.length > 0 ? override : baseTitle,
    hidden,
    children
  };
}
function applyCustomizationDraft(baseGroups, draft) {
  const clones = cloneSidebarGroups(baseGroups);
  const byId = /* @__PURE__ */ new Map();
  for (const group of clones) {
    byId.set(resolveGroupKey(group), group);
  }
  const orderedIds = mergeGroupOrder(draft.order, Array.from(byId.keys()));
  const seen = /* @__PURE__ */ new Set();
  const result = [];
  for (const id of orderedIds) {
    if (seen.has(id)) continue;
    const group = byId.get(id);
    if (!group) continue;
    seen.add(id);
    const baseName = group.defaultName ?? group.name;
    const override = draft.groupLabels[id]?.trim();
    const orderedItems = applyItemOrder(group.items, resolveItemKey, draft.itemOrder?.[id]);
    result.push({
      ...group,
      name: override && override.length > 0 ? override : baseName,
      items: orderedItems.map((item) => applyItemDraft(item, draft))
    });
  }
  return result;
}
function filterMainSidebarGroups(groups) {
  const isMainItem = (item) => {
    if (item.pageContext && item.pageContext !== "main") return false;
    return true;
  };
  return groups.map((group) => ({
    ...group,
    items: group.items.filter(isMainItem).map((item) => ({
      ...item,
      children: item.children?.filter(isMainItem)
    }))
  })).filter((group) => group.items.length > 0);
}
function collectSidebarDefaults(groups) {
  const groupDefaults = /* @__PURE__ */ new Map();
  const itemDefaults = /* @__PURE__ */ new Map();
  const visitItems = (items) => {
    for (const item of items) {
      const key = resolveItemKey(item);
      const baseTitle = item.defaultTitle ?? item.title;
      itemDefaults.set(key, baseTitle);
      itemDefaults.set(item.href, baseTitle);
      if (item.children && item.children.length > 0) visitItems(item.children);
    }
  };
  for (const group of groups) {
    const key = resolveGroupKey(group);
    groupDefaults.set(key, group.defaultName ?? group.name);
    visitItems(group.items);
  }
  return { groupDefaults, itemDefaults };
}
export {
  applyCustomizationDraft,
  applyItemOrder,
  cloneSidebarGroups,
  collectSidebarDefaults,
  filterMainSidebarGroups,
  mergeGroupOrder,
  resolveGroupKey,
  resolveItemKey
};
//# sourceMappingURL=customization-helpers.js.map
