import "core-js/modules/es6.regexp.constructor";
import "core-js/modules/es6.regexp.replace";
import "core-js/modules/es6.array.find";
import "core-js/modules/web.dom.iterable";
import "core-js/modules/es6.number.constructor";
import _createClass from "@babel/runtime-corejs2/helpers/createClass";
import _inheritsLoose from "../../inheritsLoose";
import _defineProperty from "@babel/runtime-corejs2/helpers/defineProperty";
import Intact from 'intact-angular';
import template from './index.vdt';
import '../../styles/kpc.css';
import './index.css';
var numberReg = /^(-|\+)?\d+(\.(\d+)?)?$/;

var Spinner =
/*#__PURE__*/
function (_Intact) {
  _inheritsLoose(Spinner, _Intact);

  function Spinner() {
    return _Intact.apply(this, arguments) || this;
  }

  var _proto = Spinner.prototype;

  _proto.defaults = function defaults() {
    return {
      disabled: false,
      value: 0,
      max: Number.POSITIVE_INFINITY,
      min: Number.NEGATIVE_INFINITY,
      step: 1,
      size: 'default',
      vertical: false,
      precision: undefined,
      formatter: undefined,
      parser: undefined,
      prefix: undefined,
      suffix: undefined,
      width: undefined,
      _value: 0
    };
  };

  _proto._init = function _init() {
    var _this = this;

    // make sure the min/max/step is valid
    var defaults = this.defaults();
    ['min', 'max', 'step'].forEach(function (item) {
      _this.on("$receive:" + item, function (c, v) {
        if (typeof v !== 'number') {
          _this.set(item, defaults[item], {
            async: true
          });
        }
      });
    });
    this.on('$receive', function (c, keys) {
      if (['max', 'min', 'precision', 'value', 'formatter', 'parser', 'prefix', 'suffix'].find(function (key) {
        return ~keys.indexOf(key);
      })) {
        _this._fixValue();
      }
    });
  };

  _proto._fixValue = function _fixValue(value, fallbackValue) {
    if (value === void 0) {
      value = this.get('value');
    }

    if (fallbackValue === void 0) {
      fallbackValue = 0;
    }

    this.set(this._getFixedValue(value, fallbackValue));
  };

  _proto._getFixedValue = function _getFixedValue(value, fallbackValue) {
    if (value === void 0) {
      value = this.get('value');
    }

    if (fallbackValue === void 0) {
      fallbackValue = 0;
    }

    var _this$get = this.get(),
        precision = _this$get.precision,
        max = _this$get.max,
        min = _this$get.min;

    if (min > max) {
      Intact.utils.error(new Error("[Spinner] min must less than or equal to max, but got min: " + min + " max: " + max));
      return {
        _value: this._format(0),
        value: 0
      };
    }

    value = this._parse(value);
    var originValue = this.get('value');

    if (value == null || !numberReg.test(value)) {
      value = fallbackValue;
    }

    value = Number(value);

    if (value >= max) {
      value = max;
    } else if (value < min) {
      value = min;
    }

    var _value = value;

    if (precision != null) {
      _value = value.toFixed(precision);
      value = +_value;
    }

    _value = this._format(_value);
    return {
      _value: _value,
      value: value
    };
  };

  _proto._parse = function _parse(value) {
    var _this$get2 = this.get(),
        parser = _this$get2.parser,
        prefix = _this$get2.prefix,
        suffix = _this$get2.suffix;

    value = String(value);

    if (!parser) {
      if (prefix) {
        value = value.replace(new RegExp("^" + prefix), '');
      }

      if (suffix) {
        value = value.replace(new RegExp(suffix + "$"), '');
      }

      return value;
    }

    return parser(value);
  };

  _proto._format = function _format(value) {
    var _this$get3 = this.get(),
        formatter = _this$get3.formatter,
        prefix = _this$get3.prefix,
        suffix = _this$get3.suffix;

    if (!formatter) {
      return "" + (prefix || '') + value + (suffix || '');
    }

    return formatter(value);
  };

  _proto._increase = function _increase(e) {
    var _this$get4 = this.get(),
        value = _this$get4.value,
        step = _this$get4.step;

    this._fixValue(Number((+value + step).toFixed(10)));
  };

  _proto._decrease = function _decrease(e) {
    var _this$get5 = this.get(),
        value = _this$get5.value,
        step = _this$get5.step;

    this._fixValue(Number((+value - step).toFixed(10)));
  };

  _proto._disableDecrease = function _disableDecrease() {
    var _this$get6 = this.get(),
        value = _this$get6.value,
        min = _this$get6.min,
        step = _this$get6.step,
        disabled = _this$get6.disabled;

    return disabled || +value <= min || Number((min + step).toFixed(10)) > value;
  };

  _proto._disableIncrease = function _disableIncrease() {
    var _this$get7 = this.get(),
        value = _this$get7.value,
        max = _this$get7.max,
        step = _this$get7.step,
        disabled = _this$get7.disabled;

    return disabled || +value >= max || Number((max - step).toFixed(10)) < value;
  };

  _proto._changeValue = function _changeValue(e) {
    this._fixValue(e.target.value.trim(), this.get('value'));
  } // we need change value as long as the input is valid, #213
  ;

  _proto._onInput = function _onInput(e) {
    var val = e.target.value;

    var _this$_getFixedValue = this._getFixedValue(val.trim(), this.get('value')),
        value = _this$_getFixedValue.value;

    var data = {
      _value: val,
      value: value
    }; // if (_value === val) {
    // data.value = value;
    // }

    this.set(data);
  };

  _createClass(Spinner, [{
    key: "template",
    get: function get() {
      return template;
    }
  }]);

  return Spinner;
}(Intact);

_defineProperty(Spinner, "propTypes", {
  disabled: Boolean,
  value: [Number, String],
  max: Number,
  min: Number,
  step: Number,
  size: ['large', 'default', 'small', 'mini'],
  vertical: Boolean,
  precision: Number,
  formatter: Function,
  parser: Function,
  prefix: String,
  suffix: String,
  width: [String, Number]
});

export { Spinner as default };
export { Spinner };