import { charKeyNameToSvgKeyId } from './vk-to-keyboard-svg-id';

function normalizeKeyToken(token: string): string {
  return token.trim().toLowerCase().replace(/-/g, '_');
}

function tokenToSvgIds(token: string): string[] {
  const t = normalizeKeyToken(token);
  if (!t) {
    return [];
  }

  const named: Record<string, string[]> = {
    shift: ['K_kb5a'],
    l_shift: ['K_kb5a'],
    r_shift: ['K_kb5m'],
    left_shift: ['K_kb5a'],
    right_shift: ['K_kb5m'],
    lshift: ['K_kb5a'],
    rshift: ['K_kb5m'],

    meta: ['K_kb6b'],
    super: ['K_kb6b'],
    l_super: ['K_kb6b'],
    r_super: ['K_kb6l'],
    win: ['K_kb6b'],
    windows: ['K_kb6b'],
    l_meta: ['K_kb6b'],
    r_meta: ['K_kb6l'],
    left_meta: ['K_kb6b'],
    right_meta: ['K_kb6l'],
    l_win: ['K_kb6b'],
    r_win: ['K_kb6l'],
    left_win: ['K_kb6b'],
    right_win: ['K_kb6l'],

    control: ['K_kb6a'],
    ctrl: ['K_kb6a'],
    l_control: ['K_kb6a'],
    r_control: ['K_kb6n'],
    left_control: ['K_kb6a'],
    right_control: ['K_kb6n'],
    l_ctrl: ['K_kb6a'],
    r_ctrl: ['K_kb6n'],
    left_ctrl: ['K_kb6a'],
    right_ctrl: ['K_kb6n'],

    alt: ['K_kb6c'],
    l_alt: ['K_kb6c'],
    r_alt: ['K_kb6k'],
    left_alt: ['K_kb6c'],
    right_alt: ['K_kb6k'],

    tab: ['K_kb3a'],
    enter: ['K_kb4n'],
    return: ['K_kb4n'],
    backspace: ['K_kb2n'],
    space: ['K_kb6d'],
    caps: ['K_kb4a'],
    menu: ['K_kb6m'],

    // Пунктуация по имени (на случай если агент шлёт имя, а не символ)
    comma: ['K_kb5j'],
    period: ['K_kb5k'],
    dot: ['K_kb5k'],
    slash: ['K_kb5l'],
    semicolon: ['K_kb4l'],
    colon: ['K_kb4l'],
    quote: ['K_kb4m'],
    apostrophe: ['K_kb4m'],
    grave: ['K_kb2a'],
    backtick: ['K_kb2a'],
    tilde: ['K_kb2a'],
    minus: ['K_kb2l'],
    dash: ['K_kb2l'],
    underscore: ['K_kb2l'],
    equal: ['K_kb2m'],
    equals: ['K_kb2m'],
    plus: ['K_kb2m'],
    left_bracket: ['K_kb3l'],
    leftbracket: ['K_kb3l'],
    right_bracket: ['K_kb3m'],
    rightbracket: ['K_kb3m'],
    backslash: ['K_kb3n'],
    pipe: ['K_kb3n'],

    up: ['K_kb7u'],
    down: ['K_kb7d'],
    left: ['K_kb7l'],
    right: ['K_kb7r'],
    arrow_up: ['K_kb7u'],
    arrow_down: ['K_kb7d'],
    arrow_left: ['K_kb7l'],
    arrow_right: ['K_kb7r'],
    arrowup: ['K_kb7u'],
    arrowdown: ['K_kb7d'],
    arrowleft: ['K_kb7l'],
    arrowright: ['K_kb7r'],
  };

  if (named[t]) {
    return named[t]!;
  }

  const single = charKeyNameToSvgKeyId(t);
  if (single) {
    return [single];
  }

  return [];
}

export function keyNameModifiersPayloadToSvgIds(o: Record<string, unknown>): string[] {
  const out: string[] = [];
  const seen = new Set<string>();

  const add = (ids: string[]) => {
    for (const id of ids) {
      if (id && !seen.has(id)) {
        seen.add(id);
        out.push(id);
      }
    }
  };

  const modifiers = typeof o['modifiers'] === 'string' ? o['modifiers'] : '';
  const keyName = typeof o['key_name'] === 'string' ? o['key_name'] : '';

  for (const part of modifiers.split('+').map((s) => s.trim()).filter(Boolean)) {
    add(tokenToSvgIds(part));
  }
  if (keyName.trim()) {
    add(tokenToSvgIds(keyName));
  }

  return out;
}
