import { ReactNode } from 'react'; import { ItemContent, ItemId, ItemProps } from '@snack-uikit/list'; import { BaseOption, ContentRenderProps, FilterOption } from '../types'; import { isAccordionOption, isGroupOption, isGroupSelectOption, isNextListOption } from './typeGuards'; export function transformOptionsToItems( options: FilterOption[], contentRender?: (option: { label: ItemId; value?: ItemId; contentRenderProps?: T; disabled?: boolean }) => ReactNode, ): ItemProps[] { return options.map(option => { if (isAccordionOption(option) || isNextListOption(option)) { const { label, options, id, contentRenderProps, disabled, ...rest } = option; return { ...rest, disabled, id, content: contentRender ? ( contentRender({ label, contentRenderProps, disabled }) ) : ( ), items: transformOptionsToItems(options), }; } if (isGroupSelectOption(option)) { const { options, ...rest } = option; return { ...rest, items: transformOptionsToItems(options), }; } if (isGroupOption(option)) { const { options, ...rest } = option; return { ...rest, items: transformOptionsToItems(options), }; } const { label, value, contentRenderProps, disabled, ...rest } = option as BaseOption; return { ...rest, disabled, id: value, content: contentRender ? ( contentRender({ label, contentRenderProps, disabled, value }) ) : ( ), }; }); }