import { BlockObjectRequest } from "@notionhq/client/build/src/api-endpoints"; import { isStatGroupDesc, StatDesc, StatGroupDesc, StatsType, } from "../../../statistiques/v2/desc/StatsDesc"; import { formatValue } from "../../../format/formatValue"; 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 type BulletedListItemBlockObjectRequest = Extract< BlockObjectRequest, { bulleted_list_item: object } >; export type BulletedListItemChildren = BulletedListItemBlockObjectRequest["bulleted_list_item"]["children"]; export function createStatGroupChildrenListItemBlock( descriptor: D, stats: StatsType ): BulletedListItemBlockObjectRequest[] { return Object.keys(descriptor.stats).map((statName) => { const childStatDesc = descriptor.stats[statName]; const childStatValue = stats[statName]; return isStatGroupDesc(childStatDesc) ? createStatGroupListItemBlock( childStatDesc, childStatValue as StatsType ) : createStatListItemBlock(childStatDesc, childStatValue as number); }); } function createStatListItemBlock( descriptor: StatDesc, statValue: number ): BulletedListItemBlockObjectRequest { return { bulleted_list_item: { rich_text: [ { text: { content: descriptor.label + ": " + formatValue(statValue, descriptor), }, }, ], }, }; }