import { ReactNode } from 'react'; import { ItemId } from '@snack-uikit/list'; import { ContentRenderProps, FilterOption } from '../types'; import { isBaseOption } from './typeGuards'; export type FlattenOption = { value: ItemId; label: ItemId; contentRenderProps?: T; disabled?: boolean; hidden?: boolean; afterContent?: ReactNode; beforeContent?: ReactNode; }; type KindFlattenOptionsProps = { options: FilterOption[]; }; export function kindFlattenOptions({ options, }: KindFlattenOptionsProps): { flattenOptions: Record>; } { const flattenOptions: Record> = {}; function flatten(option: FilterOption) { if (isBaseOption(option)) { const { value, label, contentRenderProps, disabled, afterContent, beforeContent, hidden } = option; flattenOptions[value] = { value, label, contentRenderProps, disabled, afterContent, beforeContent, hidden, }; return; } const { options } = option; for (let idx = 0; idx < options.length; idx++) { flatten(options[idx]); } return; } for (let idx = 0; idx < options.length; idx++) { flatten(options[idx]); } return { flattenOptions, }; }