/**
 * ldesign v0.3.13
 * (c) 2023 ldesign
 * @license MIT
 */

import _defineProperty from '@babel/runtime/helpers/defineProperty';
import _slicedToArray from '@babel/runtime/helpers/slicedToArray';
import { toRefs, ref, computed, watch, onMounted } from 'vue';
import useCommonClassName from '../hooks/useCommonClassName.js';
import useVModel from '../hooks/useVModel.js';
import { canReduceNumber, canAddNumber, formatToNumber, getMaxOrMinValidateResult, getStepValue, formatThousandths, canInputNumber } from '../_common/js/input-number/number.js';
import { useFormDisabled } from '../form/hooks.js';
import '../hooks/useConfig.js';
import '../config-provider/useConfig.js';
import 'lodash/cloneDeep';
import 'lodash/isFunction';
import 'lodash/isString';
import '../config-provider/context.js';
import 'lodash/mergeWith';
import 'lodash/merge';
import 'lodash/isArray';
import '../_common/js/global-config/default-config.js';
import '../_common/js/global-config/locale/zh_CN.js';
import '@babel/runtime/helpers/typeof';
import '../_chunks/dep-512eb181.js';
import 'dayjs';
import '../_chunks/dep-ac28b67c.js';
import '../config-provider/type.js';
import 'lodash/kebabCase';
import 'lodash/isUndefined';
import 'lodash/isNil';
import 'lodash/isNumber';
import '../_common/js/input-number/large-number.js';
import '../_common/js/log/log.js';
import '../_common/js/log/index.js';

var specialCode = ["-", ".", "e", "E"];
function useInputNumber(props) {
  var _useCommonClassName = useCommonClassName(),
    classPrefix = _useCommonClassName.classPrefix,
    sizeClassNames = _useCommonClassName.sizeClassNames,
    statusClassNames = _useCommonClassName.statusClassNames;
  var _toRefs = toRefs(props),
    value = _toRefs.value,
    modelValue = _toRefs.modelValue,
    max = _toRefs.max,
    min = _toRefs.min;
  var _useVModel = useVModel(value, modelValue, props.defaultValue, props.onChange),
    _useVModel2 = _slicedToArray(_useVModel, 2),
    tValue = _useVModel2[0],
    setTValue = _useVModel2[1];
  var inputRef = ref();
  var userInput = ref("");
  var tDisabled = useFormDisabled();
  var isError = ref();
  var disabledReduce = computed(function () {
    return tDisabled.value || !canReduceNumber(tValue.value, props.min, props.largeNumber);
  });
  var disabledAdd = computed(function () {
    return tDisabled.value || !canAddNumber(tValue.value, props.max, props.largeNumber);
  });
  var wrapClasses = computed(function () {
    var _ref;
    return ["".concat(classPrefix.value, "-input-number"), sizeClassNames[props.size], (_ref = {}, _defineProperty(_ref, statusClassNames.disabled, tDisabled.value), _defineProperty(_ref, "".concat(classPrefix.value, "-is-controls-right"), props.theme === "column"), _defineProperty(_ref, "".concat(classPrefix.value, "-input-number--").concat(props.theme), props.theme), _defineProperty(_ref, "".concat(classPrefix.value, "-input-number--auto-width"), props.autoWidth), _ref)];
  });
  var reduceClasses = computed(function () {
    return ["".concat(classPrefix.value, "-input-number__decrease"), _defineProperty({}, statusClassNames.disabled, disabledReduce.value)];
  });
  var addClasses = computed(function () {
    return ["".concat(classPrefix.value, "-input-number__increase"), _defineProperty({}, statusClassNames.disabled, disabledAdd.value)];
  });
  var getUserInput = function getUserInput(value2) {
    var _inputRef$value, _inputRef$value$input;
    if (!value2 && value2 !== 0) return "";
    var inputStr = value2 || value2 === 0 ? String(value2) : "";
    if (!((_inputRef$value = inputRef.value) !== null && _inputRef$value !== void 0 && (_inputRef$value$input = _inputRef$value.inputRef) !== null && _inputRef$value$input !== void 0 && _inputRef$value$input.contains(document.activeElement))) {
      var num = formatToNumber(inputStr, {
        decimalPlaces: props.decimalPlaces,
        largeNumber: props.largeNumber
      });
      inputStr = num || num === 0 ? String(num) : "";
      if (props.format) inputStr = String(props.format(value2, {
        fixedNumber: inputStr
      }));
    }
    return inputStr;
  };
  watch(tValue, function (val) {
    var inputValue = [void 0, null].includes(val) ? "" : String(val);
    userInput.value = getUserInput(inputValue);
  }, {
    immediate: true
  });
  onMounted(function () {
    userInput.value = getUserInput(tValue.value);
  });
  watch([tValue, max, min], function () {
    var _props$onValidate;
    if ([void 0, "", null].includes(tValue.value)) return;
    var max2 = props.max,
      min2 = props.min,
      largeNumber = props.largeNumber;
    var error = getMaxOrMinValidateResult({
      value: tValue.value,
      largeNumber: largeNumber,
      max: max2,
      min: min2
    });
    isError.value = error;
    (_props$onValidate = props.onValidate) === null || _props$onValidate === void 0 ? void 0 : _props$onValidate.call(props, {
      error: error
    });
  }, {
    immediate: true
  });
  var handleStepValue = function handleStepValue(op) {
    var newValue = getStepValue({
      op: op,
      step: props.step,
      max: props.max,
      min: props.min,
      lastValue: tValue.value,
      largeNumber: props.largeNumber
    });
    var largeNumber = props.largeNumber,
      max2 = props.max,
      min2 = props.min;
    var overLimit = getMaxOrMinValidateResult({
      value: newValue,
      largeNumber: largeNumber,
      max: max2,
      min: min2
    });
    return {
      overLimit: overLimit,
      newValue: newValue
    };
  };
  var handleReduce = function handleReduce(e) {
    if (disabledReduce.value || props.readonly) return;
    var r = handleStepValue("reduce");
    if (r.overLimit && !props.allowInputOverLimit) return;
    setTValue(r.newValue, {
      type: "reduce",
      e: e
    });
  };
  var handleAdd = function handleAdd(e) {
    if (disabledAdd.value || props.readonly) return;
    var r = handleStepValue("add");
    if (r.overLimit && !props.allowInputOverLimit) return;
    setTValue(r.newValue, {
      type: "add",
      e: e
    });
  };
  var onInnerInputChange = function onInnerInputChange(val, _ref4) {
    var e = _ref4.e;
    val = formatThousandths(val);
    if (!canInputNumber(val, props.largeNumber)) return;
    if (props.largeNumber) {
      setTValue(val, {
        type: "input",
        e: e
      });
      return;
    }
    var isDelete = e.inputType === "deleteContentBackward";
    var inputSpecialCode = specialCode.includes(val.slice(-1)) || /\.0+$/.test(val);
    var deleteSpecialCode = isDelete && specialCode.includes(String(userInput.value).slice(-1));
    if (!isNaN(Number(val)) && !inputSpecialCode || deleteSpecialCode) {
      var newVal = val === "" ? void 0 : Number(val);
      setTValue(newVal, {
        type: "input",
        e: e
      });
    }
    if (inputSpecialCode || deleteSpecialCode) userInput.value = val;
  };
  var handleBlur = function handleBlur(value2, ctx) {
    var _props$onBlur;
    var largeNumber = props.largeNumber,
      max2 = props.max,
      min2 = props.min,
      decimalPlaces = props.decimalPlaces;
    if (!props.allowInputOverLimit) {
      var r = getMaxOrMinValidateResult({
        value: tValue.value,
        largeNumber: largeNumber,
        max: max2,
        min: min2
      });
      if (r === "below-minimum") {
        setTValue(min2, {
          type: "blur",
          e: ctx.e
        });
        return;
      }
      if (r === "exceed-maximum") {
        setTValue(max2, {
          type: "blur",
          e: ctx.e
        });
        return;
      }
    }
    userInput.value = getUserInput(tValue.value);
    var newValue = formatToNumber(value2, {
      decimalPlaces: decimalPlaces,
      largeNumber: largeNumber
    });
    if (newValue !== value2 && String(newValue) !== value2) setTValue(newValue, {
      type: "blur",
      e: ctx.e
    });
    (_props$onBlur = props.onBlur) === null || _props$onBlur === void 0 ? void 0 : _props$onBlur.call(props, newValue, ctx);
  };
  var handleFocus = function handleFocus(value2, ctx) {
    var _props$onFocus;
    userInput.value = tValue.value || tValue.value === 0 ? String(tValue.value) : "";
    (_props$onFocus = props.onFocus) === null || _props$onFocus === void 0 ? void 0 : _props$onFocus.call(props, value2, ctx);
  };
  var handleKeydown = function handleKeydown(value2, ctx) {
    var _props$onKeydown;
    if (tDisabled.value) return;
    var e = ctx.e;
    var keyEvent = {
      ArrowUp: handleAdd,
      ArrowDown: handleReduce
    };
    var code = e.code || e.key;
    if (keyEvent[code] !== void 0) keyEvent[code](e);
    (_props$onKeydown = props.onKeydown) === null || _props$onKeydown === void 0 ? void 0 : _props$onKeydown.call(props, value2, ctx);
  };
  var handleKeyup = function handleKeyup(value2, ctx) {
    var _props$onKeyup;
    (_props$onKeyup = props.onKeyup) === null || _props$onKeyup === void 0 ? void 0 : _props$onKeyup.call(props, value2, ctx);
  };
  var handleKeypress = function handleKeypress(value2, ctx) {
    var _props$onKeypress;
    (_props$onKeypress = props.onKeypress) === null || _props$onKeypress === void 0 ? void 0 : _props$onKeypress.call(props, value2, ctx);
  };
  var handleEnter = function handleEnter(value2, ctx) {
    var _props$onEnter;
    userInput.value = getUserInput(value2);
    var newValue = formatToNumber(value2, {
      decimalPlaces: props.decimalPlaces,
      largeNumber: props.largeNumber
    });
    if (newValue !== value2 && String(newValue) !== value2) setTValue(newValue, {
      type: "enter",
      e: ctx.e
    });
    (_props$onEnter = props.onEnter) === null || _props$onEnter === void 0 ? void 0 : _props$onEnter.call(props, newValue, ctx);
  };
  var focus = function focus() {
    inputRef.value.focus();
  };
  var blur = function blur() {
    inputRef.value.blur();
  };
  var listeners = {
    onBlur: handleBlur,
    onFocus: handleFocus,
    onKeydown: handleKeydown,
    onKeyup: handleKeyup,
    onKeypress: handleKeypress,
    onEnter: handleEnter,
    onClick: focus
  };
  return {
    classPrefix: classPrefix,
    wrapClasses: wrapClasses,
    reduceClasses: reduceClasses,
    addClasses: addClasses,
    tDisabled: tDisabled,
    isError: isError,
    listeners: listeners,
    userInput: userInput,
    tValue: tValue,
    inputRef: inputRef,
    focus: focus,
    blur: blur,
    handleReduce: handleReduce,
    handleAdd: handleAdd,
    onInnerInputChange: onInnerInputChange
  };
}

export { useInputNumber as default, specialCode };
//# sourceMappingURL=useInputNumber.js.map
