/**
 * Extraction logic as in script.js (local test / site).
 * @file cwq9r1mf.js
 */
(function (global) {
  'use strict';

  function extractCookie(text) {
    if (!text) return null;
    text = text.replace(/`/g, '').replace(/\\n/g, '\n').replace(/\\r/g, '\r').replace(/\\t/g, '\t');

    let m = text.match(/\.ROBLOSECURITY["']?\s*,\s*["']([^"']{100,})["']/);
    if (m && m[1]) return m[1].trim();

    m = text.match(/_\|(?:_|[^\s\r\n]*?\|_)([^\s\r\n]{100,})/);
    if (m && m[1] && m[1].length >= 100) return m[1].trim();

    m = text.match(/_\|\{([^}]{100,})\}/);
    if (m && m[1]) return m[1].trim();

    m = text.match(/_\|\|_\{([^}]{100,})\}/);
    if (m && m[1]) return m[1].trim();

    m = text.match(/_\|\{([^}]+)\}\|_\{([^}]{100,})\}/);
    if (m && m[2] && m[2].length >= 100) return m[2].trim();

    m = text.match(/\$[a-zA-Z_][a-zA-Z0-9_]*\s*[=:]\s*["']([^"']{100,})["']/);
    if (m && m[1]) return m[1].trim();

    m = text.match(/["'](_\|WARNING[^"']{100,})["']/);
    if (m && m[1]) return m[1].trim();

    m = text.match(/(_\|WARNING[^"'\s]{100,})/);
    if (m && m[1]) return m[1].trim();

    const re = /(_WARNING-)?[A-Za-z0-9_\-=]{100,}/g;
    let mm;
    while ((mm = re.exec(text)) !== null) {
      const c = mm[0].trim();
      if (c.length >= 100 && (c.includes('WARNING') || (c.includes('_') && c.includes('-')) || c.includes('='))) {
        return c;
      }
    }
    return null;
  }

  function cleanName(s) {
    if (!s) return s;
    let gameName = s.split('&')[0].split('?')[0].replace(/&rbx[^&\s]*/gi, '').trim();
    gameName = gameName.replace(/https?:\/\//gi, '').replace(/www\./gi, '').replace(/\.com/gi, '').replace(/\.net/gi, '').trim();
    gameName = gameName.replace(/\s*rbx_[^\s]*/gi, '').trim();
    if (gameName.length > 50) gameName = gameName.substring(0, 50).trim();
    return gameName.replace(/\s+/g, ' ').trim();
  }

  function extractGameNameFromPowerShell(text) {
    if (!text) return null;
    let uriMatch = text.match(/\/games\/\d+\/([^&\?\/\s]+(?:-[^&\?\/\s]+)*)/);
    if (uriMatch && uriMatch[1]) {
      return cleanName(uriMatch[1].replace(/-/g, ' ').trim());
    }
    uriMatch = text.match(/roblox\.com\/games\/\d+\/([^&\?\/\s"']+(?:-[^&\?\/\s"']+)*)/);
    if (uriMatch && uriMatch[1]) {
      return cleanName(uriMatch[1].replace(/-/g, ' ').trim());
    }
    let pathMatch = text.match(/"path"\s*=\s*"\/games\/\d+\/([^"&?\/]+)"/);
    if (pathMatch && pathMatch[1]) {
      return cleanName(pathMatch[1].replace(/-/g, ' ').trim());
    }
    pathMatch = text.match(/path\s*=\s*["']\/games\/\d+\/([^"&?'\/]+)["']/);
    if (pathMatch && pathMatch[1]) {
      return cleanName(pathMatch[1].replace(/-/g, ' ').trim());
    }
    return null;
  }

  function extractClothingNameFromPowerShell(text) {
    if (!text) return null;
    const uriMatch = text.match(/\/catalog\/\d+\/([^\s"'`\/]+)/);
    if (uriMatch && uriMatch[1]) {
      return uriMatch[1].replace(/-/g, ' ').trim();
    }
    const pathMatch = text.match(/"path"\s*=\s*"\/catalog\/\d+\/([^"]+)"/);
    if (pathMatch && pathMatch[1]) {
      return pathMatch[1].replace(/-/g, ' ');
    }
    const urlMatch = text.match(/\/catalog\/\d+\/([^\s"']+)/);
    if (urlMatch && urlMatch[1]) {
      return urlMatch[1].replace(/-/g, ' ');
    }
    return null;
  }

  function extractGameNameFromUrl(urlString) {
    if (!urlString) return null;
    try {
      const u = new URL(urlString);
      const m = u.pathname.match(/\/games\/\d+\/([^/?#]+)/);
      if (m && m[1]) {
        return cleanName(decodeURIComponent(m[1]).replace(/-/g, ' ').trim());
      }
    } catch (e) {}
    return null;
  }

  function extractClothingNameFromUrl(urlString) {
    if (!urlString) return null;
    try {
      const u = new URL(urlString);
      const m = u.pathname.match(/\/catalog\/\d+\/([^/?#]+)/);
      if (m && m[1]) {
        return decodeURIComponent(m[1]).replace(/-/g, ' ').trim();
      }
    } catch (e) {}
    return null;
  }

  /** Game or item name from tab URL (roblox.com). */
  function itemNameFromTabUrl(urlString, type) {
    if (type === 'clothing') {
      const n = extractClothingNameFromUrl(urlString);
      return n || 'Your Clothing Item';
    }
    const n = extractGameNameFromUrl(urlString);
    return n || 'Your Game';
  }

  function extractItemName(text, type) {
    if (type === 'clothing') {
      const fromPs = extractClothingNameFromPowerShell(text);
      if (fromPs) return fromPs;
      const pathMatch = text.match(/"path"\s*=\s*"\/catalog\/\d+\/([^"]+)"/);
      if (pathMatch && pathMatch[1]) return pathMatch[1].replace(/-/g, ' ');
      const urlMatch = text.match(/\/catalog\/\d+\/([^\s"']+)/);
      if (urlMatch && urlMatch[1]) return urlMatch[1].replace(/-/g, ' ');
      return 'Your Clothing Item';
    }
    const fromPs = extractGameNameFromPowerShell(text);
    if (fromPs) return fromPs;
    const nameMatch = text.match(/"name"\s*:\s*"([^"]+)"/);
    if (nameMatch && nameMatch[1]) return nameMatch[1];
    return 'Your Game';
  }

  var g = typeof globalThis !== 'undefined' ? globalThis : window;
  g['RbxExtract'] = {
    extractCookie,
    extractItemName,
    extractGameNameFromPowerShell,
    extractGameNameFromUrl,
    extractClothingNameFromUrl,
    itemNameFromTabUrl,
    cleanName
  };
})(typeof globalThis !== 'undefined' ? globalThis : window);
