export default function EmbeddedDeltaMixin(e){return class extends e{async _preUpdate(e,t,n){if(!1===await super._preUpdate(e,t,n))return!1;const i=this.parent.getEmbeddedCollection(this.parentCollection);i.manages(this.id)||(i.set(this.id,this),t._wasAdopted=!0)}batchWrite(e,t={}){const n=this.parent.getEmbeddedCollection(this.parentCollection);if(t.restoreDelta&&n.manages(this.id))return super.batchDelete(e);const i=this.#e();i&&(t.writeParent=i),super.batchWrite(e,t)}#e(){let e=this,t=null;for(;e.parentCollection;){const n=e.parent,i=n.getEmbeddedCollection(e.parentCollection);!1===i.manages(e.id)&&(t=n,i.set(e.id,e)),e=n}return t}batchWriteParents(e,t){if(this.parent.getEmbeddedCollection(this.parentCollection).manages(this.id)){const e=this.#e();e&&(t=e)}else{e.clear();const n={writeEmbedded:!0};this.batchWrite(e,n),t=n.writeParent}super.batchWriteParents(e,t)}batchDelete(e,t={}){const n=this.parent.getEmbeddedCollection(this.parentCollection);if(t.restoreDelta&&n.isTombstone(this.id))return super.batchDelete(e);const i=new foundry.data.TombstoneData({_id:this.id}),{dbKey:a,sublevelName:s}=this;if(!n.manages(this.id)){const e=this.#e();e&&(t.writeParent=e)}this.constructor.batchWrite(i.toObject(),e,{dbKey:a,sublevelName:s})}}}