"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

exports.__esModule = true;
exports.default = void 0;

var _create = _interopRequireDefault(require("../utils/create"));

var _utils = require("../utils");

var _unit = require("../utils/format/unit");

var _number = require("../utils/format/number");

var LONG_PRESS_START_TIME = 600;
var LONG_PRESS_INTERVAL = 200;

function equal(value1, value2) {
  return String(value1) === String(value2);
} // add num and avoid float number


function add(num1, num2) {
  var cardinal = Math.pow(10, 10);
  return Math.round((num1 + num2) * cardinal) / cardinal;
}

var _default = (0, _create.default)({
  render: function render() {
    var _vm = this;

    var _h = _vm.$createElement;

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

    return _c('div', {
      class: _vm.b([_vm.theme])
    }, [_c('div', {
      directives: [{
        name: "show",
        rawName: "v-show",
        value: _vm.showMinus,
        expression: "showMinus"
      }],
      ref: "minus",
      on: {
        "click": function click($event) {
          $event.stopPropagation();
          return _vm.onChange('minus');
        },
        "touchstart": function touchstart($event) {
          return _vm.onTouchStart('minus');
        },
        "touchend": _vm.onTouchEnd,
        "touchcancel": _vm.onTouchEnd
      }
    }, [_vm._t("left", [_c('button', {
      class: _vm.b('minus', {
        disabled: _vm.minusDisabled
      }),
      style: _vm.buttonStyle
    })], {
      "disabled": _vm.minusDisabled
    })], 2), _c('input', {
      ref: "input",
      class: _vm.b('input'),
      style: _vm.inputStyle,
      attrs: {
        "type": _vm.integer ? 'tel' : 'text',
        "role": "spinbutton",
        "disabled": _vm.disabled,
        "readonly": _vm.disableInput,
        "inputmode": _vm.integer ? 'numeric' : 'decimal',
        "aria-valuemax": _vm.max,
        "aria-valuemin": _vm.min,
        "aria-valuenow": _vm.currentValue
      },
      domProps: {
        "value": _vm.currentValue
      },
      on: {
        "input": _vm.onInput,
        "focus": _vm.onFocus,
        "blur": _vm.onBlur
      }
    }), _c('div', {
      directives: [{
        name: "show",
        rawName: "v-show",
        value: _vm.showPlus,
        expression: "showPlus"
      }],
      ref: "plus",
      on: {
        "click": function click($event) {
          $event.stopPropagation();
          return _vm.onChange('plus');
        },
        "touchstart": function touchstart($event) {
          return _vm.onTouchStart('plus');
        },
        "touchend": _vm.onTouchEnd,
        "touchcancel": _vm.onTouchEnd
      }
    }, [_vm._t("right", [_c('button', {
      class: _vm.b('plus', {
        disabled: _vm.plusDisabled
      }),
      style: _vm.buttonStyle
    })], {
      "disabled": _vm.plusDisabled
    })], 2)]);
  },
  name: "stepper",
  props: {
    value: null,
    theme: String,
    integer: Boolean,
    disabled: Boolean,
    inputWidth: [Number, String],
    buttonSize: [Number, String],
    asyncChange: Boolean,
    disablePlus: Boolean,
    disableMinus: Boolean,
    disableInput: Boolean,
    decimalLength: [Number, String],
    name: {
      type: [Number, String],
      default: ''
    },
    min: {
      type: [Number, String],
      default: 1
    },
    max: {
      type: [Number, String],
      default: Infinity
    },
    step: {
      type: [Number, String],
      default: 1
    },
    defaultValue: {
      type: [Number, String],
      default: 1
    },
    showPlus: {
      type: Boolean,
      default: true
    },
    showMinus: {
      type: Boolean,
      default: true
    },
    longPress: {
      type: Boolean,
      default: true
    }
  },
  data: function data() {
    var defaultValue = (0, _utils.isDef)(this.value) ? this.value : this.defaultValue;
    var value = this.format(defaultValue);

    if (!equal(value, this.value)) {
      this.$emit('input', value);
    }

    return {
      currentValue: value,
      height: ''
    };
  },
  computed: {
    minusDisabled: function minusDisabled() {
      return this.disabled || this.disableMinus || this.currentValue <= this.min;
    },
    plusDisabled: function plusDisabled() {
      return this.disabled || this.disablePlus || this.currentValue >= this.max;
    },
    inputStyle: function inputStyle() {
      var style = {};

      if (this.inputWidth) {
        style.width = (0, _unit.addUnit)(this.inputWidth);
      }

      if (this.height) {
        style.height = this.height;
      }

      return style;
    },
    buttonStyle: function buttonStyle() {
      if (this.buttonSize) {
        var size = (0, _unit.addUnit)(this.buttonSize);
        return {
          width: size,
          height: size
        };
      }
    }
  },
  watch: {
    max: 'check',
    min: 'check',
    integer: 'check',
    decimalLength: 'check',
    value: function value(val) {
      if (!equal(val, this.currentValue)) {
        this.currentValue = this.format(val);
      }
    },
    currentValue: function currentValue(val) {
      this.$emit('input', val);
      this.$emit('change', val, {
        name: this.name
      });
    }
  },
  methods: {
    check: function check() {
      var val = this.format(this.currentValue);

      if (!equal(val, this.currentValue)) {
        this.currentValue = val;
      }
    },
    // formatNumber illegal characters
    formatNumber: function formatNumber(value) {
      return (0, _number.formatNumber)(String(value), !this.integer);
    },
    format: function format(value) {
      value = this.formatNumber(value); // format range

      value = value === '' ? 0 : +value;
      value = isNaN(value) ? this.min : value;
      value = Math.max(Math.min(this.max, value), this.min); // format decimal

      if ((0, _utils.isDef)(this.decimalLength)) {
        value = value.toFixed(this.decimalLength);
      }

      return value;
    },
    onInput: function onInput(event) {
      var value = event.target.value;
      var formatted = this.formatNumber(value); // limit max decimal length

      if ((0, _utils.isDef)(this.decimalLength) && formatted.indexOf('.') !== -1) {
        var pair = formatted.split('.');
        formatted = pair[0] + "." + pair[1].slice(0, this.decimalLength);
      }

      if (!equal(value, formatted)) {
        event.target.value = formatted;
      }

      this.emitChange(formatted);
    },
    emitChange: function emitChange(value) {
      if (this.asyncChange) {
        this.$emit('input', value);
        this.$emit('change', value, {
          name: this.name
        });
      } else {
        this.currentValue = value;
      }
    },
    onChange: function onChange(type) {
      if (this[type + "Disabled"]) {
        this.$emit('overlimit', type);
        return;
      }

      var diff = type === 'minus' ? -this.step : +this.step;
      var value = this.format(add(+this.currentValue, diff));
      this.emitChange(value);
      this.$emit(type);
    },
    onFocus: function onFocus(event) {
      // readonly not work in lagacy mobile safari
      if (this.disableInput && this.$refs.input) {
        this.$refs.input.blur();
      } else {
        this.$emit('focus', event);
      }
    },
    onBlur: function onBlur(event) {
      var value = this.format(event.target.value);
      event.target.value = value;
      this.currentValue = value;
      this.$emit('blur', event);
    },
    longPressStep: function longPressStep(type) {
      var _this = this;

      this.longPressTimer = setTimeout(function () {
        _this.onChange(type);

        _this.longPressStep(type);
      }, LONG_PRESS_INTERVAL);
    },
    onTouchStart: function onTouchStart(type) {
      var _this2 = this;

      if (!this.longPress) {
        return;
      }

      clearTimeout(this.longPressTimer);
      this.isLongPress = false;
      this.longPressTimer = setTimeout(function () {
        _this2.isLongPress = true;

        _this2.onChange(type);

        _this2.longPressStep(type);
      }, LONG_PRESS_START_TIME);
    },
    onTouchEnd: function onTouchEnd() {
      if (!this.longPress) {
        return;
      }

      clearTimeout(this.longPressTimer);
    }
  },
  mounted: function mounted() {
    var plus = this.$refs.plus;
    var minus = this.$refs.minus;

    if (plus && minus) {
      var plusHeight = plus.clientHeight;
      var minusHeight = minus.clientHeight;

      if (plusHeight > minusHeight) {
        this.height = (0, _unit.addUnit)(plusHeight);
      } else {
        this.height = (0, _unit.addUnit)(minusHeight);
      }
    }
  }
});

exports.default = _default;