/* COPYRIGHT Esri - https://js.arcgis.com/5.1.2/LICENSE.txt */
import{__decorate as e}from"tslib";import t from"./Analysis.js";import n from"./Viewshed.js";import{equals as r}from"../core/arrayUtils.js";import i from"../core/Collection.js";import{referenceSetter as s,castForReferenceSetter as o}from"../core/collectionUtils.js";import{cyclicalDegrees as l}from"../core/Cyclical.js";import{deg2rad as a,clamp as u}from"../core/mathUtils.js";import{watch as p,syncAndInitial as c}from"../core/reactiveUtils.js";import{property as m,subclass as h}from"../core/accessorSupport/decorators.js";import d from"../geometry/Extent.js";import{projectOrLoadMany as f}from"../geometry/projectionUtils.js";import{getGeometryEffectiveElevationMode as v}from"../support/elevationInfoUtils.js";const y=i.ofType(n);let x=class extends t{constructor(e){super(e),this.type="viewshed",this._extent=null}initialize(){this.addHandles(p(()=>this._computeExtent(),e=>{null==e.pending&&(this._extent=e.extent)},c))}get viewsheds(){return this._get("viewsheds")||new y}set viewsheds(e){this._set("viewsheds",s(e,this.viewsheds,y))}get spatialReference(){for(const e of this.viewsheds)if(null!=e.observer)return e.observer.spatialReference;return null}get extent(){return this._extent}get valid(){return this.viewsheds.every(e=>e.valid)}async waitComputeExtent(){const e=this._computeExtent();null!=e.pending&&await e.pending}_computeExtent(){const{spatialReference:e}=this;if(null==e)return{pending:null,extent:null};const t=this.viewsheds.filter(e=>null!=e.observer),n=t.map(e=>e.observer).toArray(),r=f(n,e);if(null!=r.pending)return{pending:r.pending,extent:null};const i=r.geometries.map((e,n)=>{const r=t.at(n);return null!=e&&null!=r?this._computeViewshedExtent(this.viewsheds.at(n),e):null}).filter(e=>null!=e).reduce((e,t)=>g(e,t),null);return null!=i&&t.some(e=>!w(e))&&(i.zmin=void 0,i.zmax=void 0),{pending:null,extent:i}}_computeViewshedExtent(e,t){const{farDistance:n,heading:r,tilt:i,horizontalFieldOfView:s,verticalFieldOfView:o}=e,{spatialReference:p}=t,c=s/2,m=o/2,h=n/p.metersPerUnit,f=[l.normalize(r-c),r,l.normalize(r+c)],v=d.fromPoint(t),y=e=>{const t=f.map(t=>l.normalize(t-e));if(t[0]>t[2]||360===s)return h;const n=t.map(e=>Math.abs(e>180?360-e:e)).reduce((e,t)=>e>t?t:e);return n>90?0:h*Math.cos(a(n))};v.xmax+=y(90),v.xmin-=y(-90),v.ymax+=y(0),v.ymin-=y(180);const x=t.z;if(null!=x){let e=x,t=x;const r=i-90,s=u(r+m,-90,90),o=u(r-m,-90,90),l=p?.isGeographic?n:h;e+=l*_(s),t+=l*_(o);const a=j(m)*l,d=_(r)*a*(1-j(c));i<90&&(e-=d),i>90&&(t-=d),v.zmax=Math.max(e,x),v.zmin=Math.min(t,x)}return v}equals(e){return this===e||super.equals(e)&&r(this.viewsheds.toArray(),e.viewsheds.toArray(),(e,t)=>e.equals(t))}clear(){this.viewsheds.removeAll()}};function w(e){return"absolute-height"===v(e.observer,e.elevationInfo)}function g(e,t){return null==e?t:null==t?e:e.union(t)}function j(e){return Math.cos(a(e))}function _(e){return Math.sin(a(e))}e([m({type:["viewshed"]})],x.prototype,"type",void 0),e([m({cast:o,type:y,nonNullable:!0})],x.prototype,"viewsheds",null),e([m({readOnly:!0})],x.prototype,"spatialReference",null),e([m()],x.prototype,"_extent",void 0),e([m()],x.prototype,"extent",null),e([m({readOnly:!0})],x.prototype,"valid",null),x=e([h("esri.analysis.ViewshedAnalysis")],x);export{x as default};
