// Converts an Eldorado notification JSON into a clean, display-ready object.
// Multi-game: gameId 17 = League of Legends, 32 = Valorant.
// IMPORTANT: boostingCategoryId differs per game, so the category TYPE is derived
// from gameCategoryTitle (reliable across games), not the numeric id.
(function (root) {
  "use strict";

  const GAME_BY_ID = { "17": "lol", "32": "valorant" };
  const GAME_NAME = { lol: "League of Legends", valorant: "Valorant" };

  // Normalize the human title into a type the pricing engine understands.
  function categoryType(title) {
    const t = String(title || "").toLowerCase();
    if (/net\s*win/.test(t)) return "netwins";
    if (/placement/.test(t)) return "placements";
    if (/custom/.test(t)) return "custom";
    if (/rank boost|radiant boost|boost/.test(t)) return "rank"; // Radiant Boost = climb to Radiant
    return "other";
  }

  function parseBoostNotification(item) {
    const n = item && item.notification;
    if (!n || n.event !== "BoostingRequestCreated") return null;

    const d = n.details || {};
    const c = n.customNotificationData || {};
    const fields = Array.isArray(c.requestFields) ? c.requestFields.slice() : [];
    const categoryId = c.boostingCategoryId != null ? String(c.boostingCategoryId) : null;
    const gameId = String(d.gameId || c.gameId || "");
    const game = GAME_BY_ID[gameId] || (gameId ? "game" + gameId : "unknown");
    const title = d.gameCategoryTitle || "Boost";
    const type = categoryType(title);

    // Region is ALWAYS the last requestField (full name for LoL, short code for Valorant).
    let region = null, currentRank = null, targetRank = null, extra = null;
    if (fields.length) region = fields[fields.length - 1];
    const rankFields = fields.slice(0, -1);

    if (type === "rank") {
      currentRank = rankFields[0] || null;
      targetRank = rankFields[1] || null;
    } else if (type === "netwins" || type === "placements") {
      currentRank = rankFields[0] || null;
    } else {
      // custom / other: freeform text (may carry rank info as plain text)
      const dv = c.descriptionValues || {};
      extra = rankFields.join(", ") || Object.values(dv).join(", ") || null;
    }

    return {
      id: n.id,
      detailsId: d.detailsId || null,
      buyer: d.buyerUsername || null,
      seller: d.sellerUsername || null,
      gameId,
      game, // "lol" | "valorant" | ...
      gameName: GAME_NAME[game] || game,
      categoryId, // raw, game-specific
      category: title, // display title
      type, // normalized: rank | netwins | placements | custom | other
      currentRank,
      targetRank,
      region,
      extra,
      fields,
      date: n.notificationDate || null,
      readStatus: n.notificationReadStatus || null,
    };
  }

  function parseNotificationsResponse(json) {
    const results = (json && json.results) || [];
    const out = [];
    for (const item of results) {
      const p = parseBoostNotification(item);
      if (p) out.push(p);
    }
    return out;
  }

  const api = { parseBoostNotification, parseNotificationsResponse, categoryType, GAME_BY_ID, GAME_NAME };
  if (typeof module !== "undefined" && module.exports) module.exports = api;
  root.EldoradoParse = api;
})(typeof globalThis !== "undefined" ? globalThis : this);
