"use strict";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const event = require("../../../../constants/event.js");
const types = require("../../../../utils/types.js");
const error = require("../../../../utils/error.js");
const Vue = require("vue");
const shared_esmBundler = require("../../../../../../@vue/shared/dist/shared.esm-bundler.js");
const useWatch = (props, initData, minValue, maxValue, emit, elFormItem) => {
  const _emit = (val) => {
    emit(event.UPDATE_MODEL_EVENT, val);
    emit(event.INPUT_EVENT, val);
  };
  const valueChanged = () => {
    if (props.range) return ![minValue.value, maxValue.value].every((item, index) => item === initData.oldValue[index]);
    else return props.modelValue !== initData.oldValue;
  };
  const setValues = () => {
    var _a, _b;
    if (props.min > props.max) error.throwError("Slider", "min should not be greater than max.");
    const val = props.modelValue;
    if (props.range && shared_esmBundler.isArray(val)) if (val[1] < props.min) _emit([props.min, props.min]);
    else if (val[0] > props.max) _emit([props.max, props.max]);
    else if (val[0] < props.min) _emit([props.min, val[1]]);
    else if (val[1] > props.max) _emit([val[0], props.max]);
    else {
      initData.firstValue = val[0];
      initData.secondValue = val[1];
      if (valueChanged()) {
        if (props.validateEvent) (_a = elFormItem == null ? void 0 : elFormItem.validate) == null ? void 0 : _a.call(elFormItem, "change").catch((err) => error.debugWarn(err));
        initData.oldValue = val.slice();
      }
    }
    else if (!props.range && types.isNumber(val) && !Number.isNaN(val)) if (val < props.min) _emit(props.min);
    else if (val > props.max) _emit(props.max);
    else {
      initData.firstValue = val;
      if (valueChanged()) {
        if (props.validateEvent) (_b = elFormItem == null ? void 0 : elFormItem.validate) == null ? void 0 : _b.call(elFormItem, "change").catch((err) => error.debugWarn(err));
        initData.oldValue = val;
      }
    }
  };
  setValues();
  Vue.watch(() => initData.dragging, (val) => {
    if (!val) setValues();
  });
  Vue.watch(() => props.modelValue, (val, oldVal) => {
    if (initData.dragging || shared_esmBundler.isArray(val) && shared_esmBundler.isArray(oldVal) && val.every((item, index) => item === oldVal[index]) && initData.firstValue === val[0] && initData.secondValue === val[1]) return;
    setValues();
  }, { deep: true });
  Vue.watch(() => [props.min, props.max], () => {
    setValues();
  });
};
exports.useWatch = useWatch;
