const BaseExtractor = require('./base');

class SpaceThemeOrgArchive extends BaseExtractor {
  constructor() {
    super();
    this.patterns = [
      /bestpokiesau\.com/,
      /getbets\.us/,
      /getb8\.us/,
      /slotsspot\.com/,
      /comparasino\.com/,
    ];
  }

  getExtractCode() {
    return String.raw`
(function() {
  var HOST = location.hostname.replace('www.', '');
  var R = [], S = {};

  function offsite(node) {
    for (var a of node.querySelectorAll('a[href]')) {
      try {
        var u = new URL(a.href, document.baseURI);
        if (u.hostname.replace('www.', '') !== HOST) return u.origin;
      } catch(e) {}
    }
    return '';
  }

  // Space theme sites put casino brand logos in divs with classes containing "archive-item-brand-logo"
  for (var logoDiv of document.querySelectorAll('[class*="archive-item-brand-logo"]')) {
    var img2 = logoDiv.querySelector('img[alt]');
    if (!img2 || !img2.alt) continue;

    var raw2 = (img2.alt || '').trim();
    // Strip trailing " Casino" from alt text like "Wild Casino" -> "Wild"  
    var name2 = raw2.replace(/\s+C[aA]sino\s*$/i, '').trim();
    if (!name2 || name2.length < 2 || S[name2]) continue;
    if (/^\p{Emoji}+$/u.test(name2)) continue;

    // Walk up from brand-logo div to broader card container that holds affiliate links
    var cardCont = logoDiv.parentElement;
    while (cardCont && cardCont !== document.body) {
      var link2 = offsite(cardCont);
      if (link2) break;
      cardCont = cardCont.parentElement;
    }

    S[name2] = true;
    R.push({ name: name2, link: link2 || '', bonus: '' });
  }

  return R.slice(0, 25);
})()
`;
  }
}

module.exports = SpaceThemeOrgArchive;