"use strict";
const common_vendor = require("../../../../common/vendor.js");
const uni_modules_wotDesignUni_components_common_util = require("../common/util.js");
const uni_modules_wotDesignUni_components_wdInputNumber_types = require("./types.js");
const uni_modules_wotDesignUni_components_common_interceptor = require("../common/interceptor.js");
if (!Math) {
  wdIcon();
}
const wdIcon = () => "../wd-icon/wd-icon.js";
const __default__ = {
  name: "wd-input-number",
  options: {
    virtualHost: true,
    addGlobalClass: true,
    styleIsolation: "shared"
  }
};
const _sfc_main = /* @__PURE__ */ common_vendor.defineComponent({
  ...__default__,
  props: uni_modules_wotDesignUni_components_wdInputNumber_types.inputNumberProps,
  emits: ["change", "focus", "blur", "update:modelValue"],
  setup(__props, { emit: __emit }) {
    const props = __props;
    const emit = __emit;
    const inputValue = common_vendor.ref(getInitValue());
    let longPressTimer = null;
    const minDisabled = common_vendor.computed(() => {
      const val = toNumber(inputValue.value);
      return props.disabled || val <= props.min || addStep(val, -props.step) < props.min;
    });
    const maxDisabled = common_vendor.computed(() => {
      const val = toNumber(inputValue.value);
      return props.disabled || val >= props.max || addStep(val, props.step) > props.max;
    });
    common_vendor.watch(
      () => props.modelValue,
      (val) => {
        inputValue.value = formatValue(val);
      }
    );
    common_vendor.watch([() => props.max, () => props.min, () => props.precision], () => {
      const val = toNumber(inputValue.value);
      inputValue.value = formatValue(val);
    });
    function getInitValue() {
      if (!props.updateOnInit) {
        return formatDisplay(props.modelValue);
      }
      const formatted = formatValue(props.modelValue);
      if (!uni_modules_wotDesignUni_components_common_util.isEqual(String(formatted), String(props.modelValue))) {
        emit("update:modelValue", formatted);
      }
      return formatted;
    }
    function getPrecision(val) {
      if (!uni_modules_wotDesignUni_components_common_util.isDef(val))
        return 0;
      const str = val.toString();
      const dotIndex = str.indexOf(".");
      return dotIndex === -1 ? 0 : str.length - dotIndex - 1;
    }
    function toPrecision(val) {
      const precision = Number(props.precision);
      return Math.round(val * Math.pow(10, precision)) / Math.pow(10, precision);
    }
    function toNumber(val) {
      if (props.allowNull && (!uni_modules_wotDesignUni_components_common_util.isDef(val) || val === "")) {
        return NaN;
      }
      if (!uni_modules_wotDesignUni_components_common_util.isDef(val) || val === "") {
        return props.min;
      }
      let str = String(val);
      if (str.endsWith("."))
        str = str.slice(0, -1);
      if (str.startsWith("."))
        str = "0" + str;
      if (str.startsWith("-."))
        str = "-0" + str.substring(1);
      if (str === "-" || str === "")
        return props.min;
      let num = Number(str);
      if (isNaN(num))
        num = props.min;
      return normalizeValue(num);
    }
    function normalizeValue(val) {
      let result = val;
      if (props.stepStrictly) {
        const stepPrecision = getPrecision(props.step);
        const factor = Math.pow(10, stepPrecision);
        result = Math.round(result / props.step) * factor * props.step / factor;
      }
      if (props.stepStrictly) {
        result = applyStrictBounds(result, props.min, props.max);
      } else {
        result = Math.min(Math.max(result, props.min), props.max);
      }
      if (uni_modules_wotDesignUni_components_common_util.isDef(props.precision)) {
        result = toPrecision(result);
      }
      return result;
    }
    function applyStrictBounds(val, min, max) {
      if (val >= min && val <= max)
        return val;
      const stepPrecision = getPrecision(props.step);
      const factor = Math.pow(10, stepPrecision);
      if (val < min) {
        const minSteps = Math.ceil(min * factor / (props.step * factor));
        const candidate = toPrecision(minSteps * props.step * factor / factor);
        if (candidate > max) {
          const maxSteps = Math.floor(max * factor / (props.step * factor));
          return toPrecision(maxSteps * props.step * factor / factor);
        }
        return candidate;
      }
      if (val > max) {
        const maxSteps = Math.floor(max * factor / (props.step * factor));
        const candidate = toPrecision(maxSteps * props.step * factor / factor);
        if (candidate < min) {
          const minSteps = Math.ceil(min * factor / (props.step * factor));
          return toPrecision(minSteps * props.step * factor / factor);
        }
        return candidate;
      }
      return val;
    }
    function formatValue(val) {
      if (props.allowNull && (!uni_modules_wotDesignUni_components_common_util.isDef(val) || val === "")) {
        return "";
      }
      const num = toNumber(val);
      const precision = Number(props.precision);
      if (!uni_modules_wotDesignUni_components_common_util.isDef(props.precision)) {
        return num;
      }
      return precision === 0 ? Number(num.toFixed(0)) : num.toFixed(precision);
    }
    function formatDisplay(val) {
      if (props.allowNull && (!uni_modules_wotDesignUni_components_common_util.isDef(val) || val === "")) {
        return "";
      }
      if (!uni_modules_wotDesignUni_components_common_util.isDef(val) || val === "") {
        return props.min;
      }
      let num = Number(val);
      if (isNaN(num)) {
        return props.min;
      }
      const precision = Number(props.precision);
      if (!uni_modules_wotDesignUni_components_common_util.isDef(props.precision)) {
        return num;
      }
      return precision === 0 ? Number(num.toFixed(0)) : num.toFixed(precision);
    }
    function isIntermediate(val) {
      if (!val)
        return false;
      const str = String(val);
      return str.endsWith(".") || str.startsWith(".") || str.startsWith("-.") || str === "-" || Number(props.precision) > 0 && str.indexOf(".") === -1;
    }
    function cleanInput(val) {
      if (!val)
        return "";
      let cleaned = val.replace(/[^\d.-]/g, "");
      const hasNegative = cleaned.startsWith("-");
      cleaned = cleaned.replace(/-/g, "");
      if (hasNegative)
        cleaned = "-" + cleaned;
      const precision = Number(props.precision);
      if (precision > 0) {
        const parts = cleaned.split(".");
        if (parts.length > 2) {
          cleaned = parts[0] + "." + parts.slice(1).join("");
        }
      } else {
        cleaned = cleaned.split(".")[0];
      }
      if (cleaned.startsWith("."))
        return "0" + cleaned;
      if (cleaned.startsWith("-."))
        return "-0" + cleaned.substring(1);
      return cleaned;
    }
    function updateValue(val) {
      if (props.allowNull && (!uni_modules_wotDesignUni_components_common_util.isDef(val) || val === "")) {
        if (uni_modules_wotDesignUni_components_common_util.isEqual("", String(props.modelValue))) {
          inputValue.value = "";
          return;
        }
        const doUpdate2 = () => {
          inputValue.value = "";
          emit("update:modelValue", "");
          emit("change", { value: "" });
        };
        uni_modules_wotDesignUni_components_common_interceptor.callInterceptor(props.beforeChange, { args: [""], done: doUpdate2 });
        return;
      }
      const num = toNumber(val);
      const display = formatValue(val);
      if (uni_modules_wotDesignUni_components_common_util.isEqual(String(num), String(props.modelValue))) {
        inputValue.value = display;
        return;
      }
      const doUpdate = () => {
        inputValue.value = display;
        emit("update:modelValue", num);
        emit("change", { value: num });
      };
      uni_modules_wotDesignUni_components_common_interceptor.callInterceptor(props.beforeChange, { args: [num], done: doUpdate });
    }
    function addStep(val, step) {
      const num = Number(val);
      if (isNaN(num))
        return normalizeValue(props.min);
      const precision = Math.max(getPrecision(num), getPrecision(step));
      const factor = Math.pow(10, precision);
      const result = (num * factor + step * factor) / factor;
      return normalizeValue(result);
    }
    function handleClick(type) {
      const step = type === "add" ? props.step : -props.step;
      if (step < 0 && (minDisabled.value || props.disableMinus) || step > 0 && (maxDisabled.value || props.disablePlus))
        return;
      const newVal = addStep(inputValue.value, step);
      updateValue(newVal);
    }
    function handleInput(event) {
      const rawVal = event.detail.value || "";
      inputValue.value = rawVal;
      common_vendor.nextTick$1(() => {
        if (rawVal === "") {
          inputValue.value = "";
          if (props.immediateChange && props.allowNull) {
            updateValue("");
          }
          return;
        }
        const cleaned = cleanInput(rawVal);
        if (Number(props.precision) > 0 && isIntermediate(cleaned)) {
          inputValue.value = cleaned;
          return;
        }
        inputValue.value = cleaned;
        if (props.immediateChange) {
          updateValue(cleaned);
        }
      });
    }
    function handleBlur(event) {
      const val = event.detail.value || "";
      updateValue(val);
      emit("blur", { value: val });
    }
    function handleFocus(event) {
      emit("focus", event.detail);
    }
    function longPressStep(type) {
      clearLongPressTimer();
      longPressTimer = setTimeout(() => {
        handleClick(type);
        longPressStep(type);
      }, 250);
    }
    function handleTouchStart(type) {
      if (!props.longPress)
        return;
      clearLongPressTimer();
      longPressTimer = setTimeout(() => {
        handleClick(type);
        longPressStep(type);
      }, 600);
    }
    function handleTouchEnd() {
      if (!props.longPress)
        return;
      clearLongPressTimer();
    }
    function clearLongPressTimer() {
      if (longPressTimer) {
        clearTimeout(longPressTimer);
        longPressTimer = null;
      }
    }
    return (_ctx, _cache) => {
      return common_vendor.e({
        a: common_vendor.p({
          name: "decrease",
          ["custom-class"]: "wd-input-number__action-icon"
        }),
        b: common_vendor.n(`wd-input-number__action ${minDisabled.value || _ctx.disableMinus ? "is-disabled" : ""}`),
        c: common_vendor.o(($event) => handleClick("sub")),
        d: common_vendor.o(($event) => handleTouchStart("sub")),
        e: common_vendor.o(handleTouchEnd),
        f: !_ctx.withoutInput
      }, !_ctx.withoutInput ? {
        g: common_vendor.s(`${_ctx.inputWidth ? "width: " + _ctx.inputWidth : ""}`),
        h: _ctx.inputType,
        i: _ctx.precision ? "decimal" : "numeric",
        j: _ctx.disabled || _ctx.disableInput,
        k: String(inputValue.value),
        l: _ctx.placeholder,
        m: _ctx.adjustPosition,
        n: common_vendor.o(handleInput),
        o: common_vendor.o(handleFocus),
        p: common_vendor.o(handleBlur),
        q: common_vendor.o(() => {
        })
      } : {}, {
        r: common_vendor.p({
          name: "add",
          ["custom-class"]: "wd-input-number__action-icon"
        }),
        s: common_vendor.n(`wd-input-number__action ${maxDisabled.value || _ctx.disablePlus ? "is-disabled" : ""}`),
        t: common_vendor.o(($event) => handleClick("add")),
        v: common_vendor.o(($event) => handleTouchStart("add")),
        w: common_vendor.o(handleTouchEnd),
        x: common_vendor.n(`wd-input-number ${_ctx.customClass} ${_ctx.disabled ? "is-disabled" : ""} ${_ctx.withoutInput ? "is-without-input" : ""}`),
        y: common_vendor.s(_ctx.customStyle)
      });
    };
  }
});
const Component = /* @__PURE__ */ common_vendor._export_sfc(_sfc_main, [["__scopeId", "data-v-a2d9dd24"]]);
wx.createComponent(Component);
//# sourceMappingURL=../../../../../.sourcemap/mp-weixin/uni_modules/wot-design-uni/components/wd-input-number/wd-input-number.js.map
