import BaseCombat from"../../../common/documents/combat.mjs";import ServerDocumentMixin from"../backend/server-document.mjs";export default class Combat extends(ServerDocumentMixin(BaseCombat)){static get collection(){return"combat"}static async _preCreateOperation(e,t,a){let i=!1;for(const e of t.data)!0===e.active&&(i&&(e.active=!1),i=!0);return super._preCreateOperation(e,t,a)}static async _onCreateOperation(e,t,a){const i=t.data.find((e=>e.active))?._id;if(i){const e=await this.sublevel.findUpdate({_id__ne:i,active:!0},{active:!1});e.length&&db.DatabaseBackend.emit(this.documentName,"update",e.map((e=>({_id:e._id,active:!1}))),{},a)}return super._onCreateOperation(e,t,a)}static async _preUpdateOperation(e,t,a){const i=t._updateData;let n=!1;for(const e of Object.values(i))!0===e.active&&(n?e.active=!1:n=!0);if(n){const t=await this.find({active:!0});for(const a of t)a.id in i||(i[a.id]={active:!1},e.push(a))}return super._preUpdateOperation(e,t,a)}static async _onUpdateOperation(e,t,a){if(await super._onUpdateOperation(e,t,a),Number.isNumeric(t.advanceTime)&&!t.worldTime&&(t.worldTime={delta:t.advanceTime},delete t.advanceTime),"object"==typeof t.worldTime){const{delta:e,...i}=t.worldTime;Number.isNumeric(e)&&await db.Setting.advanceTime(e,i,a)}}static async _onDeleteScenes(e,t,a){if(t.pack)return;const i=e.map((e=>e._id)),n=(await this.sublevel.findDelete({scene__in:i})).map((e=>e._id));db.DatabaseBackend.emit(this.documentName,"delete",n,{},a)}}