{"version":3,"file":"useStateful.mjs","sources":["../../../../src/composables/useStateful.ts"],"sourcesContent":["import { ref, computed, watch, type PropType, type Ref, type WritableComputedRef } from 'vue'\nimport { NOT_PROVIDED, useUserProvidedProp } from './useUserProvidedProp'\n\nexport type StatefulProps = {\n  stateful: boolean\n}\n\nexport type StatefulOptions<T> = {\n  eventName?: string\n  /** Prefer to set default value for prop, not here. */\n  defaultValue?: T\n}\n\n/**\n * You could add these props to any component by destructuring them inside props option.\n * @example\n * props: { ...useStatefulProps, componentsOwnProp, etc. }\n * It's better to add props at the beginning, to make sure that Component own props will be used instead in case of collision\n */\nexport const useStatefulProps = {\n  stateful: { type: Boolean as PropType<boolean>, default: false },\n  modelValue: { type: undefined as any },\n}\n\nexport const createStatefulProps = (statefulDefault = false) => {\n  return {\n    stateful: { type: Boolean as PropType<boolean>, default: statefulDefault },\n  }\n}\n\nexport const useStatefulEmits = ['update:modelValue'] as const\n\nexport type StatefulValue<V> = WritableComputedRef<V> & {\n  /** If stateful, means value has inner state, not related to user passed by v-model */\n  stateful: boolean,\n  /** Indicates if props passed by user. If `false`, means default props value is used. */\n  userProvided: boolean\n}\n\n/**\n * Returns `valueComputed` that is proxy for `modelValue` or given key of the props\n * if `stateful` prop is `false`\n * Record<any, any> & Record<'modelValue', T>\n */\nexport const useStateful = <\n  T,\n  Key extends string = 'modelValue',\n  D = T,\n  P extends StatefulProps & (Record<Key, T> | { readonly [key in Key]?: T }) = StatefulProps & (Record<Key, T> | { readonly [key in Key]?: T }),\n  E extends (name: `update:${Key}`, ...args: any[]) => void = (name: `update:${Key}`, ...args: any[]) => void\n>(\n    props: P,\n    emit: E,\n    key: Key = 'modelValue' as Key,\n    options: StatefulOptions<D> = {} as StatefulOptions<D>,\n  ) => {\n  const { eventName, defaultValue } = options\n  const event = (eventName || `update:${key.toString()}`) as `update:${Key}`\n\n  const passedProp = useUserProvidedProp(key, props)\n\n  const defaultValuePassed = 'defaultValue' in options\n\n  const valueState = ref(\n    passedProp.value === NOT_PROVIDED\n      ? defaultValuePassed ? defaultValue : props[key]\n      : passedProp.value,\n  ) as Ref<P[Key]>\n\n  let unwatchModelValue: ReturnType<typeof watch>\n  const watchModelValue = () => {\n    unwatchModelValue = watch(() => props[key], (modelValue) => {\n      valueState.value = modelValue\n    })\n  }\n\n  watch(() => props.stateful, (stateful: boolean) => {\n    stateful ? watchModelValue() : unwatchModelValue?.()\n  }, { immediate: true })\n\n  const valueComputed = computed({\n    get: () => {\n      if (props.stateful) { return valueState.value }\n\n      return props[key]\n    },\n    set: (value) => {\n      if (props.stateful) { valueState.value = value }\n\n      emit(event, value)\n    },\n  }) as StatefulValue<P[Key]>\n\n  Object.defineProperty(valueComputed, 'stateful', {\n    get: () => props.stateful,\n  })\n\n  Object.defineProperty(valueComputed, 'userProvided', {\n    get: () => passedProp.value !== NOT_PROVIDED,\n  })\n\n  return { valueComputed }\n}\n"],"names":[],"mappings":";;AAmBO,MAAM,mBAAmB;AAAA,EAC9B,UAAU,EAAE,MAAM,SAA8B,SAAS,MAAM;AAAA,EAC/D,YAAY,EAAE,MAAM,OAAiB;AACvC;AAEa,MAAA,sBAAsB,CAAC,kBAAkB,UAAU;AACvD,SAAA;AAAA,IACL,UAAU,EAAE,MAAM,SAA8B,SAAS,gBAAgB;AAAA,EAAA;AAE7E;AAEa,MAAA,mBAAmB,CAAC,mBAAmB;AAcvC,MAAA,cAAc,CAOvB,OACA,MACA,MAAW,cACX,UAA8B,OAC3B;AACC,QAAA,EAAE,WAAW,aAAiB,IAAA;AACpC,QAAM,QAAS,aAAa,UAAU,IAAI,SAAU,CAAA;AAE9C,QAAA,aAAa,oBAAoB,KAAK,KAAK;AAEjD,QAAM,qBAAqB,kBAAkB;AAE7C,QAAM,aAAa;AAAA,IACjB,WAAW,UAAU,eACjB,qBAAqB,eAAe,MAAM,GAAG,IAC7C,WAAW;AAAA,EAAA;AAGb,MAAA;AACJ,QAAM,kBAAkB,MAAM;AAC5B,wBAAoB,MAAM,MAAM,MAAM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