import { RNG } from '../utils/RNG.js';

export class PackManager {
  constructor(packsData, cardManager) {
    this.packs = new Map(packsData.map(p => [p.id, p]));
    this.cardManager = cardManager;
  }

  getPack(id) {
    return this.packs.get(id);
  }

  getAllPacks() {
    return [...this.packs.values()];
  }

  openPack(packId, save) {
    const pack = this.getPack(packId);
    if (!pack) return null;
    const rng = new RNG(Date.now() ^ (Math.random() * 0xffffffff));
    const results = [];
    for (let i = 0; i < pack.cardCount; i++) {
      const rarity = this._rollRarity(pack.dropRates, rng);
      const pool = this.cardManager.getAllCards().filter(c =>
        c.rarity === rarity &&
        c.type !== 'commander' &&
        pack.cardPool.includes(c.faction)
      );
      if (pool.length === 0) continue;
      const card = pool[rng.nextInt(0, pool.length - 1)];
      results.push(card);
    }
    return results;
  }

  _rollRarity(dropRates, rng) {
    const r = rng.next();
    let cumulative = 0;
    for (const [rarity, rate] of Object.entries(dropRates)) {
      cumulative += rate;
      if (r < cumulative) return rarity;
    }
    return 'common';
  }
}
