/* core/openings.js — compact opening book for name detection
   (longest-prefix match on SAN), ES module. */
'use strict';

export const BOOK = {
    'e4': "King's Pawn Opening",
    'e4 c5': 'Sicilian Defense',
    'e4 c5 Nf3': 'Sicilian Defense',
    'e4 c5 Nf3 d6': 'Sicilian: Najdorf setup',
    'e4 c5 Nf3 d6 d4 cxd4 Nxd4 Nf6 Nc3 a6': 'Sicilian: Najdorf Variation',
    'e4 c5 Nf3 d6 d4 cxd4 Nxd4 Nf6 Nc3 g6': 'Sicilian: Dragon Variation',
    'e4 c5 Nf3 Nc6': 'Sicilian: Old Sicilian',
    'e4 c5 Nf3 Nc6 d4 cxd4 Nxd4 g6': 'Sicilian: Accelerated Dragon',
    'e4 c5 Nf3 e6': 'Sicilian: French Variation',
    'e4 c5 Nf3 e6 d4 cxd4 Nxd4 Nc6': 'Sicilian: Taimanov Variation',
    'e4 c5 c3': 'Sicilian: Alapin Variation',
    'e4 c5 Nc3': 'Sicilian: Closed',
    'e4 e5': "Open Game",
    'e4 e5 Nf3': "King's Knight Opening",
    'e4 e5 Nf3 Nc6': "Open Game",
    'e4 e5 Nf3 Nc6 Bb5': 'Ruy Lopez',
    'e4 e5 Nf3 Nc6 Bb5 a6': 'Ruy Lopez: Morphy Defense',
    'e4 e5 Nf3 Nc6 Bb5 a6 Ba4 Nf6 O-O Be7': 'Ruy Lopez: Closed',
    'e4 e5 Nf3 Nc6 Bb5 a6 Bxc6': 'Ruy Lopez: Exchange Variation',
    'e4 e5 Nf3 Nc6 Bb5 Nf6': 'Ruy Lopez: Berlin Defense',
    'e4 e5 Nf3 Nc6 Bc4': 'Italian Game',
    'e4 e5 Nf3 Nc6 Bc4 Bc5': 'Italian: Giuoco Piano',
    'e4 e5 Nf3 Nc6 Bc4 Bc5 b4': 'Italian: Evans Gambit',
    'e4 e5 Nf3 Nc6 Bc4 Nf6': 'Italian: Two Knights Defense',
    'e4 e5 Nf3 Nc6 d4': 'Scotch Game',
    'e4 e5 Nf3 Nc6 d4 exd4 Nxd4': 'Scotch Game',
    'e4 e5 Nf3 Nc6 Nc3': 'Three Knights Game',
    'e4 e5 Nf3 Nc6 Nc3 Nf6': 'Four Knights Game',
    'e4 e5 Nf3 Nf6': 'Petrov Defense',
    'e4 e5 Nf3 d6': 'Philidor Defense',
    'e4 e5 f4': "King's Gambit",
    'e4 e5 f4 exf4': "King's Gambit Accepted",
    'e4 e5 Bc4': "Bishop's Opening",
    'e4 e5 Nc3': 'Vienna Game',
    'e4 e6': 'French Defense',
    'e4 e6 d4 d5': 'French Defense',
    'e4 e6 d4 d5 Nc3 Bb4': 'French: Winawer Variation',
    'e4 e6 d4 d5 Nc3 Nf6': 'French: Classical Variation',
    'e4 e6 d4 d5 e5': 'French: Advance Variation',
    'e4 e6 d4 d5 exd5': 'French: Exchange Variation',
    'e4 e6 d4 d5 Nd2': 'French: Tarrasch Variation',
    'e4 c6': 'Caro-Kann Defense',
    'e4 c6 d4 d5': 'Caro-Kann Defense',
    'e4 c6 d4 d5 Nc3 dxe4': 'Caro-Kann: Classical',
    'e4 c6 d4 d5 e5': 'Caro-Kann: Advance Variation',
    'e4 c6 d4 d5 exd5 cxd5': 'Caro-Kann: Exchange Variation',
    'e4 d5': 'Scandinavian Defense',
    'e4 d5 exd5 Qxd5': 'Scandinavian: Main Line',
    'e4 d6': 'Pirc Defense',
    'e4 d6 d4 Nf6 Nc3 g6': 'Pirc Defense',
    'e4 g6': 'Modern Defense',
    'e4 Nf6': 'Alekhine Defense',
    'e4 Nc6': 'Nimzowitsch Defense',
    'e4 b6': 'Owen Defense',
    'd4': "Queen's Pawn Opening",
    'd4 d5': 'Closed Game',
    'd4 d5 c4': "Queen's Gambit",
    'd4 d5 c4 dxc4': "Queen's Gambit Accepted",
    'd4 d5 c4 e6': "Queen's Gambit Declined",
    'd4 d5 c4 e6 Nc3 Nf6 Bg5': 'QGD: Classical',
    'd4 d5 c4 c6': 'Slav Defense',
    'd4 d5 c4 c6 Nf3 Nf6 Nc3 e6': 'Semi-Slav Defense',
    'd4 d5 Nf3 Nf6 e3': 'Colle System',
    'd4 d5 Bf4': 'London System',
    'd4 d5 Nf3 Nf6 Bf4': 'London System',
    'd4 Nf6': 'Indian Defense',
    'd4 Nf6 c4 e6': 'Indian: East Indian',
    'd4 Nf6 c4 e6 Nc3 Bb4': 'Nimzo-Indian Defense',
    'd4 Nf6 c4 e6 Nf3 b6': "Queen's Indian Defense",
    'd4 Nf6 c4 e6 g3': 'Catalan Opening',
    'd4 Nf6 c4 g6': "King's Indian Defense",
    'd4 Nf6 c4 g6 Nc3 d5': 'Grünfeld Defense',
    'd4 Nf6 c4 g6 Nc3 Bg7': "King's Indian Defense",
    'd4 Nf6 c4 c5': 'Benoni Defense',
    'd4 Nf6 c4 c5 d5 b5': 'Benko Gambit',
    'd4 Nf6 Bg5': 'Trompowsky Attack',
    'd4 Nf6 Nf3 g6': "King's Indian setup",
    'd4 f5': 'Dutch Defense',
    'd4 e6': 'Horwitz Defense',
    'd4 d6': 'Pirc setup',
    'd4 g6': 'Modern Defense',
    'c4': 'English Opening',
    'c4 e5': 'English: Reversed Sicilian',
    'c4 c5': 'English: Symmetrical',
    'c4 Nf6': 'English: Anglo-Indian',
    'c4 e6': 'English: Agincourt Defense',
    'Nf3': 'Réti Opening',
    'Nf3 d5': 'Réti Opening',
    'Nf3 d5 c4': 'Réti Gambit',
    'Nf3 Nf6 c4 g6': 'English/KID setup',
    'g3': "King's Fianchetto Opening",
    'b3': 'Nimzo-Larsen Attack',
    'f4': "Bird's Opening",
    'Nc3': 'Van Geet Opening',
    'b4': 'Polish Opening',
    'e4 e5 Nf3 Nc6 Bb5 a6 Ba4': 'Ruy Lopez: Morphy Defense',
    'e4 e5 Nf3 Nc6 Bb5 a6 Ba4 Nf6': 'Ruy Lopez: Morphy Defense',
    'e4 e5 d4': 'Center Game',
    'e4 e5 d4 exd4 c3': 'Danish Gambit',
    'd4 d5 e4': 'Blackmar-Diemer Gambit',
    'e4 e5 Nf3 f5': 'Latvian Gambit',
    'e4 f5': 'Fred Defense',
    'f3': 'Barnes Opening',
    'g4': 'Grob Opening',
};

// sans: array of SAN strings (with +/# suffixes ok)
export function openingName(sans) {
  let name = null;
  let key = '';
  for (let i = 0; i < Math.min(sans.length, 12); i++) {
    key += (i ? ' ' : '') + sans[i].replace(/[+#]/g, '');
    if (BOOK[key]) name = BOOK[key];
  }
  return name;
}
