import {
  __privateAdd,
  __privateGet
} from "../../chunks/chunk.QU3DSPNU.js";
var _keyboardNavigationHandler;
class ListboxKbNavController {
  constructor(host) {
    __privateAdd(this, _keyboardNavigationHandler, (e) => {
      const targetItem = e.target;
      if (!this.host.navigableItems.includes(targetItem)) return;
      let handled = false;
      if (e.key === "ArrowDown") {
        const nextItemIndex = this.host.navigableItems.indexOf(targetItem) + 1;
        const nextItem = this.host.navigableItems[nextItemIndex];
        nextItem?.focus();
        handled = true;
      } else if (e.key === "ArrowUp") {
        const prevOptionIndex = this.host.navigableItems.indexOf(targetItem) - 1;
        const prevItem = this.host.navigableItems[prevOptionIndex];
        prevItem?.focus();
        handled = true;
      } else if (e.key === "Home") {
        this.host.navigableItems[0]?.focus();
        handled = true;
      } else if (e.key === "End") {
        this.host.navigableItems[this.host.navigableItems.length - 1]?.focus();
        handled = true;
      } else {
        const key = e.key.toLowerCase();
        if (key.length !== 1) {
          return;
        }
        const isLetter = key >= "a" && key <= "z";
        const isNumber = key >= "0" && key <= "9";
        if (isLetter || isNumber) {
          const firstMatch = this.host.navigableItems.find((el) => {
            const text = el.textContent?.trim().toLowerCase();
            return text?.startsWith(key);
          });
          firstMatch?.focus();
          handled = true;
        }
      }
      if (handled) {
        e.preventDefault();
        e.stopPropagation();
      }
    });
    ;
    (this.host = host).addController(this);
  }
  hostConnected() {
    this.host.addEventListener("keydown", __privateGet(this, _keyboardNavigationHandler));
  }
  hostDisconnected() {
    this.host.removeEventListener("keydown", __privateGet(this, _keyboardNavigationHandler));
  }
}
_keyboardNavigationHandler = new WeakMap();
export {
  ListboxKbNavController
};
