"use strict";
Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } });
const aria$1 = require("../../../../utils/dom/aria.js");
const aria = require("../../../../constants/aria.js");
const event = require("../../../../utils/dom/event.js");
const submenu = require("./submenu.js");
var MenuItem = class {
  constructor(domNode, namespace) {
    this.domNode = domNode;
    this.submenu = null;
    this.submenu = null;
    this.init(namespace);
  }
  init(namespace) {
    this.domNode.setAttribute("tabindex", "0");
    const menuChild = this.domNode.querySelector(`.${namespace}-menu`);
    if (menuChild) this.submenu = new submenu.default(this, menuChild);
    this.addListeners();
  }
  addListeners() {
    this.domNode.addEventListener("keydown", (event$1) => {
      const code = event.getEventCode(event$1);
      let prevDef = false;
      switch (code) {
        case aria.EVENT_CODE.down:
          aria$1.triggerEvent(event$1.currentTarget, "mouseenter");
          this.submenu && this.submenu.gotoSubIndex(0);
          prevDef = true;
          break;
        case aria.EVENT_CODE.up:
          aria$1.triggerEvent(event$1.currentTarget, "mouseenter");
          this.submenu && this.submenu.gotoSubIndex(this.submenu.subMenuItems.length - 1);
          prevDef = true;
          break;
        case aria.EVENT_CODE.tab:
          aria$1.triggerEvent(event$1.currentTarget, "mouseleave");
          break;
        case aria.EVENT_CODE.enter:
        case aria.EVENT_CODE.numpadEnter:
        case aria.EVENT_CODE.space:
          prevDef = true;
          event$1.currentTarget.click();
          break;
      }
      if (prevDef) event$1.preventDefault();
    });
  }
};
exports.default = MenuItem;
