/*
All material copyright Esri, All Rights Reserved, unless otherwise specified.
See https://js.arcgis.com/5.0/esri/copyright.txt for details.
*/
import{__decorate as t}from"tslib";import{isSome as e,unique as r}from"../../core/arrayUtils.js";import s from"../../core/Error.js";import{abortHandle as i}from"../../core/handleUtils.js";import{getOrCreateMapValue as o}from"../../core/MapUtils.js";import{EsriPromise as n}from"../../core/Promise.js";import{throwIfAborted as l,isAbortError as a}from"../../core/promiseUtils.js";import u from"../../core/ReactiveMap.js";import{property as f,subclass as p}from"../../core/accessorSupport/decorators.js";import{isSubtypeSublayer as d}from"../../layers/support/layerUtils.js";import c from"./Edits.js";import{fetchFullFeaturesByObjectId as h}from"./workflowUtils.js";let m=class extends n{constructor(t){super(t),this.editorItems=[],this.featureInfos=null,this.parent=null,this.selectedFeature=null,this.sketchOptions=null,this.snappingManager=null,this.viewModel=null,this._editorItemsByLayer=new u,this._fullFeatures=new u,this._pendingEdits=new u}initialize(){const t=new AbortController;this.addResolvingPromise(this._initializeFullFeatures(t.signal).then(e=>{l(t.signal),this._initializeEdits(e)}).catch(t=>{if(a(t))throw new s("update-features-workflow:full-features-aborted","Fetching of full features was aborted.");throw t})),this.addHandles(i(t)),this._initializeEditorItemsByLayer()}get features(){return this.fullFeatures}get fullFeatures(){return Array.from(this._fullFeatures.values())}get initialFeatures(){return Array.from(this._pendingEdits.values()).map(t=>t.initialFeature).filter(e)}get layers(){const{featureInfos:t,features:s}=this;return t?.length?t.map(t=>t.layer):s.length?r(s.map(t=>t.sourceLayer).filter(e)):[]}set timeZone(t){this._set("timeZone",t),this._pendingEdits.forEach(e=>{e.timeZone=t})}getEditorItemForLayer(t){return this._editorItemsByLayer.get(t)}getFeatureTitle(t){return this._pendingEdits.get(t)?.displayTitle}getPendingEditsForFeature(t){const e=t.getObjectId();if(null!=e)return this._pendingEdits.get(this._fullFeatures.get(e)??t)}isSubmittable(t){const e=this._pendingEdits.get(t);return!!e&&(e.submittable||e.isAssociation)}stageDelete(){for(const t of this._pendingEdits.values())t.stageDelete()}_initializeEditorItemsByLayer(){for(const t of this.editorItems)this._editorItemsByLayer.set(t.layer,t)}_initializeEdits(t){const{timeZone:e}=this;for(const r of t){const t=new c({feature:r,timeZone:e});t.trackChanges(),this._pendingEdits.set(r,t)}}async _initializeFullFeatures(t){const{_fullFeatures:e,featureInfos:r,features:i,viewModel:n}=this,{spatialReference:l}=n.view,a=new Map;if(r?.length)for(const{layer:s,objectIds:o}of r)a.set(d(s)?s.parent:s,o);else if(i.length)for(const s of i){const t=s.getObjectId();if(null!=t){const e=s.sourceLayer??s.layer,r=d(e)?e.parent:e;o(a,r,()=>[]).push(t)}}const u=Array.from(a.entries()).map(async([r,i])=>{const o=await h(i,r,l,t);if(o.length!==i.length)throw new s("editor:update-features-workflow:fetch-full-features-incomplete","Failed to fetch full features for one or more of the provided features.");const n=r.objectIdField,a=(t,e)=>t.attributes[n]>e.attributes[n]?1:-1,u=i.sort((t,e)=>t-e),f=o.sort(a);for(let t=0;t<u.length;t++)e.set(u[t],f[t]);return o});return(await Promise.all(u)).flat()}};t([f({constructOnly:!0})],m.prototype,"editorItems",void 0),t([f({constructOnly:!0})],m.prototype,"featureInfos",void 0),t([f()],m.prototype,"features",null),t([f()],m.prototype,"fullFeatures",null),t([f()],m.prototype,"initialFeatures",null),t([f()],m.prototype,"layers",null),t([f()],m.prototype,"parent",void 0),t([f()],m.prototype,"selectedFeature",void 0),t([f({constructOnly:!0})],m.prototype,"sketchOptions",void 0),t([f()],m.prototype,"snappingManager",void 0),t([f()],m.prototype,"timeZone",null),t([f()],m.prototype,"viewModel",void 0),t([f()],m.prototype,"_fullFeatures",void 0),m=t([p("esri.widgets.Editor.UpdateFeaturesWorkflowData")],m);const y=m;export{y as default};
