/* COPYRIGHT Esri - https://js.arcgis.com/5.0.18/LICENSE.txt */
import{__decorate as e}from"tslib";import t from"../../../../../core/Accessor.js";import{isSome as o}from"../../../../../core/arrayUtils.js";import{destroyHandle as r}from"../../../../../core/handleUtils.js";import{initial as n}from"../../../../../core/reactiveUtils.js";import{property as i,subclass as s}from"../../../../../core/accessorSupport/decorators.js";import{UpdatingHandles as a}from"../../../../../core/support/UpdatingHandles.js";import p from"../../../../../geometry/Point.js";import l from"../../../../../geometry/Polygon.js";import{initializeProjection as c,project as d}from"../../../../../geometry/projectionUtils.js";import{ControlPointOperations as m}from"./ControlPointOperations.js";import{EditGeometry as h}from"../../../../interactive/editGeometry/EditGeometry.js";let g=class extends t{get operations(){return this._operations}get updating(){return this._updatingHandles.updating}constructor(e){super(e),this._updatingHandles=new a}initialize(){this.addHandles([r(this._updatingHandles),this._updatingHandles.add(()=>{const e=this.element.georeference;return"control-points"===e?.type?e.controlPoints:null},e=>this._elementControlPointsChanged(e),n)])}_elementControlPointsChanged(e){const t=e?.map(({mapPoint:e})=>e).filter(o),r=this.view.spatialReference;this._updatingHandles.addPromise(this._whenProjected(t,r,e=>{if(!e)return void this._replaceOperations(null);const{_operations:t}=this,o=e.map(({x:e,y:t})=>[e,t]);o.push(o[0].slice());const n=new l({rings:[o],spatialReference:r});if(t?.trySetGeometry(n))return void this.onModifiedExternally();const i=this.view.state.viewingMode;this._replaceOperations(new m(h.fromGeometry(n,i),i,e=>this._operationsGeometryChanged(e)))}))}_operationsGeometryChanged(e){const{element:{georeference:t},_operations:r}=this;if(!r||!t||"control-points"!==t.type||!t.controlPoints)return;const n=r.data.geometry,i=t.controlPoints.map(({mapPoint:e})=>e).filter(o),s=r.data.parts[0].isClosed()?1:0;if(n.rings[0]?.length-s!==i.length)return;const a=n.rings[0].map(([e,t])=>new p({x:e,y:t,spatialReference:n.spatialReference}));a.length-=s;const l=i.map(({spatialReference:e})=>e);this._updatingHandles.addPromise(this._whenProjected(a,l,t=>this._updateElementControlPoints(t,e)))}_updateElementControlPoints(e,t){const{georeference:o}=this.element;if(!o||!e||"control-points"!==o.type||o.controlPoints?.length!==e?.length)return;const r=o.controlPoints;if(r?.length===e.length)for(let n=0;n<r.length;n++)t&&(r[n].sourcePoint=o.toSource(e[n])),r[n].mapPoint=e[n]}async _whenProjected(e,t,o){if(!e)return void o(e);const{_operations:r,element:{georeference:n}}=this,i=e.map(({spatialReference:e})=>e),s=Array.isArray(t)?t:new Array(i.length).fill(t);await c(Array.from(i).map((e,t)=>({source:e,dest:s[t]})));const a=e.map((e,t)=>d(e,s[t]));r===this._operations&&n===this.element.georeference&&o(a)}_replaceOperations(e){this._operations&&(this.removeHandles(this._operations),this._operations.destroy()),this._operations=e}};e([i({constructOnly:!0})],g.prototype,"view",void 0),e([i({constructOnly:!0})],g.prototype,"layer",void 0),e([i({constructOnly:!0})],g.prototype,"element",void 0),e([i({constructOnly:!0})],g.prototype,"onModifiedExternally",void 0),e([i()],g.prototype,"_operations",void 0),e([i()],g.prototype,"operations",null),e([i()],g.prototype,"updating",null),g=e([s("esri.views.3d.interactive.editingTools.media.MediaElementControllerControlPoints")],g);export{g as MediaElementControllerControlPoints};
