/*! bxs-vue-ui v3.3.0 */
'use strict';

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

require('../utils/entry-virtual.js');
var normalizeComponent = require('../utils/normalize-component.js');
require('../utils/object-create.js');
require('../utils/html.js');
require('../utils/es.number.constructor.js');
require('../utils/es.object.to-string.js');
require('../utils/array-iteration.js');
require('../utils/sloppy-array-method.js');
require('../utils/for-each.js');
require('../utils/string-trim.js');
require('../utils/extends.js');
require('vue');
require('../icon/index.js');
require('../utils/splice.js');
var emitter = require('../utils/emitter.js');
require('../utils/a-function.js');
require('../utils/species-constructor.js');
require('../utils/es.string.split.js');
var isNan = require('../utils/is-nan.js');
require('../utils/set-timeout.js');
var toast = require('../toast/index.js');
require('../utils/es.number.to-fixed.js');
var _parseFloat = require('../utils/parse-float.js');

function washNumFn(num, precision) {
  var value = "" + num;

  if (precision) {
    value = _parseFloat._parseFloat(num).toFixed(precision);
  } else {
    value = "" + Math.round(_parseFloat._parseFloat(num));
  }

  return value;
}

var script = normalizeComponent.create({
  name: 'stepper',
  mixins: [emitter.Emitter],
  props: {
    disabled: {
      type: Boolean,
      default: false
    },
    disableInput: {
      type: Boolean,
      default: false
    },
    value: {
      type: Number,
      default: 0
    },
    min: {
      type: Number,
      default: -Infinity
    },
    max: {
      type: Number,
      default: Infinity
    },
    step: {
      type: Number,
      default: 1
    },
    precision: {
      type: Number,
      default: 0
    }
  },
  data: function data() {
    return {
      num: this.value,
      // 绑定值
      oldNum: this.value // 绑定老值

    };
  },
  computed: {
    disabledAddClass: {
      get: function get() {
        if (this.disabled) {
          return 'bx-number-disabled';
        }

        if (this.max && Number(this.num) >= this.max) {
          return 'bx-number-disabled';
        }

        return '';
      }
    },
    disabledMinusClass: {
      get: function get() {
        if (this.disabled) {
          return 'bx-number-disabled';
        }

        if (this.min && Number(this.num) <= this.min) {
          return 'bx-number-disabled';
        }

        return '';
      }
    },
    disableInputClass: {
      get: function get() {
        if (this.disableInput) {
          return 'bx-input-disabled';
        }

        if (this.disabled) {
          return 'bx-input-disabled';
        }

        return '';
      }
    }
  },
  model: {
    prop: 'value',
    event: 'change'
  },
  watch: {
    value: {
      handler: function handler() {
        var washedValue = washNumFn(this.value, this.precision);

        if (washedValue !== this.num) {
          if (_parseFloat._parseFloat(this.value) > this.max) {
            this.$emit('change', this.max);
            this.num = this.max;
          } else if (_parseFloat._parseFloat(this.value) < this.min) {
            this.$emit('change', this.min);
            this.num = this.min;
          } else {
            this.num = washedValue;
          }
        }

        if (this.value !== this.num - 0) {
          this.$emit('change', this.num - 0);
        }
      },
      immediate: true
    }
  },
  methods: {
    // 输入框失去焦点时
    onblurFn: function onblurFn() {
      var number = _parseFloat._parseFloat(this.num);

      if (this.num === '') {
        toast.default('请输入数字');
        this.num = this.oldNum;
        return;
      }

      if (isNan._Number$isNaN(Number(this.num))) {
        toast.default('请输入数字');
        this.num = this.oldNum;
        return;
      }

      if (number > this.max) {
        toast.default("\u8BF7\u8F93\u4E0D\u5927\u4E8E" + this.max + "\u7684\u6570\u503C");
        this.num = this.max;
        return;
      }

      if (number < this.min) {
        toast.default("\u8BF7\u8F93\u4E0D\u5C0F\u4E8E" + this.min + "\u7684\u6570\u503C");
        this.num = this.min;
        return;
      }

      if (!this.num || this.step.toString().split('.').length - 1 > this.precision) {
        this.num = this.oldNum;
      } else {
        this.num = number.toFixed(this.precision);
      }

      this.oldNum = this.num;

      if (this.num !== this.oldNum) {
        this.$emit('change', _parseFloat._parseFloat(this.num));
        this.dispatch('bxs-form-item', 'form.item.validate');
      }

      this.$emit('blur', _parseFloat._parseFloat(this.num));
    },
    // 输入框获取焦点时
    onfocusFn: function onfocusFn() {
      if (isNan._Number$isNaN(Number(this.num))) {
        this.num = this.oldNum;
      }

      if (this.num) {
        if (this.precision > 0) {
          this.num = _parseFloat._parseFloat(this.num).toFixed(this.precision);
        } else {
          this.num = "" + this.num;
        }
      }

      this.oldNum = this.num;
      this.$emit('focus', _parseFloat._parseFloat(this.num));
      this.dispatch('bxs-form-item', 'form.item.reset');
    },
    // 加减操作
    changeNum: function changeNum(type) {
      var fixNum = this.precision;

      var num = _parseFloat._parseFloat(this.num);

      var tmpNum = _parseFloat._parseFloat(this.num);

      if (this.disabled) {
        return false;
      }

      var n = type === 'up' ? num + _parseFloat._parseFloat(this.step) : num - _parseFloat._parseFloat(this.step);

      if (fixNum === 0) {
        tmpNum = _parseFloat._parseFloat(n).toFixed(fixNum);
      } else {
        tmpNum = n;
        tmpNum = washNumFn(tmpNum, this.precision);
      }

      if (tmpNum > this.max) {
        tmpNum = this.max;
      }

      if (tmpNum < this.min) {
        tmpNum = this.min;
      }

      this.num = tmpNum;
      this.num = "" + this.num;
      this.oldNum = this.num;
      this.$emit(type === 'up' ? 'plus' : 'minus', _parseFloat._parseFloat(this.num));
      this.$emit('change', _parseFloat._parseFloat(this.num));
      this.dispatch('bxs-form-item', 'form.item.validate');
      return true;
    }
  }
});

/* script */
var __vue_script__ = script;
/* template */

var __vue_render__ = function __vue_render__() {
  var _vm = this;

  var _h = _vm.$createElement;

  var _c = _vm._self._c || _h;

  return _c('div', {
    staticClass: "bx-stepper-control"
  }, [_c('div', {
    staticClass: "bx-stepper-minus",
    class: _vm.disabledMinusClass,
    on: {
      "click": function click($event) {
        _vm.changeNum('down');
      }
    }
  }), _c('input', {
    directives: [{
      name: "model",
      rawName: "v-model",
      value: _vm.num,
      expression: "num"
    }],
    staticClass: "bx-stepper-input",
    class: _vm.disableInputClass,
    attrs: {
      "type": "number",
      "disabled": _vm.disableInput || _vm.disabled
    },
    domProps: {
      "value": _vm.num
    },
    on: {
      "blur": _vm.onblurFn,
      "focus": _vm.onfocusFn,
      "input": function input($event) {
        if ($event.target.composing) {
          return;
        }

        _vm.num = $event.target.value;
      }
    }
  }), _c('div', {
    staticClass: "bx-stepper-plus",
    class: _vm.disabledAddClass,
    on: {
      "click": function click($event) {
        _vm.changeNum('up');
      }
    }
  })]);
};

var __vue_staticRenderFns__ = [];
/* style */

var __vue_inject_styles__ = undefined;
/* scoped */

var __vue_scope_id__ = undefined;
/* module identifier */

var __vue_module_identifier__ = undefined;
/* functional template */

var __vue_is_functional_template__ = false;
/* style inject */

/* style inject SSR */

var index = normalizeComponent.__vue_normalize__({
  render: __vue_render__,
  staticRenderFns: __vue_staticRenderFns__
}, __vue_inject_styles__, __vue_script__, __vue_scope_id__, __vue_is_functional_template__, __vue_module_identifier__, undefined, undefined);

exports.default = index;
