{"version":3,"file":"use-watch.mjs","sources":["../../../../../../../packages/components/slider/src/composables/use-watch.ts"],"sourcesContent":["import { watch } from 'vue'\nimport { INPUT_EVENT, UPDATE_MODEL_EVENT } from '@element-plus/constants'\nimport { debugWarn, throwError } from '@element-plus/utils'\nimport type { ComputedRef, SetupContext } from 'vue'\nimport type { Arrayable } from '@element-plus/utils'\nimport type { FormItemContext } from '@element-plus/components/form'\nimport type { SliderEmits, SliderInitData, SliderProps } from '../slider'\n\nexport const useWatch = (\n props: SliderProps,\n initData: SliderInitData,\n minValue: ComputedRef,\n maxValue: ComputedRef,\n emit: SetupContext['emit'],\n elFormItem: FormItemContext\n) => {\n const _emit = (val: Arrayable) => {\n emit(UPDATE_MODEL_EVENT, val)\n emit(INPUT_EVENT, val)\n }\n\n const valueChanged = () => {\n if (props.range) {\n return ![minValue.value, maxValue.value].every(\n (item, index) => item === (initData.oldValue as number[])[index]\n )\n } else {\n return props.modelValue !== initData.oldValue\n }\n }\n\n const setValues = () => {\n if (props.min > props.max) {\n throwError('Slider', 'min should not be greater than max.')\n }\n const val = props.modelValue\n if (props.range && Array.isArray(val)) {\n if (val[1] < props.min) {\n _emit([props.min, props.min])\n } else if (val[0] > props.max) {\n _emit([props.max, props.max])\n } else if (val[0] < props.min) {\n _emit([props.min, val[1]])\n } else if (val[1] > props.max) {\n _emit([val[0], props.max])\n } else {\n initData.firstValue = val[0]\n initData.secondValue = val[1]\n if (valueChanged()) {\n if (props.validateEvent) {\n elFormItem?.validate?.('change').catch((err) => debugWarn(err))\n }\n initData.oldValue = val.slice()\n }\n }\n } else if (!props.range && typeof val === 'number' && !Number.isNaN(val)) {\n if (val < props.min) {\n _emit(props.min)\n } else if (val > props.max) {\n _emit(props.max)\n } else {\n initData.firstValue = val\n if (valueChanged()) {\n if (props.validateEvent) {\n elFormItem?.validate?.('change').catch((err) => debugWarn(err))\n }\n initData.oldValue = val\n }\n }\n }\n }\n\n setValues()\n\n watch(\n () => initData.dragging,\n (val) => {\n if (!val) {\n setValues()\n }\n }\n )\n\n watch(\n () => props.modelValue,\n (val, oldVal) => {\n if (\n initData.dragging ||\n (Array.isArray(val) &&\n Array.isArray(oldVal) &&\n val.every((item, index) => item === oldVal[index]) &&\n initData.firstValue === val[0] &&\n initData.secondValue === val[1])\n ) {\n return\n }\n setValues()\n },\n {\n deep: true,\n }\n )\n\n watch(\n () => [props.min, props.max],\n () => {\n setValues()\n }\n )\n}\n"],"names":[],"mappings":";;;;;;AAGY,MAAC,QAAQ,GAAG,CAAC,KAAK,EAAE,QAAQ,EAAE,QAAQ,EAAE,QAAQ,EAAE,IAAI,EAAE,UAAU,KAAK;AACnF,EAAE,MAAM,KAAK,GAAG,CAAC,GAAG,KAAK;AACzB,IAAI,IAAI,CAAC,kBAAkB,EAAE,GAAG,CAAC,CAAC;AAClC,IAAI,IAAI,CAAC,WAAW,EAAE,GAAG,CAAC,CAAC;AAC3B,GAAG,CAAC;AACJ,EAAE,MAAM,YAAY,GAAG,MAAM;AAC7B,IAAI,IAAI,KAAK,CAAC,KAAK,EAAE;AACrB,MAAM,OAAO,CAAC,CAAC,QAAQ,CAAC,KAAK,EAAE,QAAQ,CAAC,KAAK,CAAC,CAAC,KAAK,CAAC,CAAC,IAAI,EAAE,KAAK,KAAK,IAAI,KAAK,QAAQ,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAC,CAAC;AACzG,KAAK,MAAM;AACX,MAAM,OAAO,KAAK,CAAC,UAAU,KAAK,QAAQ,CAAC,QAAQ,CAAC;AACpD,KAAK;AACL,GAAG,CAAC;AACJ,EAAE,MAAM,SAAS,GAAG,MAAM;AAC1B,IAAI,IAAI,EAAE,EAAE,EAAE,CAAC;AACf,IAAI,IAAI,KAAK,CAAC,GAAG,GAAG,KAAK,CAAC,GAAG,EAAE;AAC/B,MAAM,UAAU,CAAC,QAAQ,EAAE,qCAAqC,CAAC,CAAC;AAClE,KAAK;AACL,IAAI,MAAM,GAAG,GAAG,KAAK,CAAC,UAAU,CAAC;A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