/**
 * tdesign v0.41.7
 * (c) 2022 tdesign
 * @license MIT
 */

'use strict';

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

var helper = require('../_chunks/dep-99705d51.js');
var _asyncToGenerator = require('@babel/runtime/helpers/asyncToGenerator');
var _defineProperty = require('@babel/runtime/helpers/defineProperty');
var _regeneratorRuntime = require('@babel/runtime/regenerator');
var Vue = require('vue');
var tdesignIconsVue = require('tdesign-icons-vue');
var button_index = require('../button/index.js');
var input_index = require('../input/index.js');
var utils_event = require('../utils/event.js');
var config = require('../config.js');
var utils_classnames = require('../utils/classnames.js');
var inputNumber_props = require('./props.js');
require('../button/button.js');
require('../loading/index.js');
require('../loading/loading.js');
require('../loading/icon/gradient.js');
require('../_common/js/loading/circle-adapter.js');
require('../_common/js/utils/set-style.js');
require('../_common/js/utils/helper.js');
require('@babel/runtime/helpers/objectWithoutProperties');
require('@babel/runtime/helpers/slicedToArray');
require('../utils/dom.js');
require('raf');
require('lodash/isString');
require('../utils/easing.js');
require('../utils/render-tnode.js');
require('@babel/runtime/helpers/typeof');
require('../utils/transfer-dom.js');
require('../loading/props.js');
require('../utils/withInstall.js');
require('lodash/capitalize');
require('../loading/plugin.js');
require('../button/props.js');
require('../utils/ripple.js');
require('../config-provider/config-receiver.js');
require('lodash/mergeWith');
require('../config-provider/context.js');
require('lodash/merge');
require('../_common/js/global-config/default-config.js');
require('../_common/js/global-config/locale/zh_CN.js');
require('../utils/mixins.js');
require('../input/addon.js');
require('../input/input.js');
require('@babel/runtime/helpers/toConsumableArray');
require('lodash/camelCase');
require('lodash/kebabCase');
require('../utils/helper.js');
require('../input/props.js');
require('../input/input-group.js');
require('../utils/map-props.js');

function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }

var _asyncToGenerator__default = /*#__PURE__*/_interopDefaultLegacy(_asyncToGenerator);
var _defineProperty__default = /*#__PURE__*/_interopDefaultLegacy(_defineProperty);
var _regeneratorRuntime__default = /*#__PURE__*/_interopDefaultLegacy(_regeneratorRuntime);
var Vue__default = /*#__PURE__*/_interopDefaultLegacy(Vue);

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty__default["default"](target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
var name = "".concat(config.prefix, "-input-number");
var _InputNumber = Vue__default["default"].extend({
  name: "TInputNumber",
  props: _objectSpread({}, inputNumber_props["default"]),
  components: {
    AddIcon: tdesignIconsVue.AddIcon,
    RemoveIcon: tdesignIconsVue.RemoveIcon,
    ChevronDownIcon: tdesignIconsVue.ChevronDownIcon,
    ChevronUpIcon: tdesignIconsVue.ChevronUpIcon,
    TButton: button_index.Button,
    TInput: input_index.Input
  },
  data: function data() {
    return {
      formDisabled: void 0,
      userInput: null,
      filterValue: null,
      isError: false,
      inputting: false
    };
  },
  computed: {
    tDisabled: function tDisabled() {
      return this.formDisabled || this.disabled;
    },
    disabledReduce: function disabledReduce() {
      return this.tDisabled || this.isError || Number(this.value) - this.step < this.min;
    },
    disabledAdd: function disabledAdd() {
      return this.tDisabled || this.isError || Number(this.value) + this.step > this.max;
    },
    valueDecimalPlaces: function valueDecimalPlaces() {
      var tempVal = this.filterValue !== null && !isNaN(Number(this.filterValue)) && !isNaN(parseFloat(this.filterValue)) ? this.filterValue : String(this.value);
      var tempIndex = tempVal.indexOf(".") + 1;
      return tempIndex > 0 ? tempVal.length - tempIndex : 0;
    },
    stepDecimalPlaces: function stepDecimalPlaces() {
      var tempVal = String(this.step);
      var tempIndex = tempVal.indexOf(".") + 1;
      return tempIndex > 0 ? tempVal.length - tempIndex : 0;
    },
    digitsNum: function digitsNum() {
      if (this.decimalPlaces !== void 0) {
        if (this.decimalPlaces < this.stepDecimalPlaces) {
          console.warn("decimal places of step should be less than decimal-places");
        }

        return this.decimalPlaces;
      }

      return this.valueDecimalPlaces > this.stepDecimalPlaces ? this.valueDecimalPlaces : this.stepDecimalPlaces;
    },
    reduceClasses: function reduceClasses() {
      return {
        "class": ["".concat(name, "__decrease"), _defineProperty__default["default"]({}, utils_classnames["default"].STATUS.disabled, this.disabledReduce)]
      };
    },
    reduceEvents: function reduceEvents() {
      return {
        on: {
          click: this.handleReduce
        }
      };
    },
    addClasses: function addClasses() {
      return {
        "class": ["".concat(name, "__increase"), _defineProperty__default["default"]({}, utils_classnames["default"].STATUS.disabled, this.disabledAdd)]
      };
    },
    addEvents: function addEvents() {
      return {
        on: {
          click: this.handleAdd
        }
      };
    },
    cmptWrapClasses: function cmptWrapClasses() {
      var _ref3;

      return {
        "class": [name, utils_classnames["default"].SIZE[this.size], (_ref3 = {}, _defineProperty__default["default"](_ref3, utils_classnames["default"].STATUS.disabled, this.tDisabled), _defineProperty__default["default"](_ref3, "".concat(config.prefix, "-is-controls-right"), this.theme === "column"), _defineProperty__default["default"](_ref3, "".concat(name, "--").concat(this.theme), this.theme), _defineProperty__default["default"](_ref3, "".concat(name, "--auto-width"), this.autoWidth), _ref3)]
      };
    },
    inputEvents: function inputEvents() {
      return {
        on: {
          blur: this.handleBlur,
          focus: this.handleFocus,
          keydown: this.handleKeydown,
          keyup: this.handleKeyup,
          keypress: this.handleKeypress
        }
      };
    },
    inputAttrs: function inputAttrs() {
      return {
        attrs: {
          disabled: this.tDisabled,
          readonly: this.readonly,
          autocomplete: "off",
          ref: "refInputElem",
          placeholder: this.placeholder,
          unselectable: this.readonly ? "on" : "off",
          tips: this.tips,
          autoWidth: this.autoWidth,
          align: this.align || (this.theme === "row" ? "center" : void 0),
          status: this.isError ? "error" : this.status
        }
      };
    },
    displayValue: function displayValue() {
      if (this.inputting && this.userInput !== null) {
        return this.filterValue;
      }

      if (this.value === void 0 || this.value === null) return "";
      return this.format && !this.inputting ? this.format(this.value) : this.value.toFixed(this.digitsNum);
    }
  },
  methods: {
    decreaseIcon: function decreaseIcon() {
      var h = this.$createElement;
      return this.theme === "column" ? h("chevron-down-icon", {
        "attrs": {
          "size": this.size
        }
      }) : h("remove-icon", {
        "attrs": {
          "size": this.size
        }
      });
    },
    increaseIcon: function increaseIcon() {
      var h = this.$createElement;
      return this.theme === "column" ? h("chevron-up-icon", {
        "attrs": {
          "size": this.size
        }
      }) : h("add-icon", {
        "attrs": {
          "size": this.size
        }
      });
    },
    handleAdd: function handleAdd(e) {
      if (this.disabledAdd || this.readonly) return;
      this.handleAction(this.getClickValue("add"), "add", e);
    },
    handleReduce: function handleReduce(e) {
      if (this.disabledReduce || this.readonly) return;
      this.handleAction(this.getClickValue("reduce"), "reduce", e);
    },
    getClickValue: function getClickValue(op) {
      var value = this.value || 0;
      var addOrReduce = {
        add: 1,
        reduce: -1
      }[op];
      var clickVal = this.toDecimalPlaces(value + addOrReduce * this.step);

      if (this.value === void 0) {
        clickVal = Math.min(Math.max(clickVal, this.min), this.max);
      }

      return Number(clickVal.toFixed(this.digitsNum));
    },
    handleInput: function handleInput(val, e) {
      this.userInput = val;
      this.filterValue = this.toValidStringNumber(this.userInput);
      this.userInput = "";
      if (!this.isValid(this.filterValue) || Number(this.filterValue) === this.value) return;
      this.updateValue(Number(this.filterValue));
      this.handleAction(Number(this.filterValue), "input", e);
    },
    handleAction: function handleAction(value, actionType, e) {
      if (actionType !== "input") {
        this.clearInput();
      }

      this.handleChange(value, {
        type: actionType,
        e: e
      });
    },
    toValidStringNumber: function toValidStringNumber(s) {
      var filterVal = s.replace(/[^\d.eE。-]/g, "").replace("\u3002", ".");

      if (this.multiE(filterVal) || this.multiDot(filterVal) || this.multiNegative(filterVal)) {
        filterVal = filterVal.substring(0, filterVal.length - 1);
      }

      return filterVal;
    },
    toValidNumber: function toValidNumber(s) {
      if (s === "") return void 0;
      var val = Number(s);
      if (isNaN(val) || isNaN(parseFloat(s))) return this.value;
      if (val > this.max) return this.max;
      if (val < this.min) return this.min;
      return parseFloat(s);
    },
    handleChange: function handleChange(value, ctx) {
      this.updateValue(value);
      utils_event.emitEvent(this, "change", value, ctx);
    },
    handleBlur: function handleBlur(e) {
      var _this = this;

      return _asyncToGenerator__default["default"]( /*#__PURE__*/_regeneratorRuntime__default["default"].mark(function _callee() {
        return _regeneratorRuntime__default["default"].wrap(function _callee$(_context) {
          while (1) {
            switch (_context.prev = _context.next) {
              case 0:
                _context.next = 2;
                return _this.handleEndInput(e);

              case 2:
                _this.clearFilterValue();

                utils_event.emitEvent(_this, "blur", _this.value, {
                  e: e
                });

              case 4:
              case "end":
                return _context.stop();
            }
          }
        }, _callee);
      }))();
    },
    handleFocus: function handleFocus(e) {
      this.handleStartInput();
      utils_event.emitEvent(this, "focus", this.value, {
        e: e
      });
    },
    handleKeydownEnter: function handleKeydownEnter(e) {
      if (!["Enter", "NumpadEnter"].includes(e.code || e.key)) return;
      utils_event.emitEvent(this, "enter", this.value, {
        e: e
      });
    },
    handleKeydown: function handleKeydown(e) {
      utils_event.emitEvent(this, "keydown", this.value, {
        e: e
      });
      this.handleKey(e);
    },
    handleKey: function handleKey(e) {
      var keyEvent = {
        ArrowUp: this.handleAdd,
        ArrowDown: this.handleReduce,
        Enter: this.handleKeydownEnter,
        NumpadEnter: this.handleKeydownEnter
      };
      var code = e.code || e.key;

      if (keyEvent[code] !== void 0) {
        keyEvent[code](e);
      }
    },
    handleKeyup: function handleKeyup(e) {
      utils_event.emitEvent(this, "keyup", this.value, {
        e: e
      });
    },
    handleKeypress: function handleKeypress(e) {
      utils_event.emitEvent(this, "keypress", this.value, {
        e: e
      });
    },
    handleStartInput: function handleStartInput() {
      this.inputting = true;
      if (this.value === void 0 || this.value === null) return;
      this.filterValue = this.value.toFixed(this.digitsNum);
    },
    handleEndInput: function handleEndInput(e) {
      this.inputting = false;
      var value = this.toValidNumber(this.filterValue);

      if (value !== void 0) {
        value = this.toDecimalPlaces(value);
      }

      if (value !== this.value) {
        this.updateValue(value);
        this.handleAction(value, "input", e);
      }

      this.isError = false;
    },
    updateValue: function updateValue(v) {
      this.$emit("input", v);
    },
    handleInputError: function handleInputError(visible) {
      this.isError = visible;
    },
    isValid: function isValid(v) {
      var numV = Number(v);

      if (this.empty(v) || isNaN(numV)) {
        this.handleInputError(true);
        return false;
      }

      return this.isValidNumber(numV);
    },
    isValidNumber: function isValidNumber(v) {
      if (v > this.max) {
        this.handleInputError(true);
        return false;
      }

      if (v < this.min) {
        this.handleInputError(true);
        return false;
      }

      this.isError = false;
      return true;
    },
    empty: function empty(v) {
      return !v && !v.replace(" ", "");
    },
    clearInput: function clearInput() {
      this.userInput = null;
    },
    clearFilterValue: function clearFilterValue() {
      this.filterValue = "";
    },
    multiE: function multiE(s) {
      var m = s.match(/[e]/gi);
      return m === null ? false : m.length > 1;
    },
    multiDot: function multiDot(s) {
      var m = s.match(/[.]/g);
      return m === null ? false : m.length > 1;
    },
    multiNegative: function multiNegative(s) {
      var m = s.match(/[-]/g);
      return m === null ? false : m.length > 2;
    },
    toDecimalPlaces: function toDecimalPlaces(value) {
      var decimalPlaces = this.decimalPlaces === void 0 ? this.digitsNum : this.decimalPlaces;
      var factor = Math.pow(10, decimalPlaces);
      return Math.round(value * factor) / factor;
    }
  },
  watch: {
    value: {
      immediate: true,
      handler: function handler(v) {
        if (v !== void 0) {
          this.isValidNumber(v);
        }
      }
    }
  },
  render: function render() {
    var _this2 = this;

    var h = arguments[0];
    return h("div", helper.helper([{}, this.cmptWrapClasses]), [this.theme !== "normal" && h("t-button", helper.helper([{}, this.reduceClasses, {}, this.reduceEvents, {
      "attrs": {
        "variant": "outline",
        "shape": "square",
        "icon": this.decreaseIcon
      }
    }])), h("t-input", helper.helper([{}, this.inputAttrs, {}, this.inputEvents, {}, {
      "props": this.inputProps
    }, {
      "attrs": {
        "value": this.displayValue
      },
      "on": {
        "change": function change(val, _ref4) {
          var e = _ref4.e;
          return _this2.handleInput(val, e);
        }
      }
    }])), this.theme !== "normal" && h("t-button", helper.helper([{}, this.addClasses, {}, this.addEvents, {
      "attrs": {
        "variant": "outline",
        "shape": "square",
        "icon": this.increaseIcon
      }
    }]))]);
  }
});

exports["default"] = _InputNumber;
//# sourceMappingURL=input-number.js.map
