import { listItemSectionBuilder, listItemSelectBuilder, } from '@wix/design-system';
import { useCallback, useMemo } from 'react';
export function useBuildOption({ filter, checkbox, renderItem, }) {
    return useCallback((keyedItem, index) => {
        const { key, item } = keyedItem;
        const option = renderItem?.(item, index);
        const title = filter.itemName(item);
        return Object.assign(listItemSelectBuilder({
            id: key,
            checkbox,
            ellipsis: true,
            title: filter.itemName(item),
            label: title,
            ...option,
        }), {
            sectionId: option?.sectionId,
            keyedItem,
        });
    }, [renderItem, checkbox]);
}
export function useBuildGroupedOptions({ items, renderSection, }) {
    return useMemo(() => {
        if (!renderSection) {
            return items;
        }
        const { groups } = items.reduce((acc, option) => {
            const sectionId = option.sectionId || '';
            let group;
            if (acc._cache[sectionId]) {
                group = acc._cache[sectionId];
            }
            else {
                group = [
                    listItemSectionBuilder(renderSection(sectionId)),
                ];
                acc.groups.push(group);
                acc._cache[sectionId] = group;
            }
            group.push(option);
            return acc;
        }, {
            _cache: {},
            groups: [],
        });
        return groups.flatMap((e) => e);
    }, [items, renderSection]);
}
//# sourceMappingURL=useBuildOption.js.map