// =============================================================
// POOL — minimal object pool to avoid per-frame allocations.
// Prefer Phaser groups for sprites; this is for plain objects or
// when you want explicit control. (Game.js uses a physics group,
// which is itself a pool; this is provided as a kit primitive.)
// =============================================================
window.Pool = class Pool {
  constructor(factory, reset, size = 0) {
    this._factory = factory;     // () => newObject
    this._reset = reset || ((o) => o); // (obj, ...args) => obj
    this._free = [];
    this._active = new Set();
    for (let i = 0; i < size; i++) this._free.push(this._factory());
  }

  get(...args) {
    const obj = this._free.length ? this._free.pop() : this._factory();
    this._reset(obj, ...args);
    this._active.add(obj);
    return obj;
  }

  release(obj) {
    if (this._active.delete(obj)) this._free.push(obj);
  }

  releaseAll() {
    this._active.forEach((o) => this._free.push(o));
    this._active.clear();
  }

  get activeCount() { return this._active.size; }
  forEachActive(fn) { this._active.forEach(fn); }
};
