/* COPYRIGHT Esri - https://js.arcgis.com/5.1.2/LICENSE.txt */
import e from"../../core/Error.js";import{getOrCreateMapValue as t}from"../../core/MapUtils.js";class s{constructor(e,t,s){this.changes=new Map,this.isOpen=!0,this.priorInvalidFeatures=new Set,this.viewModel=e,this.originActor=t,this.originField=s,this.priorInvalidFeatures=new Set(e.invalidFeatures)}get hasChanges(){return this.changes.size>0}async close(){if(this._assertIsOpen(),this.isOpen=!1,0!==this.changes.size){"single"!==this.viewModel.mode&&1!==this.viewModel.features.length||this.viewModel.validateContingentValues(this.changes.keys(),!0),this._reconcileValidityTracking(),this.viewModel.emit("values-change",this._makeEventPayload());for(const e of this._makeDeprecatedEventPayloads())this.viewModel.emit("value-change",e)}}setValue(e,s,i){if(this._assertIsOpen(),e.getAttribute(s)===i)return;e.setAttribute(s,i);t(this.changes,e,()=>new Map).set(s,i)}_assertIsOpen(){if(!this.isOpen)throw new e("batch-attribute-form:transaction-closed","Operation cannot be performed on a closed transaction.")}_makeEventPayload(){return{data:Array.from(this.changes.entries()).map(([e,t])=>({feature:e.source,changes:Array.from(t.entries()).map(([e,t])=>({fieldName:e,value:t}))})),fieldName:this.originField,name:"values-change",source:this.originActor}}_makeDeprecatedEventPayloads(){const e=this.originField;if(!e)return[];const s=new Map;for(const i of this.changes.keys()){t(s,i.getAttribute(e),()=>[]).push(i.source)}return Array.from(s.entries()).map(([t,s])=>({features:s,fieldName:e,name:"value-change",value:t}))}_reconcileValidityTracking(){if("expression"===this.originActor)return;const e=new Set(this.viewModel.invalidFeatures);for(const t of this.changes.keys()){const s=t.source,i=this.priorInvalidFeatures.has(s),a=e.has(s);i!==a&&(t.userChangesHaveMadeInvalid=a)}}}export{s as ValueChangeTransaction};
