'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var vue = require('vue');
var index = require('../../input/index.js');
var pluginVue_exportHelper = require('../../../_virtual/plugin-vue_export-helper.js');

const _sfc_main = vue.defineComponent({
  name: "ElInputValid",
  components: {
    ElInput: index.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 = vue.ref(props.modelValue);
    const currentValue = vue.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);
    };
    vue.watch(() => props.modelValue, (newVal) => {
      if (newVal) {
        if (Number(newVal) <= props.max) {
          num.value = newVal;
        } else {
          num.value = props.min;
          emit("update:modelValue", props.min);
        }
      } else {
        if (Number(num).value !== Number(newVal)) {
          num.value = newVal;
        }
      }
    }, { immediate: true });
    return {
      num,
      currentValue,
      handleBlur,
      handleFocus,
      setCurrentValue,
      handleInputChange
    };
  }
});
function _sfc_render(_ctx, _cache, $props, $setup, $data, $options) {
  const _component_el_input = vue.resolveComponent("el-input");
  return vue.openBlock(), vue.createBlock(_component_el_input, {
    ref: "input",
    modelValue: _ctx.num,
    "onUpdate:modelValue": _cache[0] || (_cache[0] = ($event) => _ctx.num = $event),
    style: vue.normalizeStyle({ width: _ctx.width }),
    class: vue.normalizeClass([_ctx.className, "el-inputVaild"]),
    disabled: _ctx.disabled,
    placeholder: _ctx.placeholder,
    max: _ctx.max,
    min: _ctx.min,
    "prefix-icon": _ctx.prefixIcon,
    "suffix-icon": _ctx.suffixIcon,
    clearable: _ctx.clearable,
    onBlur: _ctx.handleBlur,
    onFocus: _ctx.handleFocus,
    onInput: _ctx.handleInputChange
  }, null, 8, ["modelValue", "style", "class", "disabled", "placeholder", "max", "min", "prefix-icon", "suffix-icon", "clearable", "onBlur", "onFocus", "onInput"]);
}
var InputValid = /* @__PURE__ */ pluginVue_exportHelper["default"](_sfc_main, [["render", _sfc_render], ["__file", "/Users/linxinjian/work/element-plus/packages/components/input-valid/src/input-valid.vue"]]);

exports["default"] = InputValid;
//# sourceMappingURL=input-valid.js.map
