// src/keys.ts
var NAMED_KEY_SEQUENCES = {
  Enter: "\r",
  Tab: "	",
  Escape: "\x1B",
  Backspace: "\x7F",
  Delete: "\x1B[3~",
  ArrowUp: "\x1B[A",
  ArrowDown: "\x1B[B",
  ArrowRight: "\x1B[C",
  ArrowLeft: "\x1B[D",
  Home: "\x1B[H",
  End: "\x1B[F",
  PageUp: "\x1B[5~",
  PageDown: "\x1B[6~",
  Space: " "
};
var NAMED_KEY_LOWER = new Map(
  Object.entries(NAMED_KEY_SEQUENCES).map(([k, v]) => [k.toLowerCase(), v])
);
var VALID_KEYS_HINT = `Valid keys: ${Object.keys(NAMED_KEY_SEQUENCES).join(", ")}, Control+<letter>, Alt+<key>`;
var NAMED_KEY_PATTERN = Object.keys(NAMED_KEY_SEQUENCES).map(caseInsensitivePattern).join("|");
var TERMINAL_KEY_PATTERN = `^(?:${NAMED_KEY_PATTERN}|.|[Cc][Oo][Nn][Tt][Rr][Oo][Ll]\\+[A-Za-z]|[Aa][Ll][Tt]\\+.+)$`;
function unknownKeyError(key) {
  return new Error(`Unknown terminal key: ${key}. ${VALID_KEYS_HINT}`);
}
function keyToSequence(key) {
  const lowerKey = key.toLowerCase();
  const namedSequence = NAMED_KEY_LOWER.get(lowerKey);
  if (namedSequence !== void 0) {
    return namedSequence;
  }
  if (lowerKey.startsWith("control+")) {
    return controlKeyToSequence(key.slice("control+".length));
  }
  if (lowerKey.startsWith("alt+")) {
    const nestedKey = key.slice("alt+".length);
    if (nestedKey.length === 0) {
      throw unknownKeyError(key);
    }
    if (nestedKey.length === 1) {
      return "\x1B" + nestedKey;
    }
    try {
      return "\x1B" + keyToSequence(nestedKey);
    } catch {
      throw unknownKeyError(key);
    }
  }
  if (key.length === 1) {
    return key;
  }
  throw unknownKeyError(key);
}
function controlKeyToSequence(controlKey) {
  if (controlKey.length !== 1) {
    throw unknownKeyError(`Control+${controlKey}`);
  }
  const uppercaseLetter = controlKey.toUpperCase();
  const charCode = uppercaseLetter.charCodeAt(0);
  if (charCode < 65 || charCode > 90) {
    throw unknownKeyError(`Control+${controlKey}`);
  }
  return String.fromCharCode(charCode - 64);
}
function caseInsensitivePattern(value) {
  let pattern = "";
  for (const character of value) {
    const lower = character.toLowerCase();
    const upper = character.toUpperCase();
    pattern += lower === upper ? escapePatternCharacter(character) : `[${upper}${lower}]`;
  }
  return pattern;
}
function escapePatternCharacter(character) {
  return character.replace(/[\\^$.*+?()[\]{}|]/g, "\\$&");
}
export {
  TERMINAL_KEY_PATTERN,
  keyToSequence
};
//# sourceMappingURL=keys.js.map
