'use strict';

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

var vue = require('vue');
require('../../../utils/index.cjs');
var useFrame = require('./hooks/useFrame.cjs');
var useCursor = require('./hooks/useCursor.cjs');
var numberUtil = require('./utils/numberUtil.cjs');
var StepHandler = require('./StepHandler.cjs');
var MiniDecimal = require('./utils/MiniDecimal.cjs');
var props = require('./props.cjs');
var KeyCode = require('../../../utils/KeyCode.cjs');
var _class = require('../../../utils/dom/class.cjs');

function getDecimalValue(stringMode, decimalValue) {
  if (stringMode || decimalValue.isEmpty())
    return decimalValue.toString();
  return decimalValue.toNumber();
}
function getDecimalIfValidate(value) {
  const decimal = MiniDecimal.default(value);
  return decimal.isInvalidate() ? null : decimal;
}
var VcInputNumber = /* @__PURE__ */ vue.defineComponent({
  compatConfig: {
    MODE: 3
  },
  name: "InputElement",
  inheritAttrs: false,
  props: {
    ...props.inputElementProps(),
    lazy: Boolean
  },
  slots: Object,
  setup(props, {
    attrs,
    slots,
    emit,
    expose
  }) {
    const inputRef = vue.shallowRef();
    const focus = vue.shallowRef(false);
    const userTypingRef = vue.shallowRef(false);
    const compositionRef = vue.shallowRef(false);
    const decimalValue = vue.shallowRef(MiniDecimal.default(props.value));
    function setUncontrolledDecimalValue(newDecimal) {
      if (props.value === void 0)
        decimalValue.value = newDecimal;
    }
    const getPrecision = (numStr, userTyping) => {
      if (userTyping)
        return void 0;
      if (props.precision >= 0)
        return props.precision;
      return Math.max(numberUtil.getNumberPrecision(numStr), numberUtil.getNumberPrecision(props.step));
    };
    const mergedParser = (num) => {
      const numStr = String(num);
      if (props.parser)
        return props.parser(numStr);
      let parsedStr = numStr;
      if (props.decimalSeparator)
        parsedStr = parsedStr.replace(props.decimalSeparator, ".");
      return parsedStr.replace(/[^\w.-]+/g, "");
    };
    const inputValue = vue.shallowRef("");
    const mergedFormatter = (number, userTyping) => {
      if (props.formatter)
        return props.formatter(number, {
          userTyping,
          input: String(inputValue.value)
        });
      let str = typeof number === "number" ? numberUtil.num2str(number) : number;
      if (!userTyping) {
        const mergedPrecision = getPrecision(str, userTyping);
        if (numberUtil.validateNumber(str) && (props.decimalSeparator || mergedPrecision >= 0)) {
          const separatorStr = props.decimalSeparator || ".";
          str = MiniDecimal.toFixed(str, separatorStr, mergedPrecision);
        }
      }
      return str;
    };
    const initValue = (() => {
      if (decimalValue.value.isInvalidate() && ["string", "number"].includes(typeof props.value))
        return Number.isNaN(props.value) ? "" : props.value;
      return mergedFormatter(decimalValue.value.toString(), false);
    })();
    inputValue.value = initValue;
    function setInputValue(newValue, userTyping) {
      inputValue.value = mergedFormatter(
        // Invalidate number is sometime passed by external control, we should let it go
        // Otherwise is controlled by internal interactive logic which check by userTyping
        // You can ref 'show limited value when input is not focused' test for more info.
        newValue.isInvalidate() ? newValue.toString(false) : newValue.toString(!userTyping),
        userTyping
      );
    }
    const maxDecimal = vue.computed(() => getDecimalIfValidate(props.max));
    const minDecimal = vue.computed(() => getDecimalIfValidate(props.min));
    const upDisabled = vue.computed(() => {
      if (!maxDecimal.value || !decimalValue.value || decimalValue.value.isInvalidate())
        return false;
      return maxDecimal.value.lessEquals(decimalValue.value);
    });
    const downDisabled = vue.computed(() => {
      if (!minDecimal.value || !decimalValue.value || decimalValue.value.isInvalidate())
        return false;
      return decimalValue.value.lessEquals(minDecimal.value);
    });
    const [recordCursor, restoreCursor] = useCursor.default(inputRef, focus);
    const getRangeValue = (target) => {
      if (maxDecimal.value && !target.lessEquals(maxDecimal.value))
        return maxDecimal.value;
      if (minDecimal.value && !minDecimal.value.lessEquals(target))
        return minDecimal.value;
      return null;
    };
    const isInRange = (target) => !getRangeValue(target);
    const triggerValueUpdate = (newValue, userTyping) => {
      var _a;
      let updateValue = newValue;
      let isRangeValidate = isInRange(updateValue) || updateValue.isEmpty();
      if (!updateValue.isEmpty() && !userTyping) {
        updateValue = getRangeValue(updateValue) || updateValue;
        isRangeValidate = true;
      }
      if (!props.readonly && !props.disabled && isRangeValidate) {
        const numStr = updateValue.toString();
        const mergedPrecision = getPrecision(numStr, userTyping);
        if (mergedPrecision >= 0)
          updateValue = MiniDecimal.default(MiniDecimal.toFixed(numStr, ".", mergedPrecision));
        if (!updateValue.equals(decimalValue.value)) {
          setUncontrolledDecimalValue(updateValue);
          (_a = props.onChange) == null ? void 0 : _a.call(props, updateValue.isEmpty() ? null : getDecimalValue(props.stringMode, updateValue));
          if (props.value === void 0)
            setInputValue(updateValue, userTyping);
        }
        return updateValue;
      }
      return decimalValue.value;
    };
    const onNextPromise = useFrame.default();
    const collectInputValue = (inputStr) => {
      var _a;
      recordCursor();
      inputValue.value = inputStr;
      if (!compositionRef.value) {
        const finalValue = mergedParser(inputStr);
        const finalDecimal = MiniDecimal.default(finalValue);
        if (!finalDecimal.isNaN())
          triggerValueUpdate(finalDecimal, true);
      }
      (_a = props.onInput) == null ? void 0 : _a.call(props, inputStr);
      onNextPromise(() => {
        let nextInputStr = inputStr;
        if (!props.parser)
          nextInputStr = inputStr.replace(/。/g, ".");
        if (nextInputStr !== inputStr)
          collectInputValue(nextInputStr);
      });
    };
    const onCompositionStart = () => {
      compositionRef.value = true;
    };
    const onCompositionEnd = () => {
      compositionRef.value = false;
      collectInputValue(inputRef.value.value);
    };
    const onInternalInput = (e) => {
      collectInputValue(e.target.value);
    };
    const onInternalStep = (up) => {
      var _a, _b;
      if (up && upDisabled.value || !up && downDisabled.value)
        return;
      userTypingRef.value = false;
      let stepDecimal = MiniDecimal.default(props.step);
      if (!up)
        stepDecimal = stepDecimal.negate();
      const target = (decimalValue.value || MiniDecimal.default(0)).add(stepDecimal.toString());
      const updatedValue = triggerValueUpdate(target, false);
      (_a = props.onStep) == null ? void 0 : _a.call(props, getDecimalValue(props.stringMode, updatedValue), {
        offset: props.step,
        type: up ? "up" : "down"
      });
      (_b = inputRef.value) == null ? void 0 : _b.focus();
    };
    const flushInputValue = (userTyping) => {
      const parsedValue = MiniDecimal.default(mergedParser(inputValue.value));
      let formatValue = parsedValue;
      if (!parsedValue.isNaN()) {
        formatValue = triggerValueUpdate(parsedValue, userTyping);
      } else {
        formatValue = decimalValue.value;
      }
      if (props.value !== void 0) {
        setInputValue(decimalValue.value, false);
      } else if (!formatValue.isNaN()) {
        setInputValue(formatValue, false);
      }
    };
    const onKeyDown = (event) => {
      var _a;
      const {
        which
      } = event;
      userTypingRef.value = true;
      if (which === KeyCode.KeyCode.ENTER) {
        if (!compositionRef.value)
          userTypingRef.value = false;
        flushInputValue(false);
        (_a = props.onPressEnter) == null ? void 0 : _a.call(props, event);
      }
      if (props.keyboard === false)
        return;
      if (!compositionRef.value && [KeyCode.KeyCode.UP, KeyCode.KeyCode.DOWN].includes(which)) {
        onInternalStep(KeyCode.KeyCode.UP === which);
        event.preventDefault();
      }
    };
    const onKeyUp = () => {
      userTypingRef.value = false;
    };
    const onBlur = (e) => {
      flushInputValue(false);
      focus.value = false;
      userTypingRef.value = false;
      emit("blur", e);
    };
    vue.watch(() => props.precision, () => {
      if (!decimalValue.value.isInvalidate())
        setInputValue(decimalValue.value, false);
    }, {
      flush: "post"
    });
    vue.watch(() => props.value, () => {
      const newValue = MiniDecimal.default(props.value);
      decimalValue.value = newValue;
      const currentParsedValue = MiniDecimal.default(mergedParser(inputValue.value));
      if (!newValue.equals(currentParsedValue) || !userTypingRef.value || props.formatter) {
        setInputValue(newValue, userTypingRef.value);
      }
    }, {
      flush: "post"
    });
    vue.watch(inputValue, () => {
      if (props.formatter)
        restoreCursor();
    }, {
      flush: "post"
    });
    vue.watch(() => props.disabled, (val) => {
      if (val)
        focus.value = false;
    });
    expose({
      focus: () => {
        var _a;
        (_a = inputRef.value) == null ? void 0 : _a.focus();
      },
      blur: () => {
        var _a;
        (_a = inputRef.value) == null ? void 0 : _a.blur();
      }
    });
    return () => {
      const {
        prefixCls = "rc-input-number",
        min,
        max,
        step = 1,
        defaultValue,
        value,
        disabled,
        readonly,
        keyboard,
        controls = true,
        autofocus,
        stringMode,
        parser,
        formatter,
        precision,
        decimalSeparator,
        onChange,
        onInput,
        onPressEnter,
        onStep,
        lazy,
        class: className,
        style,
        ...inputProps
      } = {
        ...attrs,
        ...props
      };
      const {
        upHandler,
        downHandler
      } = slots;
      const inputClassName = `${prefixCls}-input`;
      const eventProps = {};
      if (lazy)
        eventProps.onChange = onInternalInput;
      else
        eventProps.onInput = onInternalInput;
      return vue.createVNode("div", {
        "class": _class.classNames(prefixCls, className, {
          [`${prefixCls}-focused`]: focus.value,
          [`${prefixCls}-disabled`]: disabled,
          [`${prefixCls}-readonly`]: readonly,
          [`${prefixCls}-not-a-number`]: decimalValue.value.isNaN(),
          [`${prefixCls}-out-of-range`]: !decimalValue.value.isInvalidate() && !isInRange(decimalValue.value)
        }),
        "style": style,
        "onKeydown": onKeyDown,
        "onKeyup": onKeyUp
      }, [controls && vue.createVNode(StepHandler.default, {
        "prefixCls": prefixCls,
        "upDisabled": upDisabled.value,
        "downDisabled": downDisabled.value,
        "onStep": onInternalStep
      }, {
        upNode: upHandler,
        downNode: downHandler
      }), vue.createVNode("div", {
        "class": `${inputClassName}-wrap`
      }, [vue.createVNode("input", {
        "autofocus": autofocus,
        "autocomplete": "off",
        "role": "spinbutton",
        "aria-valuemin": min,
        "aria-valuemax": max,
        "aria-valuenow": decimalValue.value.isInvalidate() ? null : decimalValue.value.toString(),
        "step": step,
        ...inputProps,
        "ref": inputRef,
        "class": inputClassName,
        "value": inputValue.value,
        "disabled": disabled,
        "readonly": readonly,
        "onFocus": (e) => {
          focus.value = true;
          emit("focus", e);
        },
        ...eventProps,
        "onBlur": onBlur,
        "onCompositionstart": onCompositionStart,
        "onCompositionend": onCompositionEnd
      }, null)])]);
    };
  }
});

exports.default = VcInputNumber;
//# sourceMappingURL=InputElement.cjs.map
