/**
 * tdesign v0.32.0
 * (c) 2021 tdesign
 * @license MIT
 */

import { h as helper } from '../_chunks/dep-42d5c87e.js';
import _asyncToGenerator from '@babel/runtime/helpers/asyncToGenerator';
import _defineProperty from '@babel/runtime/helpers/defineProperty';
import _regeneratorRuntime from '@babel/runtime/regenerator';
import Vue from 'vue';
import { AddIcon, RemoveIcon, ChevronDownIcon, ChevronUpIcon } from 'tdesign-icons-vue';
import { emitEvent } from '../utils/event.js';
import { prefix } from '../config.js';
import { Button } from '../button/index.js';
import ClASSNAMES from '../utils/classnames.js';
import props from './props.js';
import '../utils/helper.js';
import '@babel/runtime/helpers/objectWithoutProperties';
import '@babel/runtime/helpers/slicedToArray';
import 'lodash/camelCase';
import '../button/button.js';
import '../loading/index.js';
import '../loading/loading.js';
import '../loading/icon/gradient.js';
import '../utils/set-style.js';
import '../utils/dom.js';
import 'raf';
import 'lodash/isString';
import '../utils/easing.js';
import '../utils/render-tnode.js';
import '@babel/runtime/helpers/typeof';
import '../utils/transfer-dom.js';
import '../loading/props.js';
import '../utils/withInstall.js';
import 'lodash/capitalize';
import './style/index.js';
import '../loading/type.js';
import '../loading/plugin.js';
import '../button/props.js';
import '../utils/ripple.js';
import '../button/type.js';

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(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(prefix, "-input-number");
var _InputNumber = Vue.extend({
  name: "TInputNumber",
  props: _objectSpread({}, props),
  components: {
    Add: AddIcon,
    Remove: RemoveIcon,
    ChevronDown: ChevronDownIcon,
    ChevronUp: ChevronUpIcon,
    TButton: Button
  },
  data: function data() {
    return {
      userInput: null,
      filterValue: null,
      isError: false,
      inputing: false
    };
  },
  computed: {
    disabledReduce: function disabledReduce() {
      return this.disabled || this.isError || Number(this.value) - this.step < this.min;
    },
    disabledAdd: function disabledAdd() {
      return this.disabled || 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({}, ClASSNAMES.STATUS.disabled, this.disabledReduce)]
      };
    },
    reduceEvents: function reduceEvents() {
      return {
        on: {
          click: this.handleReduce
        }
      };
    },
    addClasses: function addClasses() {
      return {
        "class": ["".concat(name, "__increase"), _defineProperty({}, ClASSNAMES.STATUS.disabled, this.disabledAdd)]
      };
    },
    addEvents: function addEvents() {
      return {
        on: {
          click: this.handleAdd
        }
      };
    },
    cmptWrapClasses: function cmptWrapClasses() {
      var _ref3;

      return {
        "class": ["t-input-number", ClASSNAMES.SIZE[this.size], (_ref3 = {}, _defineProperty(_ref3, ClASSNAMES.STATUS.disabled, this.disabled), _defineProperty(_ref3, "t-is-controls-right", this.theme === "column"), _defineProperty(_ref3, "t-input-number--normal", this.theme === "normal"), _ref3)]
      };
    },
    inputWrapProps: function inputWrapProps() {
      return {
        "class": ["t-input", {
          "t-is-error": this.isError
        }]
      };
    },
    inputClasses: function inputClasses() {
      var _ref4;

      return {
        "class": ["t-input__inner", (_ref4 = {}, _defineProperty(_ref4, ClASSNAMES.STATUS.disabled, this.disabled), _defineProperty(_ref4, "".concat(name, "-text-align"), this.theme === "row"), _ref4)]
      };
    },
    inputEvents: function inputEvents() {
      return {
        on: {
          input: this.handleInput,
          blur: this.handleBlur,
          focus: this.handleFocus,
          keydown: this.handleKeydown,
          keyup: this.handleKeyup,
          keypress: this.handleKeypress
        }
      };
    },
    inputAttrs: function inputAttrs() {
      return {
        attrs: {
          disabled: this.disabled,
          autocomplete: "off",
          ref: "refInputElem",
          placeholder: this.placeholder
        }
      };
    },
    displayValue: function displayValue() {
      if (this.inputing && this.userInput !== null) {
        return this.filterValue;
      }

      if (this.value === void 0) return "";
      return this.format && !this.inputing ? this.format(this.value) : this.value.toFixed(this.digitsNum);
    }
  },
  methods: {
    decreaseIcon: function decreaseIcon() {
      var h = this.$createElement;
      return this.theme === "column" ? h("chevron-down", {
        "attrs": {
          "size": this.size
        }
      }) : h("remove", {
        "attrs": {
          "size": this.size
        }
      });
    },
    increaseIcon: function increaseIcon() {
      var h = this.$createElement;
      return this.theme === "column" ? h("chevron-up", {
        "attrs": {
          "size": this.size
        }
      }) : h("add", {
        "attrs": {
          "size": this.size
        }
      });
    },
    handleAdd: function handleAdd(e) {
      if (this.disabledAdd) return;
      var value = this.value || 0;
      var factor = Math.pow(10, this.digitsNum);
      this.handleAction(Number(this.toDecimalPlaces((value * factor + this.step * factor) / factor).toFixed(this.digitsNum)), "add", e);
    },
    handleReduce: function handleReduce(e) {
      if (this.disabledReduce) return;
      var value = this.value || 0;
      var factor = Math.pow(10, this.digitsNum);
      this.handleAction(Number(this.toDecimalPlaces((value * factor - this.step * factor) / factor).toFixed(this.digitsNum)), "reduce", e);
    },
    handleInput: function handleInput(e) {
      this.userInput = e.target.value;
      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.substr(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);
      emitEvent(this, "change", value, ctx);
    },
    handleBlur: function handleBlur(e) {
      var _this = this;

      return _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee() {
        return _regeneratorRuntime.wrap(function _callee$(_context) {
          while (1) {
            switch (_context.prev = _context.next) {
              case 0:
                _context.next = 2;
                return _this.handleEndInput(e);

              case 2:
                _this.clearFilterValue();

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

              case 4:
              case "end":
                return _context.stop();
            }
          }
        }, _callee);
      }))();
    },
    handleFocus: function handleFocus(e) {
      this.handleStartInput();
      emitEvent(this, "focus", this.value, {
        e: e
      });
    },
    handleKeydownEnter: function handleKeydownEnter(e) {
      if (!["Enter", "NumpadEnter"].includes(e.code)) return;
      emitEvent(this, "enter", this.value, {
        e: e
      });
    },
    handleKeydown: function handleKeydown(e) {
      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
      };

      if (keyEvent[e.code] !== void 0) {
        keyEvent[e.code](e);
      }
    },
    handleKeyup: function handleKeyup(e) {
      emitEvent(this, "keyup", this.value, {
        e: e
      });
    },
    handleKeypress: function handleKeypress(e) {
      emitEvent(this, "keypress", this.value, {
        e: e
      });
    },
    handleStartInput: function handleStartInput() {
      this.inputing = true;
      if (this.value === void 0) return;
      this.filterValue = this.value.toFixed(this.digitsNum);
    },
    handleEndInput: function handleEndInput(e) {
      this.inputing = 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 h = arguments[0];
    return h("div", helper([{}, this.cmptWrapClasses]), [this.theme !== "normal" && h("t-button", helper([{}, this.reduceClasses, {}, this.reduceEvents, {
      "attrs": {
        "variant": "outline",
        "shape": "square",
        "icon": this.decreaseIcon
      }
    }])), h("div", helper([{}, this.inputWrapProps]), [h("input", helper([{
      "domProps": {
        "value": this.displayValue
      }
    }, this.inputClasses, {}, this.inputAttrs, {}, this.inputEvents]))]), this.theme !== "normal" && h("t-button", helper([{}, this.addClasses, {}, this.addEvents, {
      "attrs": {
        "variant": "outline",
        "shape": "square",
        "icon": this.increaseIcon
      }
    }]))]);
  }
});

export { _InputNumber as default };
//# sourceMappingURL=input-number.js.map
