/* eslint-disable no-var */
var VINTED_CATEGORIES = [
  {
    id: 5, name: 'Damen', children: [
      { id: 1904, name: 'Kleidung', children: [
        { id: 1, name: 'Tops & T-Shirts' },
        { id: 2, name: 'Pullover & Strickjacken' },
        { id: 3, name: 'Blusen & Hemden' },
        { id: 4, name: 'Kleider' },
        { id: 6, name: 'Röcke' },
        { id: 7, name: 'Hosen & Leggings' },
        { id: 8, name: 'Jeans' },
        { id: 9, name: 'Jacken & Mäntel' },
        { id: 10, name: 'Anzüge & Blazer' },
        { id: 11, name: 'Sportbekleidung' },
        { id: 12, name: 'Bademode' },
        { id: 13, name: 'Unterwäsche & Nachtwäsche' },
        { id: 14, name: 'Umstandsmode' },
        { id: 15, name: 'Overalls & Jumpsuits' },
        { id: 16, name: 'Kostüme & Verkleidungen' },
        { id: 1226, name: 'Shorts' },
        { id: 1904, name: 'Sonstige Kleidung' },
      ]},
      { id: 1905, name: 'Schuhe', children: [
        { id: 17, name: 'Sneaker' },
        { id: 18, name: 'Stiefel & Stiefeletten' },
        { id: 19, name: 'Pumps & High Heels' },
        { id: 20, name: 'Sandalen' },
        { id: 21, name: 'Ballerinas' },
        { id: 22, name: 'Schnürschuhe & Slipper' },
        { id: 23, name: 'Sportschuhe' },
        { id: 24, name: 'Hausschuhe' },
        { id: 1905, name: 'Sonstige Schuhe' },
      ]},
      { id: 1906, name: 'Taschen', children: [
        { id: 25, name: 'Handtaschen' },
        { id: 26, name: 'Umhängetaschen' },
        { id: 27, name: 'Rucksäcke' },
        { id: 28, name: 'Clutches & Abendtaschen' },
        { id: 29, name: 'Geldbörsen & Etuis' },
        { id: 30, name: 'Shopper & Tragetaschen' },
        { id: 1906, name: 'Sonstige Taschen' },
      ]},
      { id: 1907, name: 'Accessoires', children: [
        { id: 31, name: 'Schmuck' },
        { id: 32, name: 'Uhren' },
        { id: 33, name: 'Sonnenbrillen' },
        { id: 34, name: 'Schals & Tücher' },
        { id: 35, name: 'Gürtel' },
        { id: 36, name: 'Mützen & Hüte' },
        { id: 37, name: 'Handschuhe' },
        { id: 1907, name: 'Sonstige Accessoires' },
      ]},
      { id: 1919, name: 'Pflege & Kosmetik', children: [
        { id: 38, name: 'Makeup' },
        { id: 39, name: 'Hautpflege' },
        { id: 40, name: 'Haarpflege' },
        { id: 41, name: 'Parfum' },
        { id: 42, name: 'Nagelpflege' },
        { id: 1919, name: 'Sonstige Pflege' },
      ]},
    ]
  },
  {
    id: 1206, name: 'Herren', children: [
      { id: 1908, name: 'Kleidung', children: [
        { id: 50, name: 'T-Shirts & Poloshirts' },
        { id: 51, name: 'Pullover & Hoodies' },
        { id: 52, name: 'Hemden' },
        { id: 53, name: 'Hosen' },
        { id: 54, name: 'Jeans' },
        { id: 55, name: 'Shorts' },
        { id: 56, name: 'Jacken & Mäntel' },
        { id: 57, name: 'Anzüge & Sakkos' },
        { id: 58, name: 'Sportbekleidung' },
        { id: 59, name: 'Bademode' },
        { id: 60, name: 'Unterwäsche & Nachtwäsche' },
        { id: 61, name: 'Kostüme & Verkleidungen' },
        { id: 1908, name: 'Sonstige Kleidung' },
      ]},
      { id: 1909, name: 'Schuhe', children: [
        { id: 62, name: 'Sneaker' },
        { id: 63, name: 'Stiefel & Stiefeletten' },
        { id: 64, name: 'Business-Schuhe' },
        { id: 65, name: 'Sandalen' },
        { id: 66, name: 'Sportschuhe' },
        { id: 67, name: 'Hausschuhe' },
        { id: 1909, name: 'Sonstige Schuhe' },
      ]},
      { id: 1910, name: 'Taschen', children: [
        { id: 68, name: 'Rucksäcke' },
        { id: 69, name: 'Umhängetaschen' },
        { id: 70, name: 'Aktentaschen' },
        { id: 71, name: 'Geldbörsen & Etuis' },
        { id: 1910, name: 'Sonstige Taschen' },
      ]},
      { id: 1911, name: 'Accessoires', children: [
        { id: 72, name: 'Uhren' },
        { id: 73, name: 'Sonnenbrillen' },
        { id: 74, name: 'Krawatten & Fliegen' },
        { id: 75, name: 'Gürtel' },
        { id: 76, name: 'Mützen & Hüte' },
        { id: 77, name: 'Schals' },
        { id: 78, name: 'Schmuck' },
        { id: 1911, name: 'Sonstige Accessoires' },
      ]},
      { id: 1920, name: 'Pflege', children: [
        { id: 79, name: 'Rasur & Bartpflege' },
        { id: 80, name: 'Hautpflege' },
        { id: 81, name: 'Parfum' },
        { id: 1920, name: 'Sonstige Pflege' },
      ]},
    ]
  },
  {
    id: 1207, name: 'Kinder', children: [
      { id: 1912, name: 'Mädchen-Kleidung', children: [
        { id: 100, name: 'Tops & T-Shirts' },
        { id: 101, name: 'Pullover & Strickjacken' },
        { id: 102, name: 'Kleider & Röcke' },
        { id: 103, name: 'Hosen & Jeans' },
        { id: 104, name: 'Jacken & Mäntel' },
        { id: 1912, name: 'Sonstige Mädchen-Kleidung' },
      ]},
      { id: 1913, name: 'Jungen-Kleidung', children: [
        { id: 110, name: 'Tops & T-Shirts' },
        { id: 111, name: 'Pullover & Hoodies' },
        { id: 112, name: 'Hosen & Jeans' },
        { id: 113, name: 'Jacken & Mäntel' },
        { id: 1913, name: 'Sonstige Jungen-Kleidung' },
      ]},
      { id: 1914, name: 'Baby-Kleidung', children: [
        { id: 120, name: 'Strampler & Bodies' },
        { id: 121, name: 'Sets & Outfits' },
        { id: 122, name: 'Jacken' },
        { id: 1914, name: 'Sonstige Baby-Kleidung' },
      ]},
      { id: 1915, name: 'Kinderschuhe', children: [
        { id: 130, name: 'Sneaker' },
        { id: 131, name: 'Stiefel' },
        { id: 132, name: 'Sandalen' },
        { id: 133, name: 'Sportschuhe' },
        { id: 1915, name: 'Sonstige Kinderschuhe' },
      ]},
      { id: 1916, name: 'Spielzeug', children: [
        { id: 140, name: 'Puppen & Zubehör' },
        { id: 141, name: 'Fahrzeuge' },
        { id: 142, name: 'Bausteine & Baukästen' },
        { id: 143, name: 'Kuscheltiere' },
        { id: 144, name: 'Brettspiele & Puzzles' },
        { id: 145, name: 'Lernspielzeug' },
        { id: 1916, name: 'Sonstiges Spielzeug' },
      ]},
    ]
  },
  {
    id: 1208, name: 'Haus & Wohnen', children: [
      { id: 1917, name: 'Dekoration', children: [
        { id: 200, name: 'Bilder & Poster' },
        { id: 201, name: 'Kerzen & Windlichter' },
        { id: 202, name: 'Vasen' },
        { id: 203, name: 'Spiegel' },
        { id: 204, name: 'Figuren & Skulpturen' },
        { id: 1917, name: 'Sonstige Dekoration' },
      ]},
      { id: 245, name: 'Küche & Essen', children: [
        { id: 210, name: 'Geschirr' },
        { id: 211, name: 'Gläser & Tassen' },
        { id: 212, name: 'Besteck' },
        { id: 213, name: 'Küchengeräte' },
        { id: 214, name: 'Aufbewahrung' },
      ]},
      { id: 246, name: 'Textilien', children: [
        { id: 220, name: 'Bettwäsche' },
        { id: 221, name: 'Handtücher' },
        { id: 222, name: 'Kissen & Decken' },
        { id: 223, name: 'Teppiche' },
        { id: 224, name: 'Vorhänge' },
      ]},
      { id: 247, name: 'Möbel', children: [
        { id: 230, name: 'Regale' },
        { id: 231, name: 'Tische' },
        { id: 232, name: 'Stühle' },
        { id: 233, name: 'Schränke & Kommoden' },
        { id: 234, name: 'Betten' },
      ]},
      { id: 248, name: 'Garten', children: [
        { id: 240, name: 'Gartenmöbel' },
        { id: 241, name: 'Pflanzen & Töpfe' },
        { id: 242, name: 'Grill & Outdoor' },
        { id: 243, name: 'Gartengeräte' },
      ]},
    ]
  },
  {
    id: 1209, name: 'Unterhaltung', children: [
      { id: 249, name: 'Bücher', children: [
        { id: 300, name: 'Romane & Erzählungen' },
        { id: 301, name: 'Sachbücher' },
        { id: 302, name: 'Kinder- & Jugendbücher' },
        { id: 303, name: 'Comics & Manga' },
        { id: 304, name: 'Fach- & Schulbücher' },
      ]},
      { id: 250, name: 'Musik & Film', children: [
        { id: 310, name: 'CDs' },
        { id: 311, name: 'Vinyl & Schallplatten' },
        { id: 312, name: 'DVDs & Blu-ray' },
      ]},
      { id: 251, name: 'Spiele & Konsolen', children: [
        { id: 320, name: 'Videospiele' },
        { id: 321, name: 'Konsolen' },
        { id: 322, name: 'Zubehör' },
        { id: 323, name: 'Brettspiele' },
      ]},
      { id: 252, name: 'Sport & Freizeit', children: [
        { id: 330, name: 'Fitnessgeräte' },
        { id: 331, name: 'Fahrräder & Zubehör' },
        { id: 332, name: 'Camping & Wandern' },
        { id: 333, name: 'Wintersport' },
        { id: 334, name: 'Ballsport' },
      ]},
    ]
  },
  {
    id: 1210, name: 'Elektronik & Technik', children: [
      { id: 253, name: 'Handys & Smartphones', children: [
        { id: 400, name: 'iPhones' },
        { id: 401, name: 'Samsung' },
        { id: 402, name: 'Sonstige Smartphones' },
        { id: 403, name: 'Handyhüllen & -zubehör' },
      ]},
      { id: 254, name: 'Computer & Tablets', children: [
        { id: 410, name: 'Laptops' },
        { id: 411, name: 'Tablets' },
        { id: 412, name: 'PCs & Desktops' },
        { id: 413, name: 'Monitore' },
        { id: 414, name: 'Netzwerkspeicher (NAS)' },
        { id: 415, name: 'Server' },
        { id: 416, name: 'Drucker & Scanner' },
        { id: 417, name: 'PC-Zubehör' },
      ]},
      { id: 255, name: 'Foto & Kamera', children: [
        { id: 420, name: 'Digitalkameras' },
        { id: 421, name: 'Objektive' },
        { id: 422, name: 'Kamerazubehör' },
      ]},
      { id: 256, name: 'Audio & HiFi', children: [
        { id: 430, name: 'Kopfhörer' },
        { id: 431, name: 'Lautsprecher' },
        { id: 432, name: 'HiFi-Anlagen' },
      ]},
      { id: 257, name: 'TV & Video', children: [
        { id: 440, name: 'Fernseher' },
        { id: 441, name: 'Streaming-Geräte' },
        { id: 442, name: 'Beamer' },
      ]},
      { id: 258, name: 'Kabel & Zubehör', children: [
        { id: 450, name: 'Kabel' },
        { id: 451, name: 'Adapter' },
        { id: 452, name: 'Speicherkarten & USB-Sticks' },
        { id: 453, name: 'Ladegeräte & Akkus' },
      ]},
    ]
  },
  {
    id: 1211, name: 'Haustiere', children: [
      { id: 259, name: 'Hunde', children: [
        { id: 500, name: 'Hundeleine & Halsband' },
        { id: 501, name: 'Hundebett & Decke' },
        { id: 502, name: 'Hundespielzeug' },
        { id: 503, name: 'Hundebekleidung' },
      ]},
      { id: 260, name: 'Katzen', children: [
        { id: 510, name: 'Kratzbäume' },
        { id: 511, name: 'Katzenspielzeug' },
        { id: 512, name: 'Katzenbett' },
      ]},
      { id: 261, name: 'Sonstige Haustiere' },
    ]
  },
];

function flattenCategories(tree, path) {
  const result = [];
  path = path || [];
  for (const cat of tree) {
    const currentPath = [...path, cat.name];
    if (cat.children) {
      for (const sub of cat.children) {
        const subPath = [...currentPath, sub.name];
        if (sub.children) {
          for (const leaf of sub.children) {
            result.push({
              id: leaf.id,
              name: leaf.name,
              path: [...subPath, leaf.name],
              pathStr: [...subPath, leaf.name].join(' > '),
              search: [...subPath, leaf.name].join(' ').toLowerCase(),
            });
          }
        } else {
          result.push({
            id: sub.id,
            name: sub.name,
            path: subPath,
            pathStr: subPath.join(' > '),
            search: subPath.join(' ').toLowerCase(),
          });
        }
      }
    }
  }
  return result;
}

var VINTED_CATEGORIES_FLAT = flattenCategories(VINTED_CATEGORIES);

function matchCategory(query) {
  if (!query) return [];
  const q = query.toLowerCase().trim();
  const words = q.split(/\s+/);
  const scored = VINTED_CATEGORIES_FLAT.map(cat => {
    let score = 0;
    const s = cat.search;
    if (s.includes(q)) score += 100;
    for (const w of words) {
      if (w.length < 2) continue;
      if (s.includes(w)) score += 20;
      if (cat.name.toLowerCase().includes(w)) score += 30;
    }
    // Bonus for exact leaf match
    if (cat.name.toLowerCase() === q) score += 200;
    return { ...cat, score };
  }).filter(c => c.score > 0);
  scored.sort((a, b) => b.score - a.score);
  return scored.slice(0, 15);
}
