/*
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 t from"../../analysis/LineOfSightAnalysis.js";import s from"../../analysis/LineOfSightAnalysisObserver.js";import o from"../../analysis/LineOfSightAnalysisTarget.js";import i from"../../core/Accessor.js";import n from"../../core/Collection.js";import{referenceSetter as r,castForReferenceSetter as a}from"../../core/collectionUtils.js";import{deprecateUnnecessaryViewModel as l}from"../../core/deprecate.js";import{handlesGroup as c}from"../../core/handleUtils.js";import"../../core/has.js";import g from"../../core/Logger.js";import{watch as d,syncAndInitial as h,sync as p}from"../../core/reactiveUtils.js";import{property as m,subclass as y}from"../../core/accessorSupport/decorators.js";import v from"../../geometry/Point.js";import{getEffectiveElevationInfo as T,getConvertedElevation as u,absoluteHeightElevationInfo as f}from"../../support/elevationInfoUtils.js";import w from"./LineOfSightTarget.js";import{AnalysisViewModelMixin as A}from"../support/AnalysisViewModelMixin.js";const _=n.ofType(w);let M=class extends(A(i)){constructor(e){super(e),this.analysis=null,this.supportedViewType="3d",this.unsupportedErrorMessage="LineOfSightViewModel is only supported in 3D views.",this._vmTargetsToConnection=new Map,this._analysisTargetsToConnection=new Map,e?.isDefaultViewModel||l(g.getLogger(this),"Line Of Sight","arcgis-line-of-sight",{version:"4.33"})}initialize(){this.addHandles([this.targets.on("after-add",e=>this._onViewModelTargetAdded(e.item)),this.targets.on("after-remove",e=>this._onViewModelTargetRemoved(e.item)),d(()=>this.analysis,e=>this._onAnalysisChange(e),h)])}destroy(){this._analysisTargetsToConnection.forEach(e=>e.remove())}get state(){return this.disabled||!this.ready?"disabled":null==this.tool?"ready":this.tool.state}get observer(){const{observer:e}=this.analysis;return null==e?.position?null:this._convertAnalysisPointToAbsoluteHeight(e.position,e.elevationInfo)}set observer(e){let t=null;e&&(t=e.clone(),t.hasZ||(t.z=0)),this.analysis.observer=new s({position:t})}get targets(){return this._get("targets")||new _}set targets(e){this._set("targets",r(e,this.targets,_))}continue(){null!=this.tool&&this.tool.continue()}stop(){null!=this.tool&&this.tool.stop()}get testInfo(){}constructAnalysis(){return new t}async onConnectToAnalysisView(e){this.addHandles([e.on("result-changed",e=>{const t=this._analysisTargetsToConnection.get(e.target);t&&(null!=e.result?(t.viewModelTarget.intersectedGraphic=e.result.intersectedGraphic,t.viewModelTarget.intersectedLocation=e.result.intersectedLocation,t.viewModelTarget.visible=e.result.visible):(t.viewModelTarget.intersectedGraphic=null,t.viewModelTarget.intersectedLocation=null,t.viewModelTarget.visible=void 0))})],"view")}onDisconnectFromAnalysisView(){this.removeHandles("view")}_onViewModelTargetAdded(e){if(this._vmTargetsToConnection.get(e))return;const t=new o({position:e.location});this._connectViewModelWithAnalysisTarget(e,t),this.analysis.targets.add(t)}_onViewModelTargetRemoved(e){const t=this._vmTargetsToConnection.get(e);t&&(t.remove(),this.analysis.targets.remove(t.analysisTarget))}_onAnalysisTargetAdded(e){if(this._analysisTargetsToConnection.get(e))return;const{position:t}=e,s=new w({location:t?this._convertAnalysisPointToAbsoluteHeight(t,e.elevationInfo):null});this._connectViewModelWithAnalysisTarget(s,e),this.targets.add(s)}_onAnalysisTargetRemoved(e){const t=this._analysisTargetsToConnection.get(e);t&&(t.remove(),this.targets.remove(t.viewModelTarget))}_connectViewModelWithAnalysisTarget(e,t){let s=!1;const o=c([d(()=>({position:t.position,elevationInfo:t.elevationInfo}),({position:t,elevationInfo:o})=>{s||(s=!0,e.location=t?this._convertAnalysisPointToAbsoluteHeight(t,o):null,s=!1)},p),d(()=>e.location,e=>{if(!s){s=!0;let o=null;e&&(o=e.clone(),o.hasZ||(o.z=0)),t.position=o,t.elevationInfo=null,s=!1}},p)]),i={analysisTarget:t,viewModelTarget:e,remove:()=>{o.remove(),this._vmTargetsToConnection.delete(e),this._analysisTargetsToConnection.delete(t)}};this._vmTargetsToConnection.set(e,i),this._analysisTargetsToConnection.set(t,i)}_onAnalysisChange(e){const t="analysis";this.removeHandles(t),this.addHandles([this.analysis.targets.on("after-add",e=>this._onAnalysisTargetAdded(e.item)),this.analysis.targets.on("after-remove",e=>this._onAnalysisTargetRemoved(e.item))],t),this.targets.removeAll(),e.targets.forEach(e=>{this._onAnalysisTargetAdded(e)})}_convertAnalysisPointToAbsoluteHeight(e,t){const s=e.clone();if(null!=this.view){const o=T(e.hasZ,t);s.z=u(this.view,e,o,f)}return s}};e([m({type:t})],M.prototype,"analysis",void 0),e([m({readOnly:!0})],M.prototype,"state",null),e([m({type:v})],M.prototype,"observer",null),e([m({type:_,cast:a,nonNullable:!0})],M.prototype,"targets",null),M=e([y("esri.widgets.LineOfSight.LineOfSightViewModel")],M);const b=M;export{b as default};
