
// Offensive type effectiveness utilities (Gen 6+ chart).
// Given one or more attacking types, computes which defender types are hit for 2x, 0.5x, or 0x
// when using STAB moves of those types (best-of across the attack types).
//
// This is intentionally simple and stable: no abilities, items, or move-specific overrides.

const TYPE_LIST = [
  "normal","fire","water","electric","grass","ice","fighting","poison","ground","flying",
  "psychic","bug","rock","ghost","dragon","dark","steel","fairy"
];

// Multiplier when ATTACKING with atkType against DEFENDER defType.
const OFFENSE = {
  normal:  { rock:0.5, ghost:0, steel:0.5 },
  fire:    { fire:0.5, water:0.5, grass:2, ice:2, bug:2, rock:0.5, dragon:0.5, steel:2 },
  water:   { fire:2, water:0.5, grass:0.5, ground:2, rock:2, dragon:0.5 },
  electric:{ water:2, electric:0.5, grass:0.5, ground:0, flying:2, dragon:0.5 },
  grass:   { fire:0.5, water:2, grass:0.5, poison:0.5, ground:2, flying:0.5, bug:0.5, rock:2, dragon:0.5, steel:0.5 },
  ice:     { fire:0.5, water:0.5, grass:2, ice:0.5, ground:2, flying:2, dragon:2, steel:0.5 },
  fighting:{ normal:2, ice:2, rock:2, dark:2, steel:2, poison:0.5, flying:0.5, psychic:0.5, bug:0.5, ghost:0, fairy:0.5 },
  poison:  { grass:2, fairy:2, poison:0.5, ground:0.5, rock:0.5, ghost:0.5, steel:0 },
  ground:  { fire:2, electric:2, grass:0.5, poison:2, flying:0, bug:0.5, rock:2, steel:2 },
  flying:  { grass:2, fighting:2, bug:2, electric:0.5, rock:0.5, steel:0.5 },
  psychic: { fighting:2, poison:2, psychic:0.5, dark:0, steel:0.5 },
  bug:     { grass:2, psychic:2, dark:2, fire:0.5, fighting:0.5, poison:0.5, flying:0.5, ghost:0.5, steel:0.5, fairy:0.5 },
  rock:    { fire:2, ice:2, flying:2, bug:2, fighting:0.5, ground:0.5, steel:0.5 },
  ghost:   { psychic:2, ghost:2, dark:0.5, normal:0 },
  dragon:  { dragon:2, steel:0.5, fairy:0 },
  dark:    { psychic:2, ghost:2, fighting:0.5, dark:0.5, fairy:0.5 },
  steel:   { ice:2, rock:2, fairy:2, fire:0.5, water:0.5, electric:0.5, steel:0.5 },
  fairy:   { fighting:2, dragon:2, dark:2, fire:0.5, poison:0.5, steel:0.5 },
};

function mult(atk, def) {
  const row = OFFENSE[atk];
  if (!row) return 1;
  const m = row[def];
  return (m === 0 || m === 0.5 || m === 2) ? m : 1;
}

function titleCaseType(t) {
  if (!t || typeof t !== "string") return "";
  return t.charAt(0).toUpperCase() + t.slice(1);
}

// Best-of across multiple attacking types (e.g., a dual-type Pokémon's STAB options).
// For each defender type, choose the maximum multiplier among the attacking types.
export function computeOffensiveMatchups(attackingTypes) {
  const atkTypes = (attackingTypes ?? [])
    .map(t => (t ?? "").toString().trim().toLowerCase())
    .filter(Boolean);

  const best = {};
  for (const def of TYPE_LIST) {
    let b = 1;
    for (const atk of atkTypes) {
      const m = mult(atk, def);
      if (m > b) b = m;
    }
    best[def] = b;
  }

  const strong = [];
  const weak = [];
  const immune = [];

  for (const def of TYPE_LIST) {
    const m = best[def];
    if (m === 2) strong.push(def);
    else if (m === 0.5) weak.push(def);
    else if (m === 0) immune.push(def);
  }

  return {
    strong,
    weak,
    immune,
    best
  };
}

// Formats type list into lines with N items per line.
// Example (perLine=2):
// "Water, Fire,\nIce, Dragon"
export function formatTypeList(types, perLine = 2) {
  const list = (types ?? []).filter(Boolean).map(t => titleCaseType(String(t)));
  if (list.length === 0) return "—";
  const lines = [];
  for (let i = 0; i < list.length; i += perLine) {
    const chunk = list.slice(i, i + perLine).join(", ");
    // Add trailing comma for all but the last line, matching the user's example.
    const trailing = (i + perLine < list.length) ? "," : "";
    lines.push(chunk + trailing);
  }
  return lines.join("\n");
}
