import { GroupItem, TwoFieldGroup } from 'src/components/AdvancedFilter/types';

type Item = {
  id: number | string;
  name: string;
};

type Group = {
  id: number | string;
  name: string;
  items: Item[];
};

const parseItems = (group: Group): GroupItem[] =>
  group.items.map(i => ({
    label: i.name,
    value: i.id,
    checked: false,
  }));

export const parseGroup = (groups: Group[]) =>
  groups.reduce<TwoFieldGroup[]>((acc, cur) => {
    if (cur.items.length) {
      acc.push({
        name: cur.name,
        value: cur.id,
        items: parseItems(cur),
      });
    }
    return acc;
  }, []);
