import type { FeatureMeta, FeatureGroup } from '../types';

export function groupFeaturesByCategory(features: FeatureMeta[]): FeatureGroup[] {
  const groups: FeatureGroup[] = [];
  const seen = new Map<string, FeatureMeta[]>();

  for (const feature of features) {
    if (!feature.group) {
      throw new Error(`Feature '${feature.name}' is missing its group`);
    }
    const groupName = feature.group;
    let arr = seen.get(groupName);
    if (!arr) {
      arr = [];
      seen.set(groupName, arr);
      groups.push({ name: groupName, features: arr });
    }
    arr.push(feature);
  }

  return groups;
}
