import type { SectionData, CombinedOptionsType, SelectOptionType, } from './types'; export const isSections = >( options: CombinedOptionsType ): options is SectionData[] => { const firstOption = options[0]; return ( firstOption !== undefined && (firstOption as SectionData).category !== undefined ); }; export const toSections = >( options: CombinedOptionsType ): SectionData[] => { if (isSections(options)) { return options; } return [{ category: '', data: options }]; }; export const toFlatOptions = >( options: CombinedOptionsType ): SelectOptionType[] => { if (isSections(options)) { return options.flatMap((opt) => opt.data); } return options; }; type ScrollParams = { itemIndex: number; sectionIndex: number; }; export const getScrollParams = >( value: V, sections: SectionData[] ): ScrollParams => { let itemIndex = -1; const sectionIndex = sections.findIndex((section) => { itemIndex = section.data.findIndex((opt) => opt.value === value); return itemIndex !== -1; }); return { sectionIndex: sectionIndex < 0 ? 0 : sectionIndex, itemIndex: itemIndex < 0 ? 0 : itemIndex, }; };