{"version":3,"file":"useSingleOrMultipleValue.js","sources":["../../src/shared/useSingleOrMultipleValue.ts"],"sourcesContent":["import type { Ref } from 'vue'\nimport type { AcceptableValue, SingleOrMultipleProps } from './types'\nimport { useVModel } from '@vueuse/core'\nimport { isEqual } from 'ohash'\nimport { computed } from 'vue'\nimport { isValueEqualOrExist } from './isValueEqualOrExist'\n\n/**\n * Validates the props and it makes sure that the types are coherent with each other\n *\n * 1. If type, defaultValue, and modelValue are all undefined, throw an error.\n * 2. If modelValue and defaultValue are defined and not of the same type, throw an error.\n * 3. If type is defined:\n * a. If type is 'single' and either modelValue or defaultValue is an array, log an error and return 'multiple'.\n * b. If type is 'multiple' and neither modelValue nor defaultValue is an array, log an error and return 'single'.\n * 4. Return 'multiple' if modelValue is an array, else return 'single'.\n */\nfunction validateProps({ type, defaultValue, modelValue }: SingleOrMultipleProps) {\n const value = modelValue || defaultValue\n const canTypeBeInferred = modelValue !== undefined || defaultValue !== undefined\n\n if (canTypeBeInferred)\n return Array.isArray(value) ? 'multiple' : 'single'\n else\n return type ?? 'single' // always fallback to `single`\n}\n\nfunction getDefaultType({ type, defaultValue, modelValue }: SingleOrMultipleProps) {\n if (type)\n return type\n\n return validateProps({ type, defaultValue, modelValue })\n}\n\nfunction getDefaultValue({ type, defaultValue }: SingleOrMultipleProps) {\n if (defaultValue !== undefined)\n return defaultValue\n\n return (type === 'single') ? undefined : []\n}\n\nexport function useSingleOrMultipleValue
(\n  props: P,\n  emits: (name: Name, ...args: any[]) => void,\n) {\n  const type = computed(() => getDefaultType(props))\n  const modelValue = useVModel(props, 'modelValue', emits, {\n    defaultValue: getDefaultValue(props),\n    passive: (props.modelValue === undefined) as false,\n    deep: true,\n  }) as Ref