import {
  __decorateClass,
  __privateAdd,
  __privateGet,
  __privateMethod,
  __privateSet
} from "../../chunks/chunk.QU3DSPNU.js";
var _inputBuffer, _increment, _decrement, _handleClick, _handleFocus, _handleBlur, _handleWheel, _handleKeyDown, _GdsDatePartSpinner_instances, focusNextSpinner_fn, dispatchChangeEvent_fn, formatNumber_fn, clamp_fn, clearInputBuffer_fn;
import { property, state } from "lit/decorators.js";
import { GdsElement } from "../../gds-element.js";
import { gdsCustomElement, html } from "../../scoping.js";
import { watch } from "../../utils/decorators/watch.js";
let GdsDatePartSpinner = class extends GdsElement {
  constructor() {
    super(...arguments);
    __privateAdd(this, _GdsDatePartSpinner_instances);
    this.value = 0;
    this.length = 2;
    this.min = 0;
    this.max = Number.MAX_SAFE_INTEGER;
    this.displayValue = __privateMethod(this, _GdsDatePartSpinner_instances, formatNumber_fn).call(this, this.value, this.length);
    __privateAdd(this, _inputBuffer, "");
    __privateAdd(this, _increment, () => {
      let current = parseInt(this.value.toString());
      if (Number.isNaN(current))
        current = this.length === 2 ? this.min : (/* @__PURE__ */ new Date()).getFullYear() - 1;
      this.value = __privateMethod(this, _GdsDatePartSpinner_instances, clamp_fn).call(this, current + 1);
      __privateMethod(this, _GdsDatePartSpinner_instances, dispatchChangeEvent_fn).call(this);
    });
    __privateAdd(this, _decrement, () => {
      let current = parseInt(this.value.toString());
      if (Number.isNaN(current))
        current = this.length === 2 ? this.max : (/* @__PURE__ */ new Date()).getFullYear() + 1;
      this.value = __privateMethod(this, _GdsDatePartSpinner_instances, clamp_fn).call(this, current - 1);
      __privateMethod(this, _GdsDatePartSpinner_instances, dispatchChangeEvent_fn).call(this);
    });
    __privateAdd(this, _handleClick, (e) => {
      e.stopPropagation();
      e.preventDefault();
      this.focus();
    });
    __privateAdd(this, _handleFocus, () => {
      __privateMethod(this, _GdsDatePartSpinner_instances, clearInputBuffer_fn).call(this);
    });
    __privateAdd(this, _handleBlur, () => {
      if (__privateGet(this, _inputBuffer) === "") return;
      __privateMethod(this, _GdsDatePartSpinner_instances, clearInputBuffer_fn).call(this);
      this.value = __privateMethod(this, _GdsDatePartSpinner_instances, clamp_fn).call(this, parseInt(this.value.toString()));
      __privateMethod(this, _GdsDatePartSpinner_instances, dispatchChangeEvent_fn).call(this);
    });
    __privateAdd(this, _handleWheel, (e) => {
      e.stopPropagation();
      e.preventDefault();
      if (e.deltaY > 0) {
        __privateGet(this, _decrement).call(this);
      } else {
        __privateGet(this, _increment).call(this);
      }
    });
    __privateAdd(this, _handleKeyDown, (e) => {
      let handled = false;
      if (e.key === "ArrowUp") {
        __privateGet(this, _increment).call(this);
        handled = true;
      } else if (e.key === "ArrowDown") {
        __privateGet(this, _decrement).call(this);
        handled = true;
      } else {
        const key = parseInt(e.key);
        if (!isNaN(key)) {
          if (__privateGet(this, _inputBuffer).length < this.length) {
            __privateSet(this, _inputBuffer, __privateGet(this, _inputBuffer) + key.toString());
            this.value = parseInt(__privateGet(this, _inputBuffer));
          }
          if (__privateGet(this, _inputBuffer).length === this.length) {
            this.value = __privateMethod(this, _GdsDatePartSpinner_instances, clamp_fn).call(this, this.value);
            __privateMethod(this, _GdsDatePartSpinner_instances, clearInputBuffer_fn).call(this);
            __privateMethod(this, _GdsDatePartSpinner_instances, focusNextSpinner_fn).call(this);
            __privateMethod(this, _GdsDatePartSpinner_instances, dispatchChangeEvent_fn).call(this);
          }
          handled = true;
        }
      }
      if (handled) {
        e.preventDefault();
        e.stopPropagation();
      }
    });
  }
  // No need for a shadow root here
  createRenderRoot() {
    return this;
  }
  connectedCallback() {
    super.connectedCallback();
    this.setAttribute("role", "spinbutton");
    this.setAttribute("tabindex", "0");
    this.addEventListener("wheel", __privateGet(this, _handleWheel));
    this.addEventListener("keydown", __privateGet(this, _handleKeyDown));
    this.addEventListener("blur", __privateGet(this, _handleBlur));
    this.addEventListener("focus", __privateGet(this, _handleFocus));
    this.addEventListener("click", __privateGet(this, _handleClick));
    this.addEventListener("mousedown", __privateGet(this, _handleClick));
  }
  focus(options) {
    super.focus(options);
    __privateGet(this, _handleFocus).call(this);
  }
  render() {
    return html`${this.displayValue}`;
  }
  _refreshDisplayValue() {
    this.displayValue = __privateMethod(this, _GdsDatePartSpinner_instances, formatNumber_fn).call(this, this.value, this.length);
    this.setAttribute("aria-valuetext", this.value.toString());
  }
};
_inputBuffer = new WeakMap();
_increment = new WeakMap();
_decrement = new WeakMap();
_handleClick = new WeakMap();
_handleFocus = new WeakMap();
_handleBlur = new WeakMap();
_handleWheel = new WeakMap();
_handleKeyDown = new WeakMap();
_GdsDatePartSpinner_instances = new WeakSet();
focusNextSpinner_fn = function() {
  let nextSpinner = this.nextElementSibling;
  while (nextSpinner) {
    if (nextSpinner instanceof GdsDatePartSpinner) {
      nextSpinner.focus();
      break;
    }
    nextSpinner = nextSpinner.nextElementSibling;
  }
};
dispatchChangeEvent_fn = function() {
  this.dispatchCustomEvent("change", {
    detail: { value: this.value.toString() }
  });
};
formatNumber_fn = function(num, padZeros) {
  return String(num).padStart(padZeros, "0");
};
clamp_fn = function(value) {
  return Math.max(this.min, Math.min(this.max, value));
};
clearInputBuffer_fn = function() {
  __privateSet(this, _inputBuffer, "");
};
GdsDatePartSpinner.formAssociated = true;
__decorateClass([
  property({ type: Number })
], GdsDatePartSpinner.prototype, "value", 2);
__decorateClass([
  property({ type: Number })
], GdsDatePartSpinner.prototype, "length", 2);
__decorateClass([
  property({ type: Number, attribute: "aria-valuemin" })
], GdsDatePartSpinner.prototype, "min", 2);
__decorateClass([
  property({ type: Number, attribute: "aria-valuemax" })
], GdsDatePartSpinner.prototype, "max", 2);
__decorateClass([
  state()
], GdsDatePartSpinner.prototype, "displayValue", 2);
__decorateClass([
  watch("value")
], GdsDatePartSpinner.prototype, "_refreshDisplayValue", 1);
GdsDatePartSpinner = __decorateClass([
  gdsCustomElement("gds-date-part-spinner")
], GdsDatePartSpinner);
export {
  GdsDatePartSpinner
};
