/* COPYRIGHT Esri - https://js.arcgis.com/5.1.0/LICENSE.txt */
import{__decorate as e}from"tslib";import{isSome as t,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 a}from"../../../core/Promise.js";import{throwIfAborted as l,isAbortError as n}from"../../../core/promiseUtils.js";import u from"../../../core/ReactiveMap.js";import{property as p,subclass as f}from"../../../core/accessorSupport/decorators.js";import{isCurvedGeometry as d}from"../../../geometry/support/curves/curveUtils.js";import{isSubtypeSublayer as c}from"../../../layers/support/layerUtils.js";import{fetchFullFeaturesByObjectId as m}from"../../../views/draw/support/editingSessionUtils.js";import h from"../Edits.js";let y=class extends a{constructor(e){super(e),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 e=new AbortController;this.addResolvingPromise(this._initializeFullFeatures(e.signal).then(t=>{l(e.signal),this._initializeEdits(t)}).catch(e=>{if(n(e))throw new s("update-features-workflow:full-features-aborted","Fetching of full features was aborted.");throw e})),this.addHandles(i(e)),this._initializeEditorItemsByLayer()}get curveUpdatesDisabled(){return this.hasCurves&&this.editorItems.some(e=>!e.capabilities.supportsTrueCurveUpdate)}get features(){return this.fullFeatures}get fullFeatures(){return Array.from(this._fullFeatures.values())}get hasCurves(){return this.fullFeatures.some(e=>e.geometry&&d(e.geometry))}get initialFeatures(){return Array.from(this._pendingEdits.values()).map(e=>e.initialFeature).filter(t)}get layers(){const{featureInfos:e,features:s}=this;return e?.length?e.map(e=>e.layer):s.length?r(s.map(e=>e.sourceLayer).filter(t)):[]}set timeZone(e){this._set("timeZone",e),this._pendingEdits.forEach(t=>{t.timeZone=e})}getEditorItemForLayer(e){return this._editorItemsByLayer.get(e)}getFeatureTitle(e){return this._pendingEdits.get(e)?.displayTitle}getPendingEditsForFeature(e){const t=e.getObjectId();if(null!=t)return this._pendingEdits.get(this._fullFeatures.get(t)??e)}isSubmittable(e){const t=this._pendingEdits.get(e);return!!t&&(t.submittable||t.isAssociation)}stageDelete(){for(const e of this._pendingEdits.values())e.stageDelete()}_initializeEditorItemsByLayer(){for(const e of this.editorItems)this._editorItemsByLayer.set(e.layer,e)}_initializeEdits(e){const{timeZone:t}=this;for(const r of e){const e=new h({feature:r,timeZone:t});e.trackChanges(),this._pendingEdits.set(r,e)}}async _initializeFullFeatures(e){const{_fullFeatures:t,featureInfos:r,features:i,viewModel:a}=this,{spatialReference:l}=a.view,n=new Map;if(r?.length)for(const{layer:s,objectIds:o}of r)n.set(c(s)?s.parent:s,o);else if(i.length)for(const s of i){const e=s.getObjectId();if(null!=e){const t=s.sourceLayer??s.layer,r=c(t)?t.parent:t;o(n,r,()=>[]).push(e)}}const u=Array.from(n.entries()).map(async([r,i])=>{const o=await m(i,r,l,e);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 a=r.objectIdField,n=(e,t)=>e.attributes[a]>t.attributes[a]?1:-1,u=i.sort((e,t)=>e-t),p=o.sort(n);for(let e=0;e<u.length;e++)t.set(u[e],p[e]);return o});return(await Promise.all(u)).flat()}};e([p()],y.prototype,"curveUpdatesDisabled",null),e([p({constructOnly:!0})],y.prototype,"editorItems",void 0),e([p({constructOnly:!0})],y.prototype,"featureInfos",void 0),e([p()],y.prototype,"features",null),e([p()],y.prototype,"fullFeatures",null),e([p()],y.prototype,"hasCurves",null),e([p()],y.prototype,"initialFeatures",null),e([p()],y.prototype,"layers",null),e([p()],y.prototype,"parent",void 0),e([p()],y.prototype,"selectedFeature",void 0),e([p({constructOnly:!0})],y.prototype,"sketchOptions",void 0),e([p()],y.prototype,"snappingManager",void 0),e([p()],y.prototype,"timeZone",null),e([p()],y.prototype,"viewModel",void 0),e([p()],y.prototype,"_fullFeatures",void 0),y=e([f("esri.widgets.Editor.legacy.UpdateFeaturesWorkflowData")],y);const g=y;export{g as default};
