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

class BetterGamblingIE extends BaseExtractor {
  constructor() {
    super();
    this.patterns = [/bettergambling\.ie/];
  }

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

  // imgs have alt="X Casino IE Logo" inside divs with class bonus-info-img or casino-div
  // External redirect is https://bgredir.com on every card

  function getOffsite(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 '';
  }

  for (var imgEl of document.querySelectorAll('img[alt]')) {
    var raw = (imgEl.alt || '').trim();

    // Match "Something Casino IE Logo" pattern → extract brand
    var m = raw.match(/^(.+)\s+Casino\s+IE\s+Logo$/i);
    if (!m) continue;

    var name = m[1].trim();
    if (name.length < 3 || S[name]) continue;

    // Walk up to find external link container  
    var cont = imgEl.parentElement;
    var link = '';
    while (cont && cont !== document.body) {
      link = getOffsite(cont);
      if (link) break;
      cont = cont.parentElement;  
   }

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

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

module.exports = BetterGamblingIE;