/* COPYRIGHT Esri - https://js.arcgis.com/5.1.0/LICENSE.txt */
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 r,subclass as i}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 c,computeTriangulatedVector as u,computeTriangulatedAreaMeasurement as m,computeTriangulatedDistanceMeasurement as o}from"../../imageMeasurementUtils.js";import{extractHorizonAnglesFromMedia as l}from"../../utils.js";import{TriangulatedSketchHandlerMixin as d}from"../../mixins/TriangulatedSketchHandlerMixin.js";let p=[],g=class extends(d(t)){constructor(e){super(e),this.type="measurement"}async handleUpdate(e){const{measureType:t,updatingTriangularMeasurementState:a,currentBestFeature:r,currentBestFeatureMeasurementImage:i,activeTriangulatedViewer:s,activeViewer:n,layer:c,imageMeasurementViewer:u,mode:m,footprintExtent:o,view:l}=this.viewModel;if(i&&r&&t){if(this._preventLeftMouseDrag(),"distance"===t){const t=e.graphics[0].geometry;p=t.paths[0]}else if("area"===t){const t=e.graphics[0].geometry;p=t.rings[0]}if("complete"===e.state)if(this.viewModel.triangulatedMeasurementGraphic=e.graphics.at(-1),a)await this._updateTriangularMeasurements(p);else{const e={pixels:p,activeFeature:i,activeViewer:s,mode:m,spatialReference:l?.spatialReference};await(this.measurementService?.updateMeasurementVectorsArray(e));const a={pixels:p,measureType:t,currentBestFeature:r,currentBestFeatureMeasurementImage:i,activeViewer:n,activeTriangulatedViewer:s,mode:m,imageSize:u?.imageSize,layer:c,footprintExtent:o},d=await(this.measurementService?.calculateTriangulatedMeasurements(a));d&&("distance"===t?(this.viewModel.triangulatedDistanceMeasurement=d.distance,this.viewModel.triangulatedDistanceAccuracy=d.accuracy):"area"===t&&(this.viewModel.triangulatedAreaMeasurement=d.area,this.viewModel.triangulatedAreaAccuracy=d.accuracy??this.viewModel.triangulatedAreaAccuracy))}}}async _computeMeasurementVector(e,t){const{activeViewer:a,activeTriangulatedViewer:r,mode:i,view:n}=this.viewModel,m=a?.imageSize,o=l(r?.media);if(!e||!t||!m)return;const d=c(e,i,m,o),p=n?.spatialReference,g=[];for(let c=0;c<d.length;c++){const e={x:d[c][0],y:d[c][1]};let t=await(this.measurementService?.referenceTransformationService?.pixelToMapPoint(e));if(!t)return;p&&!t.spatialReference.equals(p)&&(t=await s(t,p)),g.push(t)}return u(g,t)}async _updateTriangularMeasurements(e,t){const{measureType:r,mode:i,activeTriangulatedViewer:s,activeViewer:c,layer:u,footprintExtent:l,triangularMeasurementActive:d,currentBestFeature:p,currentBestFeatureMeasurementImage:g,updatingTriangularMeasurementState:v}=this.viewModel,w=s?.imageSize;if(!(d&&g&&v&&s?.imageSize&&2===this.measurementService?.measurementVectors?.length&&w))return;const[M,h]=s.imageSize;this.measurementService.measurementVectors.splice(-1);const f=await this._computeMeasurementVector(e,g);if(a(t),f&&this.measurementService.measurementVectors.push(f),2===this.measurementService.measurementVectors.length){switch(r){case"distance":{this.viewModel.triangulatedDistanceMeasurement=await o(this.measurementService.measurementVectors),a(t);const r=e.map(([e,t,a])=>{const{x:r,y:i}=n({x:e,y:t,z:a},M,h);return[r,i]}),m={measurementArray:e,measurementType:"distance",currentBestFeature:p,currentBestFeatureMeasurementImage:g,activeViewer:c,imageSize:w,layer:u,triangularMeasurementActive:!0,footprintExtent:l,options:t},d={measurementArray:r,measurementType:"distance",secondaryViewer:!0,currentBestFeature:p,currentBestFeatureMeasurementImage:g,activeViewer:c,activeTriangulatedViewer:s,layer:u,footprintExtent:l,options:t},v="default"===i?await this.measurementService.calculateAccuracy(m):await this.measurementService.calculateAccuracyPanoramic(d);if(a(t),v&&this.viewModel.triangulatedDistanceMeasurement?.distance){const e=Math.atan(v/this.viewModel.triangulatedDistanceMeasurement.distance);this.viewModel.triangulatedDistanceAccuracy={distanceAccuracy:v,angleAccuracy:e}}break}case"area":if(g?.geometry.spatialReference){const e=await m(this.measurementService.measurementVectors,g.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([r()],g.prototype,"type",void 0),g=e([i("esri.widgets.OrientedImageryViewer.adapters.sketch.TriangulatedMeasurementAdapter")],g);const v=g;export{v as default};
