import { defineComponent, ref, watchEffect } from 'vue';
import { ElInput } from '../../input/index.mjs';

var script = defineComponent({
  name: "ElInputValid",
  components: {
    ElInput
  },
  props: {
    width: String,
    placeholder: String,
    min: {
      type: Number,
      default: 0
    },
    max: {
      type: Number,
      default: 2e7
    },
    modelValue: {
      type: [Number, String]
    },
    disabled: {
      type: Boolean,
      default: false
    },
    deleteAll: {
      type: Boolean,
      default: true
    },
    isInt: {
      type: Boolean,
      default: true
    },
    precision: {
      type: Number,
      default: 2
    },
    className: String,
    suffixIcon: String,
    prefixIcon: String,
    clearable: {
      type: Boolean,
      default: false
    }
  },
  events: ["input", "change", "blur", "focus", "update:modelValue"],
  setup(props, { emit }) {
    const num = ref("");
    const currentValue = ref(0);
    const handleBlur = (event) => {
      emit("blur", event);
    };
    const handleFocus = (event) => {
      emit("focus", event);
    };
    const setCurrentValue = (val) => {
      let newVal = val;
      if (!props.deleteAll && !newVal && newVal !== 0) {
        newVal = props.min;
      }
      if (props.deleteAll && !newVal && newVal !== 0) {
        newVal = void 0;
      }
      if (newVal !== "0.") {
        if (newVal >= props.max)
          newVal = props.max;
        if (newVal <= props.min)
          newVal = props.min;
      }
      const oldVal = currentValue.value;
      num.value = newVal;
      emit("update:modelValue", newVal);
      emit("change", newVal, oldVal);
      currentValue.value = newVal;
    };
    const handleInputChange = (param) => {
      let val = param;
      if (props.isInt) {
        if (val.indexOf(".") > -1) {
          val = val.split(".")[0];
        }
        val = val.replace(/[^(0)*(\d+)]/g, "");
      } else {
        if (val === ".") {
          val = "0";
        }
        val = val.replace(/[^(\-)*(\d+).]/g, "");
        val = val.replace(/\.{2,}/g, ".");
        val = val.replace(/\-{2,}/g, "-");
        val = val.replace(".", "$#$").replace(/\./g, "").replace("$#$", ".");
        val = val.replace(/^(\-)*(\d+)\.(\d{1,2}).*$/, "$1$2.$3");
      }
      if (val.indexOf(".") < 0 && val !== "" && val !== "-") {
        val = parseFloat(val);
      }
      setCurrentValue(val);
    };
    watchEffect(props.modelValue, (newVal) => {
      if (newVal) {
        if (Number(newVal) <= props.max) {
          num.value = newVal.value;
        } else {
          num.value = props.min;
          emit("update:modelValue", props.min);
        }
      } else {
        if (Number(num).value !== Number(newVal)) {
          num.value = newVal;
        }
      }
    });
    return {
      num,
      currentValue,
      handleBlur,
      handleFocus,
      setCurrentValue,
      handleInputChange
    };
  }
});

export { script as default };
//# sourceMappingURL=input-valid.vue_vue_type_script_lang.mjs.map
