import { isMultiValueStatDesc, isSingleValueStatDesc, isStatGroupDesc, StatGroupDesc, StatsType, } from "../../../statistiques/v2/desc/StatsDesc"; import { createSingleValueStatListItemBlock } from "./createSingleValueStatListItemBlock"; import { BulletedListItemBlockObjectRequest } from "../blocks/BulletedListItemBlockObjectRequest"; import { BulletedListItemChildren } from "../blocks/BulletedListItemChildren"; import { createMultiValueStatListItemBlock } from "./createMultiValueStatListItemBlock"; import { ValueFormatOptions } from "../../../format/ValueFormatOptions"; export function createStatGroupListItemBlock( descriptor: D, stats: StatsType ): BulletedListItemBlockObjectRequest { return { bulleted_list_item: { rich_text: [ { text: { content: descriptor.label, }, }, ], children: createStatGroupChildrenListItemBlock( descriptor, stats ) as BulletedListItemChildren, }, }; } export function createStatGroupChildrenListItemBlock( descriptor: D, stats: StatsType ): BulletedListItemBlockObjectRequest[] { return Object.keys(descriptor.stats).map((statName) => { const childStatDesc = descriptor.stats[statName]; const childStatValue = stats[statName]; if (isStatGroupDesc(childStatDesc)) { return createStatGroupListItemBlock( childStatDesc, childStatValue as StatsType ); } if (isSingleValueStatDesc(childStatDesc)) { return createSingleValueStatListItemBlock( childStatDesc.label, childStatDesc as ValueFormatOptions, childStatValue as number ); } if (isMultiValueStatDesc(childStatDesc)) { return createMultiValueStatListItemBlock( childStatDesc, childStatValue as Record ); } throw "Mussing case"; }); }