// Maps browser KeyboardEvent.code values to Windows virtual-key codes.
// Reference: https://learn.microsoft.com/en-us/windows/win32/inputdev/virtual-key-codes

const VK = {};

// Letters (KeyA..KeyZ -> 0x41..0x5A)
for (let i = 0; i < 26; i++) VK['Key' + String.fromCharCode(65 + i)] = 0x41 + i;
// Top-row digits (Digit0..Digit9 -> 0x30..0x39)
for (let i = 0; i <= 9; i++) VK['Digit' + i] = 0x30 + i;
// Numpad digits
for (let i = 0; i <= 9; i++) VK['Numpad' + i] = 0x60 + i;
// Function keys
for (let i = 1; i <= 24; i++) VK['F' + i] = 0x70 + (i - 1);

Object.assign(VK, {
  Escape: 0x1b,
  Tab: 0x09,
  CapsLock: 0x14,
  ShiftLeft: 0xa0,
  ShiftRight: 0xa1,
  ControlLeft: 0xa2,
  ControlRight: 0xa3,
  AltLeft: 0xa4,
  AltRight: 0xa5,
  MetaLeft: 0x5b,
  MetaRight: 0x5c,
  ContextMenu: 0x5d,
  Space: 0x20,
  Enter: 0x0d,
  NumpadEnter: 0x0d,
  Backspace: 0x08,
  Delete: 0x2e,
  Insert: 0x2d,
  Home: 0x24,
  End: 0x23,
  PageUp: 0x21,
  PageDown: 0x22,
  ArrowUp: 0x26,
  ArrowDown: 0x28,
  ArrowLeft: 0x25,
  ArrowRight: 0x27,
  PrintScreen: 0x2c,
  ScrollLock: 0x91,
  Pause: 0x13,
  NumLock: 0x90,
  // OEM punctuation (US layout)
  Semicolon: 0xba,
  Equal: 0xbb,
  Comma: 0xbc,
  Minus: 0xbd,
  Period: 0xbe,
  Slash: 0xbf,
  Backquote: 0xc0,
  BracketLeft: 0xdb,
  Backslash: 0xdc,
  BracketRight: 0xdd,
  Quote: 0xde,
  // Numpad operators
  NumpadMultiply: 0x6a,
  NumpadAdd: 0x6b,
  NumpadSubtract: 0x6d,
  NumpadDecimal: 0x6e,
  NumpadDivide: 0x6f,
});

/** @param {string} code KeyboardEvent.code @returns {number|undefined} Windows VK code */
function toVirtualKey(code) {
  return VK[code];
}

module.exports = { toVirtualKey, VK };
