/*
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 e}from"tslib";import{EsriPromise as t}from"../../../../core/Promise.js";import{throwIfAborted as a}from"../../../../core/promiseUtils.js";import{property as i,subclass as r}from"../../../../core/accessorSupport/decorators.js";import{projectAsync as s}from"../../../../geometry/projectionUtils.js";import{convertSphereVertexToPixelLocation as n}from"../../../../layers/orientedImagery/transformations/utils.js";import{getModeCorrectedPoints as o,computeTriangulatedVector as c,computeTriangulatedAreaMeasurement as m,computeTriangulatedDistanceMeasurement as u}from"../../imageMeasurementUtils.js";import{TriangulatedSketchHandlerMixin as l}from"../../mixins/TriangulatedSketchHandlerMixin.js";let d=[],p=class extends(l(t)){constructor(e){super(e),this.type="measurement"}async handleUpdate(e){const{measureType:t,updatingTriangularMeasurementState:a}=this.viewModel;if(this._preventLeftMouseDrag(),"distance"===t){const t=e.graphics[0].geometry;d=t.paths[0]}else if("area"===t){const t=e.graphics[0].geometry;d=t.rings[0]}"complete"===e.state&&(this.viewModel.triangulatedMeasurementGraphic=e.graphics.at(-1),a?await this._updateTriangularMeasurements(d):await this.viewModel.processMeasurementVectors(d))}async _computeMeasurementVector(e,t){const{activeViewer:a,mode:i,view:r}=this.viewModel,n=a?.imageSize;if(!e||!t||!n)return;const m=o(e,i,n),u=r?.spatialReference,l=[];for(let o=0;o<m.length;o++){const e={x:m[o][0],y:m[o][1]};let a=await this.viewModel.getMapPoint(e,{feature:t,imageSize:n,mode:i});u&&!a.spatialReference.equals(u)&&(a=await s(a,u)),l.push(a)}return c(l,t)}async _updateTriangularMeasurements(e,t){const{measureType:i,mode:r,activeTriangulatedViewer:s,triangularMeasurementActive:o,currentBestFeatureMeasurementImage:c,updatingTriangularMeasurementState:l}=this.viewModel;if(!(o&&c&&l&&s?.imageSize&&2===this.viewModel.measurementVectors?.length))return;const[d,p]=s.imageSize;this.viewModel.measurementVectors.splice(-1);const g=await this._computeMeasurementVector(e,c);if(a(t),g&&this.viewModel.measurementVectors.push(g),2===this.viewModel.measurementVectors.length){switch(i){case"distance":{this.viewModel.triangulatedDistanceMeasurement=await u(this.viewModel.measurementVectors),a(t);const i=e.map(([e,t,a])=>{const{x:i,y:r}=n({x:e,y:t,z:a},d,p);return[i,r]}),s="default"===r?await this.viewModel.calculateAccuracy(e,"distance"):await this.viewModel.calculateAccuracyPanoramic(i,"distance",!0);if(a(t),s&&this.viewModel.triangulatedDistanceMeasurement?.distance){const e=Math.atan(s/this.viewModel.triangulatedDistanceMeasurement.distance);this.viewModel.triangulatedDistanceAccuracy={distanceAccuracy:s,angleAccuracy:e}}break}case"area":if(c?.geometry.spatialReference){const e=await m(this.viewModel.measurementVectors,c.geometry.spatialReference);a(t),this.viewModel.triangulatedAreaMeasurement=e}}if(this.viewModel.updatingTriangularMeasurementState=!1,"panoramic"===this.viewModel.mode){this.viewModel.activeTriangulatedViewer.viewModel.restoreNavigationHandles()}}}_preventLeftMouseDrag(){const{mode:e,activeTriangulatedViewer:t}=this.viewModel;if("panoramic"!==e||!t)return;let a=null;t.imageRenderer.on("pointer-down",e=>{a=e.button}),t.imageRenderer.on("drag",e=>{0===a&&e.stopPropagation()})}};e([i()],p.prototype,"type",void 0),p=e([r("esri.widgets.OrientedImageryViewer.adapters.sketch.TriangulatedMeasurementAdapter")],p);const g=p;export{g as default};
