"use strict";
const vue = require("vue");
const event = require("./event-BbcvB460.js");
const constants = require("./constants-DxId_i-P.js");
const index = require("./index-C_bsiScj.js");
const useDefaults = require("./useDefaults-YXpmugKp.js");
const useId = require("./useId-Dl8Hi5_U.js");
const useRtl = require("./useRtl-Cdq3VbsD.js");
const index$1 = require("./index-JJEBhaXq.js");
const _hoisted_1 = ["lang", "tabindex", "title"];
const _hoisted_2 = ["name", "form", "value"];
const _hoisted_3 = ["id", "dir", "tabindex", "aria-label", "aria-invalid", "aria-required", "aria-valuemin", "aria-valuemax", "aria-valuenow", "aria-valuetext"];
const defaultValues = {
  min: 1,
  max: 100,
  step: 1,
  repeatDelay: 500,
  repeatInterval: 100,
  repeatThreshold: 10,
  repeatMultiplier: 4
};
const _sfc_main = /* @__PURE__ */ vue.defineComponent({
  __name: "BFormSpinbutton",
  props: /* @__PURE__ */ vue.mergeModels({
    ariaControls: { default: void 0 },
    ariaLabel: { default: void 0 },
    disabled: { type: Boolean, default: false },
    form: { default: void 0 },
    formatterFn: { type: Function, default: void 0 },
    id: { default: void 0 },
    inline: { type: Boolean, default: false },
    labelDecrement: { default: "Decrement" },
    labelIncrement: { default: "Increment" },
    locale: { default: void 0 },
    max: { default: defaultValues.max },
    min: { default: defaultValues.min },
    name: { default: void 0 },
    placeholder: { default: void 0 },
    readonly: { type: Boolean, default: false },
    repeatDelay: { default: defaultValues.repeatDelay },
    repeatInterval: { default: defaultValues.repeatInterval },
    repeatStepMultiplier: { default: defaultValues.repeatMultiplier },
    repeatThreshold: { default: defaultValues.repeatThreshold },
    required: { type: Boolean, default: false },
    size: { default: void 0 },
    state: { type: [Boolean, null], default: null },
    step: { default: defaultValues.step },
    vertical: { type: Boolean, default: false },
    wrap: { type: Boolean, default: false }
  }, {
    "modelValue": {
      default: null
    },
    "modelModifiers": {}
  }),
  emits: /* @__PURE__ */ vue.mergeModels(["change"], ["update:modelValue"]),
  setup(__props, { emit: __emit }) {
    const KEY_CODES = [constants.CODE_UP, constants.CODE_DOWN, constants.CODE_HOME, constants.CODE_END, constants.CODE_PAGEUP, constants.CODE_PAGEDOWN];
    const _props = __props;
    const props = useDefaults.useDefaults(_props, "BFormSpinbutton");
    const emit = __emit;
    const modelValue = vue.useModel(__props, "modelValue");
    const element = vue.useTemplateRef("_element");
    const { focused } = index.useFocus(element);
    const computedId = useId.useId(() => props.id, "spinbutton");
    const computedClasses = vue.computed(() => ({
      "disabled": props.disabled,
      "readonly": props.readonly,
      "focus": focused.value,
      "d-inline-flex": props.inline || props.vertical,
      "d-flex": !props.inline && !props.vertical,
      "align-items-stretch": !props.vertical,
      "flex-column": props.vertical,
      [`form-control-${props.size}`]: props.size !== void 0
    }));
    const computedSpinClasses = vue.computed(() => ({
      "d-flex": props.vertical,
      "align-self-center": !props.vertical,
      "align-items-center": props.vertical,
      "border-top": props.vertical,
      "border-bottom": props.vertical,
      "border-start": !props.vertical,
      "border-end": !props.vertical
    }));
    let $_autoDelayTimer;
    let $_autoRepeatTimer;
    let $_keyIsDown = false;
    const stepNumber = index$1.useToNumber(() => props.step);
    const computedStep = vue.computed(
      () => Number.isNaN(stepNumber.value) ? defaultValues.step : stepNumber.value
    );
    const minNumber = index$1.useToNumber(() => props.min);
    const computedMin = vue.computed(
      () => Number.isNaN(minNumber.value) ? defaultValues.min : minNumber.value
    );
    const maxNumber = index$1.useToNumber(() => props.max);
    const computedMax = vue.computed(() => {
      const step = computedStep.value;
      const min = computedMin.value;
      return Math.floor((maxNumber.value - min) / step) * step + min;
    });
    const repeatDelayNumber = index$1.useToNumber(() => props.repeatDelay, {
      nanToZero: true,
      method: "parseInt"
    });
    const computedDelay = vue.computed(
      () => repeatDelayNumber.value > 0 ? repeatDelayNumber.value : defaultValues.repeatDelay
    );
    const repeatIntervalNumber = index$1.useToNumber(() => props.repeatInterval, {
      nanToZero: true,
      method: "parseInt"
    });
    const computedInterval = vue.computed(
      () => repeatIntervalNumber.value > 0 ? repeatIntervalNumber.value : defaultValues.repeatInterval
    );
    const repeatThresholdNumber = index$1.useToNumber(() => props.repeatThreshold, {
      nanToZero: true,
      method: "parseInt"
    });
    const computedThreshold = vue.computed(
      () => Math.max(
        Number.isNaN(repeatThresholdNumber.value) ? defaultValues.repeatThreshold : repeatThresholdNumber.value,
        1
      )
    );
    const repeatStepMultiplierNumber = index$1.useToNumber(() => props.repeatStepMultiplier, {
      nanToZero: true,
      method: "parseInt"
    });
    const computedStepMultiplier = vue.computed(
      () => Math.max(
        Number.isNaN(repeatStepMultiplierNumber.value) ? defaultValues.repeatMultiplier : repeatStepMultiplierNumber.value,
        1
      )
    );
    const computedPrecision = vue.computed(() => {
      const step = computedStep.value;
      return Math.floor(step) === step ? 0 : (step.toString().split(".")[1] || "").length;
    });
    const computedMultiplier = vue.computed(() => Math.pow(10, computedPrecision.value || 0));
    const valueAsFixed = vue.computed(
      () => modelValue.value === null ? "" : modelValue.value.toFixed(computedPrecision.value)
    );
    const { isRtl, locale: globalLocale } = useRtl.useRtl();
    const computedLocale = vue.computed(() => {
      const loc = (props.locale ?? globalLocale?.value) || "locale";
      const locales = [loc];
      const nf = new Intl.NumberFormat(locales);
      return nf.resolvedOptions().locale;
    });
    const defaultFormatter = () => new Intl.NumberFormat(computedLocale.value, {
      style: "decimal",
      useGrouping: false,
      minimumIntegerDigits: 1,
      minimumFractionDigits: computedPrecision.value,
      maximumFractionDigits: computedPrecision.value,
      notation: "standard"
    }).format;
    const computedFormatter = vue.computed(() => props.formatterFn ?? defaultFormatter());
    const stepValue = (direction) => {
      let { value } = modelValue;
      if (!props.disabled && value !== null) {
        const step = computedStep.value * direction;
        const min = computedMin.value;
        const max = computedMax.value;
        const multiplier = computedMultiplier.value;
        const { wrap } = props;
        value = Math.round((value - min) / step) * step + min + step;
        value = Math.round(value * multiplier) / multiplier;
        modelValue.value = value > max ? wrap ? min : max : value < min ? wrap ? max : min : value;
      }
    };
    const stepUp = (multiplier = 1) => {
      if (modelValue.value === null) {
        modelValue.value = computedMin.value;
        return;
      }
      stepValue(1 * multiplier);
    };
    const stepDown = (multiplier = 1) => {
      if (modelValue.value === null) {
        modelValue.value = props.wrap ? computedMax.value : computedMin.value;
        return;
      }
      stepValue(-1 * multiplier);
    };
    index.onKeyStroke(
      KEY_CODES,
      (event$1) => {
        const { code, altKey, ctrlKey, metaKey } = event$1;
        if (props.disabled || props.readonly || altKey || ctrlKey || metaKey) return;
        event.stopEvent(event$1, { immediatePropagation: true });
        if ($_keyIsDown) {
          return;
        }
        resetTimers();
        if ([constants.CODE_UP, constants.CODE_DOWN].includes(code)) {
          $_keyIsDown = true;
          if (code === constants.CODE_UP) {
            handleStepRepeat(event$1, stepUp);
            return;
          }
          if (code === constants.CODE_DOWN) {
            handleStepRepeat(event$1, stepDown);
          }
          return;
        }
        if (code === constants.CODE_PAGEUP) {
          stepUp(computedStepMultiplier.value);
          return;
        }
        if (code === constants.CODE_PAGEDOWN) {
          stepDown(computedStepMultiplier.value);
          return;
        }
        if (code === constants.CODE_HOME) {
          modelValue.value = computedMin.value;
          return;
        }
        if (code === constants.CODE_END) {
          modelValue.value = computedMax.value;
        }
      },
      { target: element, eventName: "keydown" }
    );
    index.onKeyStroke(
      KEY_CODES,
      (event$1) => {
        const { altKey, ctrlKey, metaKey } = event$1;
        if (props.disabled || props.readonly || altKey || ctrlKey || metaKey) return;
        event.stopEvent(event$1, { immediatePropagation: true });
        resetTimers();
        $_keyIsDown = false;
        emit("change", modelValue.value);
      },
      { target: element, eventName: "keyup" }
    );
    const handleStepRepeat = (event2, stepper) => {
      const { type } = event2 || {};
      if (!props.disabled && !props.readonly) {
        if (isMouseEvent(event2)) {
          if (type === "mousedown" && event2.button) return;
        }
        resetTimers();
        stepper(1);
        const threshold = computedThreshold.value;
        const multiplier = computedStepMultiplier.value;
        const delay = computedDelay.value;
        const interval = computedInterval.value;
        $_autoDelayTimer = setTimeout(() => {
          let count = 0;
          $_autoRepeatTimer = setInterval(() => {
            stepper(count < threshold ? 1 : multiplier);
            count++;
          }, interval);
        }, delay);
      }
    };
    const isMouseEvent = (evt) => evt.type === "mouseup" || evt.type === "mousedown";
    const onMouseup = (event$1) => {
      if (isMouseEvent(event$1)) {
        if (event$1.type === "mouseup" && event$1.button) {
          return;
        }
      }
      event.stopEvent(event$1, { immediatePropagation: true });
      resetTimers();
      setMouseup(false);
      emit("change", modelValue.value);
    };
    const setMouseup = (on) => {
      try {
        event.eventOnOff(on, [document.body, "mouseup", onMouseup, false]);
        event.eventOnOff(on, [document.body, "touchend", onMouseup, false]);
      } catch {
      }
    };
    const resetTimers = () => {
      clearTimeout($_autoDelayTimer);
      clearInterval($_autoRepeatTimer);
      $_autoDelayTimer = void 0;
      $_autoRepeatTimer = void 0;
    };
    const buttons = vue.computed(() => {
      const incrementSvgAttrs = {
        svg: {
          xmlns: "http://www.w3.org/2000/svg",
          width: "16",
          height: "16",
          fill: "currentColor",
          class: "bi bi-plus",
          viewBox: "0 0 16 16"
        },
        path: {
          d: "M8 4a.5.5 0 0 1 .5.5v3h3a.5.5 0 0 1 0 1h-3v3a.5.5 0 0 1-1 0v-3h-3a.5.5 0 0 1 0-1h3v-3A.5.5 0 0 1 8 4z"
        }
      };
      const decrementSvgAttrs = {
        svg: {
          xmlns: "http://www.w3.org/2000/svg",
          width: "16",
          height: "16",
          fill: "currentColor",
          class: "bi bi-dash",
          viewBox: "0 0 16 16"
        },
        path: { d: "M4 8a.5.5 0 0 1 .5-.5h7a.5.5 0 0 1 0 1h-7A.5.5 0 0 1 4 8z" }
      };
      const sharedButtonAttrs = {
        "class": [{ "py-0": !props.vertical }, "btn", "btn-sm", "border-0", "rounded-0"],
        "tabindex": "-1",
        "type": "button",
        "disabled": props.disabled || props.readonly,
        "aria-disabled": props.disabled || props.readonly ? true : void 0,
        "aria-controls": computedId.value
      };
      const sharedSvgAttrs = {
        "aria-hidden": true,
        "scale": focused.value ? 1.5 : 1.25
      };
      const handler = (event$1, stepper) => {
        if (!props.disabled && !props.readonly) {
          event.stopEvent(event$1, { immediatePropagation: true });
          setMouseup(true);
          focused.value = true;
          handleStepRepeat(event$1, stepper);
        }
      };
      const incrementAttrs = {
        button: {
          ...sharedButtonAttrs,
          "aria-label": props.labelIncrement || void 0,
          "aria-keyshortcuts": "ArrowUp"
        },
        svg: {
          ...sharedSvgAttrs,
          ...incrementSvgAttrs.svg
        },
        path: {
          ...incrementSvgAttrs.path
        },
        slot: {
          name: "increment"
        },
        handler: (e) => handler(e, stepUp)
      };
      const decrementAttrs = {
        button: {
          ...sharedButtonAttrs,
          "aria-label": props.labelDecrement || void 0,
          "aria-keyshortcuts": "ArrowDown"
        },
        svg: {
          ...sharedSvgAttrs,
          ...decrementSvgAttrs.svg
        },
        path: {
          ...decrementSvgAttrs.path
        },
        slot: {
          name: "decrement"
        },
        handler: (e) => handler(e, stepDown)
      };
      return {
        top: {
          ...props.vertical ? incrementAttrs : decrementAttrs
        },
        bottom: {
          ...!props.vertical ? incrementAttrs : decrementAttrs
        }
      };
    });
    return (_ctx, _cache) => {
      return vue.openBlock(), vue.createElementBlock("div", {
        ref: "_element",
        class: vue.normalizeClass(["b-form-spinbutton form-control", computedClasses.value]),
        role: "group",
        lang: computedLocale.value,
        tabindex: vue.unref(props).disabled ? void 0 : "-1",
        title: vue.unref(props).ariaLabel,
        onClick: _cache[4] || (_cache[4] = ($event) => focused.value = true)
      }, [
        vue.renderSlot(_ctx.$slots, buttons.value.top.slot.name, { hasFocus: vue.unref(focused) }, () => [
          vue.createElementVNode("button", vue.mergeProps(buttons.value.top.button, {
            onMousedown: _cache[0] || (_cache[0] = //@ts-ignore
            (...args) => buttons.value.top.handler && buttons.value.top.handler(...args)),
            onTouchstart: _cache[1] || (_cache[1] = //@ts-ignore
            (...args) => buttons.value.top.handler && buttons.value.top.handler(...args))
          }), [
            (vue.openBlock(), vue.createElementBlock("svg", vue.normalizeProps(vue.guardReactiveProps(buttons.value.top.svg)), [
              vue.createElementVNode("path", vue.normalizeProps(vue.guardReactiveProps(buttons.value.top.path)), null, 16)
            ], 16))
          ], 16)
        ]),
        vue.unref(props).name && !vue.unref(props).disabled ? (vue.openBlock(), vue.createElementBlock("input", {
          key: "hidden",
          type: "hidden",
          name: vue.unref(props).name,
          form: vue.unref(props).form,
          value: valueAsFixed.value
        }, null, 8, _hoisted_2)) : vue.createCommentVNode("", true),
        vue.createElementVNode("output", {
          id: vue.unref(computedId),
          key: "output",
          class: vue.normalizeClass(["flex-grow-1", computedSpinClasses.value]),
          dir: vue.unref(isRtl) ?? false ? "rtl" : "ltr",
          tabindex: vue.unref(props).disabled ? void 0 : "0",
          role: "spinbutton",
          "aria-live": "off",
          "aria-label": vue.unref(props).ariaLabel || void 0,
          "aria-invalid": vue.unref(props).state === false || !modelValue.value !== null && vue.unref(props).required ? true : void 0,
          "aria-required": vue.unref(props).required ? true : void 0,
          "aria-valuemin": computedMin.value,
          "aria-valuemax": computedMax.value,
          "aria-valuenow": modelValue.value !== null ? modelValue.value : void 0,
          "aria-valuetext": modelValue.value !== null ? computedFormatter.value(modelValue.value) : void 0
        }, [
          vue.createElementVNode("bdi", null, vue.toDisplayString((modelValue.value !== null ? computedFormatter.value(modelValue.value) : vue.unref(props).placeholder) || ""), 1)
        ], 10, _hoisted_3),
        vue.renderSlot(_ctx.$slots, buttons.value.bottom.slot.name, { hasFocus: vue.unref(focused) }, () => [
          vue.createElementVNode("button", vue.mergeProps(buttons.value.bottom.button, {
            onMousedown: _cache[2] || (_cache[2] = //@ts-ignore
            (...args) => buttons.value.bottom.handler && buttons.value.bottom.handler(...args)),
            onTouchstart: _cache[3] || (_cache[3] = //@ts-ignore
            (...args) => buttons.value.bottom.handler && buttons.value.bottom.handler(...args))
          }), [
            (vue.openBlock(), vue.createElementBlock("svg", vue.normalizeProps(vue.guardReactiveProps(buttons.value.bottom.svg)), [
              vue.createElementVNode("path", vue.normalizeProps(vue.guardReactiveProps(buttons.value.bottom.path)), null, 16)
            ], 16))
          ], 16)
        ])
      ], 10, _hoisted_1);
    };
  }
});
exports._sfc_main = _sfc_main;
//# sourceMappingURL=BFormSpinbutton.vue_vue_type_script_setup_true_lang-Ds5BZG1w.js.map
