import { EcrymeUtility } from "../common/ecryme-utility.js";

/* -------------------------------------------- */
export class EcrymeCombat extends Combat {
  
  /* -------------------------------------------- */
  async rollInitiative(ids, formula = undefined, messageOptions = {} ) {
    ids = typeof ids === "string" ? [ids] : ids;
    for (let cId = 0; cId < ids.length; cId++) {
      const c = this.combatants.get(ids[cId]);
      let id = c.id;
      let initBonus = c.actor ? c.actor.getInitiativeScore( this.id, id ) : -1;
      await this.updateEmbeddedDocuments("Combatant", [ { _id: id, initiative: initBonus } ]);
    }

    return this;
  }

  /* -------------------------------------------- */
  /* -------------------------------------------- */
  static async checkTurnPosition() {
    while (game.combat.turn > 0) {
      await game.combat.previousTurn()
    }
  }

  /* -------------------------------------------- */
  async _onDelete() {
    let combatants = this.combatants.contents
    for (let c of combatants) {
      let actor = game.actors.get(c.actorId)
      await actor.clearInitiative()
    }
    await super._onDelete()
  }

}
