import { listItemSectionBuilder } from '@wix/design-system';
import { useBuildOption } from '.';
export function useCollectionOptions({ collection, dropdownCollectionFilterState, renderItem, filter, }) {
    const buildOption = useBuildOption({
        filter,
        renderItem,
        checkbox: true,
    });
    const { result: { keyedItems }, query: { search: { isEmpty: isEmptySearch }, }, } = collection;
    // Original behavior: promote when not searching
    const shouldPromote = isEmptySearch;
    // Always use the snapshot from onOptionsShow to maintain consistent session behavior
    // This prevents items from jumping during the session, even if snapshot is empty
    const itemsToPromote = dropdownCollectionFilterState.promotedItems;
    const promotedItems = shouldPromote
        ? keyedItems.filter((item) => itemsToPromote.includes(item.key))
        : [];
    const finalOptions = promotedItems.length
        ? promotedItems.map(buildOption)
        : [];
    const divider = shouldPromote &&
        promotedItems.length &&
        promotedItems.length !== keyedItems.length
        ? [
            listItemSectionBuilder({
                id: 'promoted-section-divider',
                type: 'divider',
            }),
        ]
        : [];
    const promotedOptions = [
        ...finalOptions,
        ...divider,
    ];
    const promotedItemKeys = new Set(itemsToPromote);
    const otherOptions = (shouldPromote
        ? keyedItems.filter(({ key }) => !promotedItemKeys.has(key))
        : keyedItems).map(buildOption);
    const options = [...(shouldPromote ? promotedOptions : []), ...otherOptions];
    return {
        options,
    };
}
//# sourceMappingURL=useCollectionOptions.js.map