import { ComputedRef, Ref, computed, ref, unref } from 'vue'; import { setXor } from '../utils/setXor'; import { setIsEqual } from '../utils/setIsEqual'; export interface useCalcSelectedConfig { multiple: Ref; deselectRepeated: Ref; } export const useCalcSelectedOptions = ( currentSelectedOptions: ComputedRef>, { multiple, deselectRepeated }: useCalcSelectedConfig ) => { const selectedOptionsWhenDragStart = ref>(); const onStart = () => { selectedOptionsWhenDragStart.value = unref(currentSelectedOptions); }; const onEnd = () => { selectedOptionsWhenDragStart.value = undefined; }; const prevSelectedOptions = computed(() => unref(selectedOptionsWhenDragStart) || unref(currentSelectedOptions)); const calcNewSelectedOptions = (selectedOptions: Set) => { if (unref(multiple)) { if (unref(deselectRepeated)) { return setXor(selectedOptions, unref(prevSelectedOptions)); } else { return new Set([...selectedOptions, ...unref(prevSelectedOptions)]); } } else { if (!setIsEqual(selectedOptions, unref(currentSelectedOptions))) { return selectedOptions; } } }; return { calcNewSelectedOptions, onStart, onEnd }; };