/**
 * tdesign v0.46.4
 * (c) 2022 tdesign
 * @license MIT
 */

'use strict';

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

var _defineProperty = require('@babel/runtime/helpers/defineProperty');
var _slicedToArray = require('@babel/runtime/helpers/slicedToArray');
var VueCompositionAPI = require('@vue/composition-api');
var hooks_useCommonClassName = require('../hooks/useCommonClassName.js');
var hooks_useVModel = require('../hooks/useVModel.js');
var _common_js_inputNumber_number = require('../_common/js/input-number/number.js');
var hooks_useFormDisabled = require('../hooks/useFormDisabled.js');
require('../config-provider/useConfig.js');
require('../config-provider/context.js');
require('lodash/merge');
require('lodash/mergeWith');
require('../_common/js/global-config/default-config.js');
require('../_common/js/global-config/locale/zh_CN.js');
require('lodash/isString');
require('lodash/isNumber');
require('../_common/js/input-number/large-number.js');
require('../_common/js/log/log.js');

function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }

var _defineProperty__default = /*#__PURE__*/_interopDefaultLegacy(_defineProperty);
var _slicedToArray__default = /*#__PURE__*/_interopDefaultLegacy(_slicedToArray);

function useInputNumber(props, context) {
  var _useCommonClassName = hooks_useCommonClassName["default"](),
      classPrefix = _useCommonClassName.classPrefix,
      sizeClassNames = _useCommonClassName.sizeClassNames,
      statusClassNames = _useCommonClassName.statusClassNames;

  var _toRefs = VueCompositionAPI.toRefs(props),
      value = _toRefs.value,
      max = _toRefs.max,
      min = _toRefs.min;

  var _useVModel = hooks_useVModel["default"](value, props.defaultValue, props.onChange, "change"),
      _useVModel2 = _slicedToArray__default["default"](_useVModel, 2),
      tValue = _useVModel2[0],
      setTValue = _useVModel2[1];

  var inputRef = VueCompositionAPI.ref();
  var userInput = VueCompositionAPI.ref("");
  var displayValue = VueCompositionAPI.ref();

  var _useFormDisabled = hooks_useFormDisabled["default"](),
      formDisabled = _useFormDisabled.formDisabled;

  var tDisabled = VueCompositionAPI.computed(function () {
    return props.disabled || formDisabled.value;
  });
  var isError = VueCompositionAPI.ref();
  var disabledReduce = VueCompositionAPI.computed(function () {
    return tDisabled.value || !_common_js_inputNumber_number.canReduceNumber(tValue.value, props.min, props.largeNumber);
  });
  var disabledAdd = VueCompositionAPI.computed(function () {
    return tDisabled.value || !_common_js_inputNumber_number.canAddNumber(tValue.value, props.max, props.largeNumber);
  });
  var wrapClasses = VueCompositionAPI.computed(function () {
    var _ref;

    return ["".concat(classPrefix.value, "-input-number"), sizeClassNames[props.size], (_ref = {}, _defineProperty__default["default"](_ref, statusClassNames.disabled, tDisabled.value), _defineProperty__default["default"](_ref, "".concat(classPrefix.value, "-is-controls-right"), props.theme === "column"), _defineProperty__default["default"](_ref, "".concat(classPrefix.value, "-input-number--").concat(props.theme), props.theme), _defineProperty__default["default"](_ref, "".concat(classPrefix.value, "-input-number--auto-width"), props.autoWidth), _ref)];
  });
  var reduceClasses = VueCompositionAPI.computed(function () {
    return ["".concat(classPrefix.value, "-input-number__decrease"), _defineProperty__default["default"]({}, statusClassNames.disabled, disabledReduce.value)];
  });
  var addClasses = VueCompositionAPI.computed(function () {
    return ["".concat(classPrefix.value, "-input-number__increase"), _defineProperty__default["default"]({}, statusClassNames.disabled, disabledAdd.value)];
  });

  var getUserInput = function getUserInput(value2) {
    var _inputRef$value, _inputRef$value$$el;

    if (!value2 && value2 !== 0) return "";
    var inputStr = String(value2);

    if (!((_inputRef$value = inputRef.value) !== null && _inputRef$value !== void 0 && (_inputRef$value$$el = _inputRef$value.$el) !== null && _inputRef$value$$el !== void 0 && _inputRef$value$$el.contains(document.activeElement))) {
      inputStr = String(_common_js_inputNumber_number.formatToNumber(inputStr, {
        decimalPlaces: props.decimalPlaces,
        largeNumber: props.largeNumber
      }));

      if (props.format) {
        inputStr = String(props.format(value2, {
          fixedNumber: inputStr
        }));
      }
    }

    return inputStr;
  };

  VueCompositionAPI.watch(tValue, function (val) {
    var inputValue = [void 0, null].includes(val) ? "" : String(val);
    userInput.value = getUserInput(inputValue);
  }, {
    immediate: true
  });
  VueCompositionAPI.onMounted(function () {
    userInput.value = getUserInput(tValue.value);
  });
  VueCompositionAPI.watch([tValue, max, min], function () {
    var _props$onValidate;

    if ([void 0, "", null].includes(tValue.value)) return;
    var error = _common_js_inputNumber_number.getMaxOrMinValidateResult({
      value: tValue.value,
      largeNumber: props.largeNumber,
      max: props.max,
      min: props.min
    });
    isError.value = error;
    (_props$onValidate = props.onValidate) === null || _props$onValidate === void 0 ? void 0 : _props$onValidate.call(props, {
      error: error
    });
    context.emit("validate", {
      error: error
    });
  }, {
    immediate: true
  });

  var handleStepValue = function handleStepValue(op) {
    return _common_js_inputNumber_number.getStepValue({
      op: op,
      step: props.step,
      max: props.max,
      min: props.min,
      lastValue: tValue.value,
      largeNumber: props.largeNumber
    });
  };

  var handleReduce = function handleReduce(e) {
    if (disabledReduce.value || props.readonly) return;
    var newValue = handleStepValue("reduce");
    setTValue(newValue, {
      type: "reduce",
      e: e
    });
  };

  var handleAdd = function handleAdd(e) {
    if (disabledAdd.value || props.readonly) return;
    var newValue = handleStepValue("add");
    setTValue(newValue, {
      type: "add",
      e: e
    });
  };

  var onInnerInputChange = function onInnerInputChange(val, ctx) {
    if (!_common_js_inputNumber_number.canInputNumber(val, props.largeNumber)) return;
    userInput.value = val;
    var isDelete = ctx.e.inputType === "deleteContentBackward";
    var newVal = isDelete || props.largeNumber || !val ? val : Number(val);

    if (newVal !== tValue.value && !["-", ".", "e", "E"].includes(val.slice(-1))) {
      setTValue(newVal, {
        type: "input",
        e: ctx.e
      });
    }
  };

  var handleBlur = function handleBlur(value2, ctx) {
    var _props$onBlur;

    userInput.value = getUserInput(tValue.value);
    var newValue = _common_js_inputNumber_number.formatToNumber(value2, {
      decimalPlaces: props.decimalPlaces,
      largeNumber: props.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);
    context.emit("blur", 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);
    context.emit("focus", value2, ctx);
  };

  var handleKeydown = function handleKeydown(value2, ctx) {
    var _props$onKeydown;

    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);
    context.emit("keydown", 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);
    context.emit("keyup", 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);
    context.emit("keypress", value2, ctx);
  };

  var handleEnter = function handleEnter(value2, ctx) {
    var _props$onEnter;

    userInput.value = getUserInput(value2);
    var newValue = _common_js_inputNumber_number.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);
    context.emit("enter", newValue, ctx);
  };

  var focus = function focus() {
    inputRef.value.focus();
  };

  var blur = function blur() {
    inputRef.value.blur();
  };

  var listeners = {
    blur: handleBlur,
    focus: handleFocus,
    keydown: handleKeydown,
    keyup: handleKeyup,
    keypress: handleKeypress,
    enter: handleEnter,
    click: focus
  };
  return {
    classPrefix: classPrefix,
    wrapClasses: wrapClasses,
    reduceClasses: reduceClasses,
    addClasses: addClasses,
    displayValue: displayValue,
    tDisabled: tDisabled,
    isError: isError,
    listeners: listeners,
    userInput: userInput,
    tValue: tValue,
    inputRef: inputRef,
    formDisabled: formDisabled,
    focus: focus,
    blur: blur,
    handleReduce: handleReduce,
    handleAdd: handleAdd,
    onInnerInputChange: onInnerInputChange
  };
}

exports["default"] = useInputNumber;
//# sourceMappingURL=useInputNumber.js.map
