'use strict';

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

var vue = require('vue');
var index$1 = require('../../icon/index.js');
require('../../../hooks/index.js');
var index = require('../../input/index.js');
require('../../../utils/index.js');
var iconsVue = require('@element-plus/icons-vue');
var inputNumber = require('./input-number2.js');
var pluginVue_exportHelper = require('../../../_virtual/plugin-vue_export-helper.js');
var index$2 = require('../../../hooks/use-namespace/index.js');
var types = require('../../../utils/types.js');
var error = require('../../../utils/error.js');
var index$3 = require('../../../hooks/use-common-props/index.js');
var core = require('@vueuse/core');

const _sfc_main = vue.defineComponent({
  name: "PsInputNumber",
  components: {
    PsInput: index.PsInput,
    PsIcon: index$1.PsIcon,
    ArrowUp: iconsVue.ArrowUp,
    ArrowDown: iconsVue.ArrowDown,
    Plus: iconsVue.Plus,
    Minus: iconsVue.Minus
  },
  props: inputNumber.inputNumberProps,
  setup(props) {
    const input = vue.ref();
    const data = vue.reactive({
      currentValue: props.modelValue,
      userInput: null
    });
    const ns = index$2.useNamespace("input-number");
    const minDisabled = vue.computed(() => ensurePrecision(props.modelValue, -1) < props.min);
    const maxDisabled = vue.computed(() => ensurePrecision(props.modelValue) > props.max);
    const numPrecision = vue.computed(() => {
      const stepPrecision = getPrecision(props.step);
      if (!types.isUndefined(props.precision)) {
        if (stepPrecision > props.precision) {
          error.debugWarn("InputNumber", "precision should not be less than the decimal places of step");
        }
        return props.precision;
      } else {
        return Math.max(getPrecision(props.modelValue), stepPrecision);
      }
    });
    const controlsAtRight = vue.computed(() => {
      return props.controls && props.controlsPosition === "right";
    });
    const inputNumberSize = index$3.useSize();
    const inputNumberDisabled = index$3.useDisabled();
    const displayValue = vue.computed(() => {
      if (data.userInput !== null) {
        return data.userInput;
      }
      let currentValue = data.currentValue;
      if (core.isNumber(currentValue)) {
        if (Number.isNaN(currentValue))
          return "";
        if (!types.isUndefined(props.precision)) {
          currentValue = currentValue.toFixed(props.precision);
        }
      }
      return currentValue;
    });
    const toPrecision = (num, pre) => {
      if (types.isUndefined(pre))
        pre = numPrecision.value;
      return Number.parseFloat(`${Math.round(num * 10 ** pre) / 10 ** pre}`);
    };
    const getPrecision = (value) => {
      if (types.isUndefined(value))
        return 0;
      const valueString = value.toString();
      const dotPosition = valueString.indexOf(".");
      let precision = 0;
      if (dotPosition !== -1) {
        precision = valueString.length - dotPosition - 1;
      }
      return precision;
    };
    const ensurePrecision = (val, coefficient = 1) => {
      if (!core.isNumber(val))
        return data.currentValue;
      val = core.isNumber(val) ? val : Number.NaN;
      return toPrecision(val + props.step * coefficient);
    };
    vue.onMounted(() => {
      var _a;
      const innerInput = (_a = input.value) == null ? void 0 : _a.input;
      innerInput.setAttribute("role", "spinbutton");
      innerInput.setAttribute("aria-valuemax", String(props.max));
      innerInput.setAttribute("aria-valuemin", String(props.min));
      innerInput.setAttribute("aria-valuenow", String(data.currentValue));
      innerInput.setAttribute("aria-disabled", String(inputNumberDisabled.value));
      if (!core.isNumber(props.modelValue)) {
        let val = Number(props.modelValue);
        if (Number.isNaN(val)) {
          val = void 0;
        }
      }
    });
    vue.onUpdated(() => {
      var _a;
      const innerInput = (_a = input.value) == null ? void 0 : _a.input;
      innerInput == null ? void 0 : innerInput.setAttribute("aria-valuenow", data.currentValue);
    });
    return {
      input,
      displayValue,
      controlsAtRight,
      inputNumberSize,
      inputNumberDisabled,
      maxDisabled,
      minDisabled,
      ns
    };
  }
});
function _sfc_render(_ctx, _cache, $props, $setup, $data, $options) {
  const _component_arrow_down = vue.resolveComponent("arrow-down");
  const _component_minus = vue.resolveComponent("minus");
  const _component_ps_icon = vue.resolveComponent("ps-icon");
  const _component_arrow_up = vue.resolveComponent("arrow-up");
  const _component_plus = vue.resolveComponent("plus");
  const _component_ps_input = vue.resolveComponent("ps-input");
  return vue.openBlock(), vue.createElementBlock("div", {
    class: vue.normalizeClass([
      _ctx.ns.b(),
      _ctx.ns.m(_ctx.inputNumberSize),
      _ctx.ns.is("disabled", _ctx.inputNumberDisabled),
      _ctx.ns.is("without-controls", !_ctx.controls),
      _ctx.ns.is("controls-right", _ctx.controlsAtRight)
    ])
  }, [
    _ctx.controls ? (vue.openBlock(), vue.createElementBlock("span", {
      key: 0,
      role: "button",
      class: vue.normalizeClass([_ctx.ns.e("decrease"), _ctx.ns.is("disabled", _ctx.minDisabled)])
    }, [
      vue.createVNode(_component_ps_icon, null, {
        default: vue.withCtx(() => [
          _ctx.controlsAtRight ? (vue.openBlock(), vue.createBlock(_component_arrow_down, { key: 0 })) : (vue.openBlock(), vue.createBlock(_component_minus, { key: 1 }))
        ]),
        _: 1
      })
    ], 2)) : vue.createCommentVNode("v-if", true),
    _ctx.controls ? (vue.openBlock(), vue.createElementBlock("span", {
      key: 1,
      role: "button",
      class: vue.normalizeClass([_ctx.ns.e("increase"), _ctx.ns.is("disabled", _ctx.maxDisabled)])
    }, [
      vue.createVNode(_component_ps_icon, null, {
        default: vue.withCtx(() => [
          _ctx.controlsAtRight ? (vue.openBlock(), vue.createBlock(_component_arrow_up, { key: 0 })) : (vue.openBlock(), vue.createBlock(_component_plus, { key: 1 }))
        ]),
        _: 1
      })
    ], 2)) : vue.createCommentVNode("v-if", true),
    vue.createVNode(_component_ps_input, {
      ref: "input",
      type: "number",
      step: _ctx.step,
      "model-value": _ctx.displayValue,
      placeholder: _ctx.placeholder,
      disabled: _ctx.inputNumberDisabled,
      size: _ctx.inputNumberSize,
      max: _ctx.max,
      min: _ctx.min,
      name: _ctx.name,
      label: _ctx.label,
      "validate-event": false
    }, null, 8, ["step", "model-value", "placeholder", "disabled", "size", "max", "min", "name", "label"])
  ], 2);
}
var InputNumber = /* @__PURE__ */ pluginVue_exportHelper["default"](_sfc_main, [["render", _sfc_render], ["__file", "/Users/meng/workspace/element-ps/packages/components/input-number/src/input-number.vue"]]);

exports["default"] = InputNumber;
//# sourceMappingURL=input-number.js.map
