import { listItemSectionBuilder } from '@wix/design-system'; import { FiltersMap, CollectionState, ArrayField } from '@wix/bex-core'; import { ListItem, useBuildOption } from '.'; import { DropdownCollectionFilterState } from '../state'; import { KeyedItemOption, MultiSelectCheckboxFilterBaseOption, } from '../components/MultiSelectCheckboxFilterBase'; import { useWixPatternsContainer } from '@wix/bex-core/react'; interface UseCollectionOptionsProps { collection: CollectionState; dropdownCollectionFilterState: DropdownCollectionFilterState; renderItem?: (item: V, index: number) => ListItem; filter: ArrayField; } export function useCollectionOptions({ collection, dropdownCollectionFilterState, renderItem, filter, }: UseCollectionOptionsProps) { const { translate } = useWixPatternsContainer(); const { dropdownSelect } = dropdownCollectionFilterState; const buildOption = useBuildOption({ filter, renderItem, checkbox: true, getOptionState: (key) => dropdownSelect.canToggle(key) ? {} : { disabled: true, disabledDescription: translate( 'cairo.picker.itemsSelected.maxSelected.tooltip', ), }, }); 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: MultiSelectCheckboxFilterBaseOption[] = [ ...finalOptions, ...divider, ]; const promotedItemKeys = new Set(itemsToPromote); const otherOptions: KeyedItemOption[] = ( shouldPromote ? keyedItems.filter(({ key }) => !promotedItemKeys.has(key)) : keyedItems ).map(buildOption); const options = [...(shouldPromote ? promotedOptions : []), ...otherOptions]; return { options, }; }