/* COPYRIGHT Esri - https://js.arcgis.com/5.1.2/LICENSE.txt */
import{__decorate as e}from"tslib";import i from"../../../core/Accessor.js";import{createTask as t}from"../../../core/asyncUtils.js";import{makeHandle as r,handlesGroup as s}from"../../../core/handleUtils.js";import a from"../../../core/Logger.js";import{getOrCreateMapValue as o}from"../../../core/MapUtils.js";import{throwIfAborted as n}from"../../../core/promiseUtils.js";import{whenOnce as d,watch as p}from"../../../core/reactiveUtils.js";import{addMaybe as c}from"../../../core/SetUtils.js";import{property as h,subclass as l}from"../../../core/accessorSupport/decorators.js";import{UpdatingHandles as u}from"../../../core/support/UpdatingHandles.js";import y from"../../../layers/GraphicsLayer.js";import{isSubtypeSublayer as m,isKnowledgeGraphSublayer as g}from"../../../layers/support/layerUtils.js";import{getDisplayedSymbol as f}from"../../../symbols/support/symbolUtils.js";import{GraphicState as v}from"../../3d/layers/graphics/GraphicState.js";const _=()=>a.getLogger("esri.views.draw.support.ConnectedReshapeManager"),w=Symbol();let G=class extends i{constructor(e){super(e),this.activeOperationType=null,this.providerFactories=[],this.view=null,this._activeProviders=new Set,this._graphics=new Map,this._modifiedFeatures=new Map,this._modifiedGraphicsLayer=new y({listMode:"hide",internal:!0,title:"Connected Reshape Modified Graphics (Internal)"}),this._pendingGraphicsLayer=new y({listMode:"hide",internal:!0,title:"Connected Reshape Pending Graphics (Internal)"}),this._pendingFeatures=new Map,this._updatingHandles=new u}initialize(){this.view.map.addMany([this._modifiedGraphicsLayer,this._pendingGraphicsLayer])}destroy(){this.view.destroyed||this.view.map.removeMany([this._modifiedGraphicsLayer,this._pendingGraphicsLayer]),this._modifiedGraphicsLayer.destroy(),this._pendingGraphicsLayer.destroy(),this._updatingHandles.destroy()}get updating(){return this._updatingHandles.updating||Array.from(this._activeProviders).some(e=>e.updating)}startFeatureMove(e){this.activeOperationType=0,this._activeProviders.clear();for(const i of this.providerFactories)c(this._activeProviders,i.getFeatureReshapeProvider(e))}startVertexMove(e,i){this.activeOperationType=1,this._activeProviders.clear();for(const t of this.providerFactories)c(this._activeProviders,t.getVertexReshapeProvider(e,i))}finish(){this.activeOperationType=null,this._activeProviders.clear();for(const[e,i]of this._pendingFeatures){this._pendingFeatures.delete(e),this._modifiedFeatures.set(e,i);const t=this._graphics.get(e);t&&(this._modifiedGraphicsLayer.add(t),this._pendingGraphicsLayer.remove(t))}}translate(e,i,t=0){if(null===this.activeOperationType)return;const r=b(e),s=b(i),a=b(t);for(const o of this._activeProviders){const e=o.translate(r,s,a);this._update(e)}}async _initializeGraphic(e,i){e.symbol??=await this._updatingHandles.addPromise(f(e,{scale:this.view.scale,useSourceLayer:!0})),this._pendingFeatures.has(i.uniqueId)?this._pendingGraphicsLayer.add(e):this._modifiedFeatures.has(i.uniqueId)&&this._modifiedGraphicsLayer.add(e),this.addHandles(await L(i.originalGraphic,e,this.view),w)}_update(e){for(const i of e){const e=this._modifiedFeatures.delete(i.uniqueId);this._pendingFeatures.set(i.uniqueId,i);const t=o(this._graphics,i.uniqueId,()=>{const e=i.graphic.cloneShallow();return this._initializeGraphic(e,i),e});t.geometry=i.graphic.geometry,e&&(this._modifiedGraphicsLayer.remove(t),this._pendingGraphicsLayer.add(t))}}get test(){return{activeProviders:this._activeProviders,modifiedFeatures:this._modifiedFeatures,modifiedGraphicsLayer:this._modifiedGraphicsLayer,pendingFeatures:this._pendingFeatures,pendingGraphicsLayer:this._pendingGraphicsLayer}}};async function L(e,i,a){const o=e.sourceLayer,p=e.getObjectId();if(null==p)return _().error("editing:cannot-hide-graphic","Unable to hide original graphic because it has no objectId."),r();let c=null;function h(e){c?.abort(),c=t(async i=>{const t=await P(a,o);n(i),t&&t.setVisibility?.(p,e)})}return await j(a,i),h(!1),s([F(a,i,e=>h(!e)),r(async()=>{h(!0);const e=await P(a,o);await d(()=>!e?.updating)})])}function F(e,i,t){if("3d"===e.type){const r=new v({graphic:i});return s([e.trackGraphicState(r),p(()=>r.displaying,t)])}return p(()=>i.visible,t)}async function j(e,i){if("3d"===e.type){const t=new v({graphic:i}),r=e.trackGraphicState(t);await d(()=>t.displaying||t.error),r.remove()}else await d(()=>i.visible)}async function P(e,i){const t=m(i)||g(i)?i.parent:i;return t?e.whenLayerView(t):null}function b(e){return isNaN(e)?0:e}e([h()],G.prototype,"activeOperationType",void 0),e([h()],G.prototype,"providerFactories",void 0),e([h()],G.prototype,"updating",null),e([h({constructOnly:!0})],G.prototype,"view",void 0),G=e([l("esri.views.draw.support.ConnectedReshapeManager")],G);export{G as ConnectedReshapeManager,L as swapForEditingSession,F as watchGraphicVisibility,j as whenGraphicDisplayed};
