"""Mapping from VT100 (ANSI) escape sequences to the corresponding keys."""

from .events import Key

ANSI_ESCAPES: dict[bytes, tuple[Key, bool, bool, bool]] = {
    # ansi: (key, alt, ctrl, shift)
    b"\x00": (" ", False, True, False),
    b"\x01": ("a", False, True, False),
    b"\x02": ("b", False, True, False),
    b"\x03": ("c", False, True, False),
    b"\x04": ("d", False, True, False),
    b"\x05": ("e", False, True, False),
    b"\x06": ("f", False, True, False),
    b"\x07": ("g", False, True, False),
    b"\x08": ("h", False, True, False),
    b"\x09": ("tab", False, False, False),  # ctrl + i
    b"\x0a": ("j", False, True, False),
    b"\x0b": ("k", False, True, False),
    b"\x0c": ("l", False, True, False),
    b"\x0d": ("enter", False, False, False),  # ctrl + m
    b"\x0e": ("n", False, True, False),
    b"\x0f": ("o", False, True, False),
    b"\x10": ("p", False, True, False),
    b"\x11": ("q", False, True, False),
    b"\x12": ("r", False, True, False),
    b"\x13": ("s", False, True, False),
    b"\x14": ("t", False, True, False),
    b"\x15": ("u", False, True, False),
    b"\x16": ("v", False, True, False),
    b"\x17": ("w", False, True, False),
    b"\x18": ("x", False, True, False),
    b"\x19": ("y", False, True, False),
    b"\x1a": ("z", False, True, False),
    b"\x1b": ("escape", False, False, False),
    b"\x1c": ("\\", False, True, False),
    b"\x1d": ("]", False, True, False),
    b"\x1e": ("^", False, True, False),
    b"\x1f": ("-", False, True, False),
    b"\x7f": ("backspace", False, False, False),  # not ctrl + h on WSL
    b"\x9b": ("escape", False, False, True),
    b"\x1b\x00": (" ", True, True, False),
    b"\x1b\x01": ("a", True, True, False),
    b"\x1b\x02": ("b", True, True, False),
    b"\x1b\x03": ("c", True, True, False),
    b"\x1b\x04": ("d", True, True, False),
    b"\x1b\x05": ("e", True, True, False),
    b"\x1b\x06": ("f", True, True, False),
    b"\x1b\x07": ("g", True, True, False),
    b"\x1b\x08": ("h", True, True, False),
    b"\x1b\x09": ("i", True, True, False),
    b"\x1b\x0a": ("j", True, True, False),
    b"\x1b\x0b": ("k", True, True, False),
    b"\x1b\x0c": ("l", True, True, False),
    b"\x1b\x0d": ("m", True, True, False),
    b"\x1b\x0e": ("n", True, True, False),
    b"\x1b\x0f": ("o", True, True, False),
    b"\x1b\x10": ("p", True, True, False),
    b"\x1b\x11": ("q", True, True, False),
    b"\x1b\x12": ("r", True, True, False),
    b"\x1b\x13": ("s", True, True, False),
    b"\x1b\x14": ("t", True, True, False),
    b"\x1b\x15": ("u", True, True, False),
    b"\x1b\x16": ("v", True, True, False),
    b"\x1b\x17": ("w", True, True, False),
    b"\x1b\x18": ("x", True, True, False),
    b"\x1b\x19": ("y", True, True, False),
    b"\x1b\x1a": ("z", True, True, False),
    b"\x1b\x1b": ("#", True, True, False),
    b"\x1b\x1c": ("$", True, True, False),
    b"\x1b\x1d": ("%", True, True, False),
    b"\x1b\x1e": ("^", True, True, False),
    b"\x1b\x1f": ("&", True, True, False),
    b"\x1b\x7f": ("*", True, True, False),
    b"\x1bOA": ("up", False, False, False),
    b"\x1bOB": ("down", False, False, False),
    b"\x1bOC": ("right", False, False, False),
    b"\x1bOD": ("left", False, False, False),
    b"\x1bOF": ("end", False, False, False),
    b"\x1bOH": ("home", False, False, False),
    b"\x1bOP": ("f1", False, False, False),
    b"\x1bOQ": ("f2", False, False, False),
    b"\x1bOR": ("f3", False, False, False),
    b"\x1bOS": ("f4", False, False, False),
    b"\x1bOc": ("right", False, True, False),
    b"\x1bOd": ("left", False, True, False),
    b"\x1b[[A": ("f1", False, False, False),
    b"\x1b[[B": ("f2", False, False, False),
    b"\x1b[[C": ("f3", False, False, False),
    b"\x1b[[D": ("f4", False, False, False),
    b"\x1b[[E": ("f5", False, False, False),
    b"\x1b[A": ("up", False, False, False),
    b"\x1b[B": ("down", False, False, False),
    b"\x1b[C": ("right", False, False, False),
    b"\x1b[D": ("left", False, False, False),
    b"\x1b[F": ("end", False, False, False),
    b"\x1b[H": ("home", False, False, False),
    b"\x1b[Z": ("tab", False, False, True),
    b"\x1b[5A": ("up", False, True, False),
    b"\x1b[5B": ("down", False, True, False),
    b"\x1b[5C": ("right", False, True, False),
    b"\x1b[5D": ("left", False, True, False),
    b"\x1b[1;6P": ("f13", False, True, False),
    b"\x1b[1;6Q": ("f14", False, True, False),
    b"\x1b[1;6R": ("f15", False, True, False),
    b"\x1b[1;6S": ("f16", False, True, False),
    b"\x1b[1~": ("home", False, False, False),
    b"\x1b[2~": ("insert", False, False, False),
    b"\x1b[3~": ("delete", False, False, False),
    b"\x1b[4~": ("end", False, False, False),
    b"\x1b[5~": ("page_up", False, False, False),
    b"\x1b[6~": ("page_down", False, False, False),
    b"\x1b[7~": ("home", False, False, False),
    b"\x1b[8~": ("end", False, False, False),
    b"\x1b[~": ("tab", False, False, True),
    b"\x1b[11~": ("f1", False, False, False),
    b"\x1b[12~": ("f2", False, False, False),
    b"\x1b[13~": ("f3", False, False, False),
    b"\x1b[14~": ("f4", False, False, False),
    b"\x1b[15~": ("f5", False, False, False),
    b"\x1b[17~": ("f6", False, False, False),
    b"\x1b[18~": ("f7", False, False, False),
    b"\x1b[19~": ("f8", False, False, False),
    b"\x1b[20~": ("f9", False, False, False),
    b"\x1b[21~": ("f10", False, False, False),
    b"\x1b[23~": ("f11", False, False, False),
    b"\x1b[24~": ("f12", False, False, False),
    b"\x1b[25~": ("f13", False, False, False),
    b"\x1b[26~": ("f14", False, False, False),
    b"\x1b[28~": ("f15", False, False, False),
    b"\x1b[29~": ("f16", False, False, False),
    b"\x1b[31~": ("f17", False, False, False),
    b"\x1b[32~": ("f18", False, False, False),
    b"\x1b[33~": ("f19", False, False, False),
    b"\x1b[34~": ("f20", False, False, False),
    b"\x1b[15;2~": ("f17", False, False, False),
    b"\x1b[17;2~": ("f18", False, False, False),
    b"\x1b[18;2~": ("f19", False, False, False),
    b"\x1b[19;2~": ("f20", False, False, False),
    b"\x1b[20;2~": ("f21", False, False, False),
    b"\x1b[21;2~": ("f22", False, False, False),
    b"\x1b[23;2~": ("f23", False, False, False),
    b"\x1b[24;2~": ("f24", False, False, False),
    b"\x1b[62~": ("scroll_up", False, False, False),
    b"\x1b[63~": ("scroll_down", False, False, False),
    b"\x1b[3;2~": ("delete", False, False, True),
    b"\x1b[5;2~": ("page_up", False, False, True),
    b"\x1b[6;2~": ("page_down", False, False, True),
    b"\x1b[2;3~": ("insert", True, False, False),
    b"\x1b[3;3~": ("delete", True, False, False),
    b"\x1b[5;3~": ("page_up", True, False, False),
    b"\x1b[6;3~": ("page_down", True, False, False),
    b"\x1b[2;4~": ("insert", True, False, True),
    b"\x1b[3;4~": ("delete", True, False, True),
    b"\x1b[5;4~": ("page_up", True, False, True),
    b"\x1b[6;4~": ("page_down", True, False, True),
    b"\x1b[3;5~": ("delete", False, True, False),
    b"\x1b[5;5~": ("page_up", False, True, False),
    b"\x1b[6;5~": ("page_down", False, True, False),
    b"\x1b[15;5~": ("f5", False, True, False),
    b"\x1b[17;5~": ("f6", False, True, False),
    b"\x1b[18;5~": ("f7", False, True, False),
    b"\x1b[19;5~": ("f8", False, True, False),
    b"\x1b[20;5~": ("f9", False, True, False),
    b"\x1b[21;5~": ("f10", False, True, False),
    b"\x1b[23;5~": ("f11", False, True, False),
    b"\x1b[24;5~": ("f12", False, True, False),
    b"\x1b[3;6~": ("delete", False, True, True),
    b"\x1b[5;6~": ("page_up", False, True, True),
    b"\x1b[6;6~": ("page_down", False, True, True),
    b"\x1b[15;6~": ("f17", False, True, False),
    b"\x1b[17;6~": ("f18", False, True, False),
    b"\x1b[18;6~": ("f19", False, True, False),
    b"\x1b[19;6~": ("f20", False, True, False),
    b"\x1b[20;6~": ("f21", False, True, False),
    b"\x1b[21;6~": ("f22", False, True, False),
    b"\x1b[23;6~": ("f23", False, True, False),
    b"\x1b[24;6~": ("f24", False, True, False),
    b"\x1b[2;7~": ("insert", True, True, False),
    b"\x1b[5;7~": ("page_up", True, True, False),
    b"\x1b[6;7~": ("page_down", True, True, False),
    b"\x1b[2;8~": ("insert", True, True, True),
    b"\x1b[5;8~": ("page_up", True, True, True),
    b"\x1b[6;8~": ("page_down", True, True, True),
    b"\x1b[1;2A": ("up", False, False, True),
    b"\x1b[1;2B": ("down", False, False, True),
    b"\x1b[1;2C": ("right", False, False, True),
    b"\x1b[1;2D": ("left", False, False, True),
    b"\x1b[1;2F": ("end", False, False, True),
    b"\x1b[1;2H": ("home", False, False, True),
    b"\x1b[1;2P": ("f13", False, False, False),
    b"\x1b[1;2Q": ("f14", False, False, False),
    b"\x1b[1;2R": ("f15", False, False, False),
    b"\x1b[1;2S": ("f16", False, False, False),
    b"\x1b[1;3A": ("up", True, False, False),
    b"\x1b[1;3B": ("down", True, False, False),
    b"\x1b[1;3C": ("right", True, False, False),
    b"\x1b[1;3D": ("left", True, False, False),
    b"\x1b[1;3F": ("end", True, False, False),
    b"\x1b[1;3H": ("home", True, False, False),
    b"\x1b[1;4A": ("up", True, False, True),
    b"\x1b[1;4B": ("down", True, False, True),
    b"\x1b[1;4C": ("right", True, False, True),
    b"\x1b[1;4D": ("left", True, False, True),
    b"\x1b[1;4F": ("end", True, False, True),
    b"\x1b[1;4H": ("home", True, False, True),
    b"\x1b[1;5A": ("up", False, True, False),
    b"\x1b[1;5B": ("down", False, True, False),
    b"\x1b[1;5C": ("right", False, True, False),
    b"\x1b[1;5D": ("left", False, True, False),
    b"\x1b[1;5F": ("end", False, True, False),
    b"\x1b[1;5H": ("home", False, True, False),
    b"\x1b[1;5P": ("f1", False, True, False),
    b"\x1b[1;5Q": ("f2", False, True, False),
    b"\x1b[1;5R": ("f3", False, True, False),
    b"\x1b[1;5S": ("f4", False, True, False),
    b"\x1b[1;6A": ("up", False, True, True),
    b"\x1b[1;6B": ("down", False, True, True),
    b"\x1b[1;6C": ("right", False, True, True),
    b"\x1b[1;6D": ("left", False, True, True),
    b"\x1b[1;6F": ("end", False, True, True),
    b"\x1b[1;6H": ("home", False, True, True),
    b"\x1b[1;7A": ("up", True, True, False),
    b"\x1b[1;7B": ("down", True, True, False),
    b"\x1b[1;7C": ("right", True, True, False),
    b"\x1b[1;7D": ("left", True, True, False),
    b"\x1b[1;7F": ("end", True, True, False),
    b"\x1b[1;7H": ("home", True, True, False),
    b"\x1b[1;8A": ("up", True, True, True),
    b"\x1b[1;8B": ("down", True, True, True),
    b"\x1b[1;8C": ("right", True, True, True),
    b"\x1b[1;8D": ("left", True, True, True),
    b"\x1b[1;8F": ("end", True, True, True),
    b"\x1b[1;8H": ("home", True, True, True),
    b"\x1b[1;9A": ("up", True, False, False),
    b"\x1b[1;9B": ("down", True, False, False),
    b"\x1b[1;9C": ("right", True, False, False),
    b"\x1b[1;9D": ("left", True, False, False),
    b"\x1b[1;5p": ("0", False, True, False),
    b"\x1b[1;5q": ("1", False, True, False),
    b"\x1b[1;5r": ("2", False, True, False),
    b"\x1b[1;5s": ("3", False, True, False),
    b"\x1b[1;5t": ("4", False, True, False),
    b"\x1b[1;5u": ("5", False, True, False),
    b"\x1b[1;5v": ("6", False, True, False),
    b"\x1b[1;5w": ("7", False, True, False),
    b"\x1b[1;5x": ("8", False, True, False),
    b"\x1b[1;5y": ("9", False, True, False),
    b"\x1b[1;6p": ("0", False, True, True),
    b"\x1b[1;6q": ("1", False, True, True),
    b"\x1b[1;6r": ("2", False, True, True),
    b"\x1b[1;6s": ("3", False, True, True),
    b"\x1b[1;6t": ("4", False, True, True),
    b"\x1b[1;6u": ("5", False, True, True),
    b"\x1b[1;6v": ("6", False, True, True),
    b"\x1b[1;6w": ("7", False, True, True),
    b"\x1b[1;6x": ("8", False, True, True),
    b"\x1b[1;6y": ("9", False, True, True),
    b"\x1b[1;7p": ("0", True, True, False),
    b"\x1b[1;7q": ("1", True, True, False),
    b"\x1b[1;7r": ("2", True, True, False),
    b"\x1b[1;7s": ("3", True, True, False),
    b"\x1b[1;7t": ("4", True, True, False),
    b"\x1b[1;7u": ("5", True, True, False),
    b"\x1b[1;7v": ("6", True, True, False),
    b"\x1b[1;7w": ("7", True, True, False),
    b"\x1b[1;7x": ("8", True, True, False),
    b"\x1b[1;7y": ("9", True, True, False),
    b"\x1b[1;8p": ("0", True, True, True),
    b"\x1b[1;8q": ("1", True, True, True),
    b"\x1b[1;8r": ("2", True, True, True),
    b"\x1b[1;8s": ("3", True, True, True),
    b"\x1b[1;8t": ("4", True, True, True),
    b"\x1b[1;8u": ("5", True, True, True),
    b"\x1b[1;8v": ("6", True, True, True),
    b"\x1b[1;8w": ("7", True, True, True),
    b"\x1b[1;8x": ("8", True, True, True),
    b"\x1b[1;8y": ("9", True, True, True),
    # WSL
    b"\x1b[15;3~": ("f5", True, False, False),
    b"\x1b[17;3~": ("f6", True, False, False),
    b"\x1b[18;3~": ("f7", True, False, False),
    b"\x1b[19;3~": ("f8", True, False, False),
    b"\x1b[20;3~": ("f9", True, False, False),
    b"\x1b[21;3~": ("f10", True, False, False),
    b"\x1b[23;3~": ("f11", True, False, False),
    b"\x1b[24;3~": ("f12", True, False, False),
    b"\x1b[15;4~": ("f5", True, False, True),
    b"\x1b[17;4~": ("f6", True, False, True),
    b"\x1b[18;4~": ("f7", True, False, True),
    b"\x1b[19;4~": ("f8", True, False, True),
    b"\x1b[20;4~": ("f9", True, False, True),
    b"\x1b[21;4~": ("f10", True, False, True),
    b"\x1b[23;4~": ("f11", True, False, True),
    b"\x1b[24;4~": ("f12", True, False, True),
    b"\x1b[15;7~": ("f5", True, True, False),
    b"\x1b[17;7~": ("f6", True, True, False),
    b"\x1b[18;7~": ("f7", True, True, False),
    b"\x1b[19;7~": ("f8", True, True, False),
    b"\x1b[20;7~": ("f9", True, True, False),
    b"\x1b[21;7~": ("f10", True, True, False),
    b"\x1b[23;7~": ("f11", True, True, False),
    b"\x1b[24;7~": ("f12", True, True, False),
    b"\x1b[15;8~": ("f5", True, True, True),
    b"\x1b[17;8~": ("f6", True, True, True),
    b"\x1b[18;8~": ("f7", True, True, True),
    b"\x1b[19;8~": ("f8", True, True, True),
    b"\x1b[20;8~": ("f9", True, True, True),
    b"\x1b[21;8~": ("f10", True, True, True),
    b"\x1b[23;8~": ("f11", True, True, True),
    b"\x1b[24;8~": ("f12", True, True, True),
    b"\x1b[1;3P": ("f1", True, False, False),
    b"\x1b[1;3Q": ("f2", True, False, False),
    b"\x1b[1;3R": ("f3", True, False, False),
    b"\x1b[1;4P": ("f1", True, False, True),
    b"\x1b[1;4Q": ("f2", True, False, True),
    b"\x1b[1;4R": ("f3", True, False, True),
    b"\x1b[1;4S": ("f4", True, False, True),
    b"\x1b[1;7P": ("f1", True, True, False),
    b"\x1b[1;7Q": ("f2", True, True, False),
    b"\x1b[1;7R": ("f3", True, True, False),
    b"\x1b[1;7S": ("f4", True, True, False),
    b"\x1b[1;8P": ("f1", True, True, True),
    b"\x1b[1;8Q": ("f2", True, True, True),
    b"\x1b[1;8R": ("f3", True, True, True),
    b"\x1b[1;8S": ("f4", True, True, True),
}
