import type { CrmReportCatalogGroup, CrmReportDefinitionRecord } from '../../api/types';

export function buildReportCatalogGroups(
  items: CrmReportDefinitionRecord[]
): CrmReportCatalogGroup[] {
  const grouped = new Map<string, CrmReportCatalogGroup>();

  for (const item of items) {
    const current =
      grouped.get(item.category) ??
      {
        category: item.category,
        categoryLabel: item.categoryLabel,
        items: []
      };
    current.items.push(item);
    grouped.set(item.category, current);
  }

  return [...grouped.values()];
}
