"use strict";
const uni_modules_tdesignUniapp_components_common_src_instantiationDecorator = require("../common/src/instantiationDecorator.js");
const uni_modules_tdesignUniapp_components_common_config = require("../common/config.js");
const uni_modules_tdesignUniapp_components_common_utils = require("../common/utils.js");
const uni_modules_tdesignUniapp_components_stepper_props = require("./props.js");
const uni_modules_tdesignUniapp_components_common_utils_wxs = require("../common/utils.wxs.js");
const common_vendor = require("../../../../common/vendor.js");
const TIcon = () => "../icon/icon.js";
const name = `${uni_modules_tdesignUniapp_components_common_config.prefix}-stepper`;
const _sfc_main = uni_modules_tdesignUniapp_components_common_src_instantiationDecorator.uniComponent({
  name,
  options: {
    styleIsolation: "shared"
  },
  controlledProps: [
    {
      key: "value",
      event: "change"
    }
  ],
  externalClasses: [
    `${uni_modules_tdesignUniapp_components_common_config.prefix}-class`,
    `${uni_modules_tdesignUniapp_components_common_config.prefix}-class-input`,
    `${uni_modules_tdesignUniapp_components_common_config.prefix}-class-minus`,
    `${uni_modules_tdesignUniapp_components_common_config.prefix}-class-plus`
  ],
  components: {
    TIcon
  },
  props: {
    ...uni_modules_tdesignUniapp_components_stepper_props.props
  },
  data() {
    return {
      currentValue: 0,
      classPrefix: name,
      prefix: uni_modules_tdesignUniapp_components_common_config.prefix,
      tools: uni_modules_tdesignUniapp_components_common_utils_wxs.tools,
      disablePlus: false,
      disableMinus: false
    };
  },
  watch: {
    value(v) {
      this.preValue = Number(v);
      this.updateCurrentValue(this.format(this.preValue));
    }
  },
  mounted() {
    const { value, defaultValue, min } = this;
    const cur = uni_modules_tdesignUniapp_components_common_utils.coalesce(value, defaultValue);
    this.updateCurrentValue(cur ? Number(cur) : min);
  },
  methods: {
    isDisabled(type) {
      const { min, max, disabled } = this;
      const { currentValue } = this;
      if (disabled) {
        return true;
      }
      if (type === "minus" && currentValue <= min) {
        return true;
      }
      if (type === "plus" && currentValue >= max) {
        return true;
      }
      return false;
    },
    getLen(num) {
      const numStr = num.toString();
      return numStr.indexOf(".") === -1 ? 0 : numStr.split(".")[1].length;
    },
    add(a, b) {
      const maxLen = Math.max(this.getLen(a), this.getLen(b));
      const base = 10 ** maxLen;
      return Math.round(a * base + b * base) / base;
    },
    format(value) {
      const { min, max, step } = this;
      const len = Math.max(this.getLen(step), this.getLen(value));
      return Math.max(Math.min(max, value, Number.MAX_SAFE_INTEGER), min, Number.MIN_SAFE_INTEGER).toFixed(len);
    },
    setValue(value) {
      const newValue = Number(this.format(value));
      this.updateCurrentValue(newValue);
      if (this.preValue === newValue)
        return;
      this.preValue = newValue;
      this._trigger("change", { value: newValue });
    },
    minusValue() {
      if (this.isDisabled("minus")) {
        this.$emit("overlimit", { type: "minus" });
        return false;
      }
      const { currentValue, step } = this;
      this.setValue(this.add(currentValue, -step));
    },
    plusValue() {
      if (this.isDisabled("plus")) {
        this.$emit("overlimit", { type: "plus" });
        return false;
      }
      const { currentValue, step } = this;
      this.setValue(this.add(currentValue, step));
    },
    filterIllegalChar(value) {
      const v = String(value).replace(/[^0-9.]/g, "");
      const indexOfDot = v.indexOf(".");
      if (this.integer && indexOfDot !== -1) {
        return v.split(".")[0];
      }
      if (!this.integer && indexOfDot !== -1 && indexOfDot !== v.lastIndexOf(".")) {
        return v.split(".", 2).join(".");
      }
      return v;
    },
    updateCurrentValue(value) {
      this.currentValue = value;
    },
    handleFocus(e) {
      const { value } = e.detail;
      this.$emit("focus", { value });
    },
    handleInput(e) {
      const { value } = e.detail;
      if (value === "") {
        return;
      }
      const formatted = this.filterIllegalChar(value);
      const newValue = this.format(formatted);
      this.updateCurrentValue(this.integer ? newValue : formatted);
      if (this.integer || /\.\d+/.test(formatted)) {
        this.setValue(formatted);
      }
    },
    handleBlur(e) {
      const { value: rawValue } = e.detail;
      const value = this.format(rawValue);
      this.setValue(value);
      this.$emit("blur", { value });
    }
  }
});
if (!Array) {
  const _easycom_t_icon2 = common_vendor.resolveComponent("t-icon");
  _easycom_t_icon2();
}
const _easycom_t_icon = () => "../icon/icon.js";
if (!Math) {
  _easycom_t_icon();
}
function _sfc_render(_ctx, _cache, $props, $setup, $data, $options) {
  return {
    a: common_vendor.p({
      name: "remove"
    }),
    b: common_vendor.n(_ctx.classPrefix + "__minus " + _ctx.classPrefix + "__minus--" + _ctx.theme + " " + _ctx.classPrefix + "__icon--" + _ctx.size + " " + (_ctx.disabled || _ctx.disableMinus || _ctx.currentValue <= _ctx.min ? _ctx.classPrefix + "--" + _ctx.theme + "-disabled" : "") + " " + _ctx.tClassMinus),
    c: "减少" + _ctx.step,
    d: _ctx.disabled || _ctx.disableMinus || _ctx.currentValue <= _ctx.min,
    e: common_vendor.o((...args) => _ctx.minusValue && _ctx.minusValue(...args)),
    f: common_vendor.s(_ctx.inputWidth ? "width:" + _ctx.inputWidth + "px;" : ""),
    g: common_vendor.n(_ctx.classPrefix + "__input " + _ctx.classPrefix + "__input--" + _ctx.size + " " + _ctx.tClassInput),
    h: _ctx.disabled || _ctx.disableInput,
    i: _ctx.integer ? "number" : "digit",
    j: _ctx.currentValue,
    k: common_vendor.o((...args) => _ctx.handleInput && _ctx.handleInput(...args)),
    l: common_vendor.o((...args) => _ctx.handleFocus && _ctx.handleFocus(...args)),
    m: common_vendor.o((...args) => _ctx.handleBlur && _ctx.handleBlur(...args)),
    n: common_vendor.n(_ctx.classPrefix + "__input--" + _ctx.theme + " " + (_ctx.disabled || _ctx.disableInput ? _ctx.classPrefix + "--" + _ctx.theme + "-disabled" : "")),
    o: common_vendor.p({
      name: "add"
    }),
    p: common_vendor.n(_ctx.classPrefix + "__plus " + _ctx.classPrefix + "__plus--" + _ctx.theme + " " + _ctx.classPrefix + "__icon--" + _ctx.size + " " + (_ctx.disabled || _ctx.disablePlus || _ctx.currentValue >= _ctx.max ? _ctx.classPrefix + "--" + _ctx.theme + "-disabled" : "") + " " + _ctx.tClassPlus),
    q: "增加" + _ctx.step,
    r: _ctx.disabled || _ctx.disablePlus || _ctx.currentValue >= _ctx.max,
    s: common_vendor.o((...args) => _ctx.plusValue && _ctx.plusValue(...args)),
    t: common_vendor.s(_ctx.tools._style([_ctx.customStyle])),
    v: common_vendor.n(_ctx.classPrefix + " " + _ctx.classPrefix + "--" + _ctx.size + " " + _ctx.tClass)
  };
}
const Component = /* @__PURE__ */ common_vendor._export_sfc(_sfc_main, [["render", _sfc_render], ["__scopeId", "data-v-5e12fbd0"]]);
wx.createComponent(Component);
//# sourceMappingURL=../../../../../.sourcemap/mp-weixin/uni_modules/tdesign-uniapp/components/stepper/stepper.js.map
