/*
All material copyright ESRI, All Rights Reserved, unless otherwise specified.
See https://js.arcgis.com/4.24/esri/copyright.txt for details.
*/
define(["exports","../../../../chunks/_rollupPluginBabelHelpers","../../../../chunks/tslib.es6","../../../../core/Accessor","../../../../core/Handles","../../../../core/Logger","../../../../core/maybe","../../../../core/quantityUtils","../../../../core/reactiveUtils","../../../../core/accessorSupport/decorators/property","../../../../core/arrayUtils","../../../../core/has","../../../../core/accessorSupport/ensureType","../../../../core/accessorSupport/decorators/subclass","../../../../chunks/vec3","../../../../chunks/vec3f64","../../../../geometry/geometryEngine","../../../../geometry/Polyline","../../../../geometry/projection","../../../../geometry/projectionEllipsoid","../../../../geometry/support/geodesicUtils","../interfaces","../support/projectionUtils","../support/UnitNormalizer"],(function(e,t,i,n,o,r,a,s,c,l,u,d,p,h,m,v,y,_,P,g,D,f,w,M){"use strict";const L=r.getLogger("esri.views.3d.analysis.DirectLineMeasurement.DirectLineMeasurementController"),C=1e5;e.DirectLineMeasurementController=function(e){function i(t){var i;return(i=e.call(this,t)||this)._unitNormalizer=new M.UnitNormalizer,i._handles=new o,i._tempStartPosition=v.create(),i._tempEndPosition=v.create(),i._tempCornerPosition=v.create(),i}t._inheritsLoose(i,e);var n=i.prototype;return n.initialize=function(){this._handles.add(c.when((()=>{var e;return null==(e=this.view)?void 0:e.ready}),(()=>this._initialize()),{once:!0,sync:!0,initial:!0}))},n.destroy=function(){this._handles=a.destroyMaybe(this._handles)},n._initialize=function(){const e=this.view.spatialReference,t=g.getSphericalPCPF(e),i=t===g.SphericalECEFSpatialReference?g.WGS84ECEFSpatialReference:t;this._sphericalPCPF=i;const n=P.canProjectWithoutEngine(e,i);this._unitNormalizer.spatialReference=n?i:e,this._handles.add([c.watch((()=>({viewData:this.viewData,startPoint:this.analysis.startPoint})),(({viewData:e,startPoint:t})=>{e.elevationAlignedStartPoint=this._applyProjectionAndElevationAlignment(t)}),c.syncAndInitial),c.watch((()=>({viewData:this.viewData,endPoint:this.analysis.endPoint})),(({viewData:e,endPoint:t})=>{e.elevationAlignedEndPoint=this._applyProjectionAndElevationAlignment(t)}),c.syncAndInitial),c.watch((()=>({result:this._computedResult,viewData:this.viewData})),(({result:e,viewData:t})=>{t.result=e}),c.syncAndInitial)])},n._applyProjectionAndElevationAlignment=function(e){if(a.isNone(e))return e;const t=w.applyProjectionAndElevationAlignment(e,this.view.spatialReference,this.view.elevationProvider);return a.isNone(t)?(w.logFailedGeometryProjectionError(this.analysis,e.spatialReference,L),null):t},n._euclideanDistances=function(e,t){const i=e.clone();i.z=t.z;const n=this._tempStartPosition,o=this._tempEndPosition,r=this._tempCornerPosition,a=this.view.spatialReference,c=this._sphericalPCPF,l=P.canProjectWithoutEngine(a,c)?c:a;P.projectPointToVector(e,n,l),P.projectPointToVector(t,o,l),P.projectPointToVector(i,r,l);const u=m.distance(n,o),d=m.distance(r,o),p=Math.abs(t.z-e.z),h=e=>this._unitNormalizer.normalizeDistance(e),v=h(u),y=h(d),_=h(p);return{direct:s.createQuantity(v,"meters"),horizontal:s.createQuantity(y,"meters"),vertical:s.createQuantity(_,"meters")}},n._geodesicDistance=function(e,t,i){const n=e.spatialReference,o=new _({spatialReference:n});o.addPath([e,t]);const r=n.isGeographic&&D.isSupported(n)?D.geodesicLengths([o],"meters")[0]:n.isWebMercator?y.geodesicLength(o,"meters"):null,c=a.isSome(r)?r:this._fallbackGeodesicDistance(e,t,i);return s.createQuantity(c,"meters")},n._fallbackGeodesicDistance=function(e,t,i){if(P.projectPointToWGS84ComparableLonLat(e,S)&&P.projectPointToWGS84ComparableLonLat(t,z)){const e={distance:0};return D.inverseGeodeticSolver(e,S,z),e.distance}return i},t._createClass(i,[{key:"_computedResult",get:function(){const{elevationAlignedStartPoint:e,elevationAlignedEndPoint:t,measurementMode:i}=this.viewData;if(a.isNone(e)||a.isNone(t))return null;const n=this._euclideanDistances(e,t),o=this._geodesicDistance(e,t,n.horizontal.value),r=i===f.MeasurementMode.Geodesic||i===f.MeasurementMode.Auto&&n.horizontal.value>C?"geodesic":"euclidean";return{mode:r,distance:"euclidean"===r?n.direct:o,directDistance:n.direct,horizontalDistance:n.horizontal,verticalDistance:n.vertical,geodesicDistance:o}}}]),i}(n),i.__decorate([l.property()],e.DirectLineMeasurementController.prototype,"view",void 0),i.__decorate([l.property()],e.DirectLineMeasurementController.prototype,"analysis",void 0),i.__decorate([l.property()],e.DirectLineMeasurementController.prototype,"viewData",void 0),i.__decorate([l.property()],e.DirectLineMeasurementController.prototype,"_computedResult",null),e.DirectLineMeasurementController=i.__decorate([h.subclass("esri.views.3d.analysis.DirectLineMeasurement.DirectLineMeasurementController")],e.DirectLineMeasurementController);const S=v.create(),z=v.create();Object.defineProperties(e,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}})}));
