/* COPYRIGHT Esri - https://js.arcgis.com/5.0/LICENSE.txt */
import e from"../../../../Graphic.js";import{isSome as t}from"../../../../core/arrayUtils.js";import i from"../../../../core/Logger.js";import{waitTick as o,throwIfAborted as r,isAbortError as a}from"../../../../core/promiseUtils.js";import s from"../../../../geometry/Polygon.js";import{projectAsync as n}from"../../../../geometry/projectionUtils.js";import{e as l}from"../../../../chunks/centroidOperator.js";import{convertOrientationToPixelLocation as p}from"../../../../layers/orientedImagery/transformations/utils.js";import{fixedImageSize as c}from"../../constants.js";import{activePolygonSymbol as u,polygonSymbol as d}from"../../symbols.js";class g{constructor(g){this.viewModel=g,this.createFootprints=async t=>{await o(t);const{coveragePolygons:i,currentBestFeature:r,isAdditionalCoverageVisible:a}=this.viewModel;for(const o of i)o?.imageID===r.attributes.objectId?this.viewModel.bestFeatureFootprint=new e({attributes:{imageID:o?.imageID},geometry:o,symbol:u,visible:!1}):this.viewModel.additionalFootprints.push(new e({attributes:{imageID:o?.imageID},geometry:o,symbol:d.clone(),visible:a}))},this.updateFootprint=async(o,l,p)=>{const{activeViewer:d,mode:g,currentBestFeature:m,currentCoverageVisible:v,footprintExtent:f}=this.viewModel,w=l??m;if(m&&("video"===g?c:d?.imageSize)&&f&&w)try{const i=await this.viewModel.getMapPoint(o);r(p);const a=i.filter(t);if(!a.length)return;const l=[a.map(({x:e,y:t})=>[e,t,1])];l[0].push(l[0][0]);const c=new s({rings:l,spatialReference:a[0].spatialReference}),d=this.viewModel.view?.spatialReference.equals(c.spatialReference)?c:await n(c,this.viewModel.view.spatialReference,p);this.viewModel.updateCurrentCoveragePolygon(new e({geometry:d,attributes:{imageID:w.attributes.objectId},symbol:u,visible:v}))}catch(h){a(h)||i.getLogger(this.viewModel).error("update-footprint",h)}else this.viewModel.updateCurrentCoveragePolygon(null)},this.updateVideoFootprint=async o=>{const{points:p,feature:d,scale:g,options:m}=o,{activeViewer:v,mode:f,currentBestFeature:w,currentCoverageVisible:h,footprintExtent:y}=this.viewModel,M="video"===f?c:v?.imageSize,b=d??w;if(w&&M&&y&&b&&"none"!==f)try{const i={feature:b,mode:f,imageSize:M,footprintExtent:y},o=await this.viewModel.getMapPointForFeature(p,i,m);r(m);const a=o.filter(t);if(!a.length)return;const c=[a.map(({x:e,y:t})=>[e,t,1])];c[0].push(c[0][0]);let d=new s({rings:c,spatialReference:a[0].spatialReference});if(g){const e=d.spatialReference,t=l(d),i=d.rings.map(e=>e.map(([e,i])=>[t.x+(e-t.x)*g,t.y+(i-t.y)*g]));d=new s({rings:i,spatialReference:e})}const v=this.viewModel.view?.spatialReference.equals(d.spatialReference)?d:await n(d,this.viewModel.view.spatialReference,m);this.viewModel.updateCurrentCoveragePolygon(new e({geometry:v,attributes:{imageID:b.attributes.objectId},symbol:u,visible:h}))}catch(C){a(C)||i.getLogger(this.viewModel).error("update-footprint",C)}else this.viewModel.updateCurrentCoveragePolygon(null)},this.updateFootprintPanorama=async(t,i)=>{const{horizontalFieldOfView:o,pitch:a,verticalFieldOfView:l,yaw:d,feature:g}=t,{activeViewer:m,currentBestFeature:v,currentCoverageVisible:f,footprintExtent:w,mode:h}=this.viewModel,y="video"===h||"panoramic-video"===h?c:m?.imageSize,M=g??v;if(!M||!y||!w||"none"===h)return void this.viewModel.updateCurrentCoveragePolygon(null);const{cameraPitch:b,objectId:C}=M.attributes;if(a+b-l/2>180)return void this.viewModel.updateCurrentCoveragePolygon(null);const F=[],R=l/2,P=o/2,j=a-R,I=b+j,x=b+(a+R);if(I>180)return void this.viewModel.updateCurrentCoveragePolygon(null);const V=j<0,D=[-P,R],z=[P,R],E=[P,-R],S=[-P,-R];if(I<180&&x>180){F.push(V?E:S);const e=Math.floor(o/5),t=o/e;for(let i=0;i<=e;i++)F.push([i*t-P,90-a]);F.push(V?S:E)}x<180&&(F.push(D,z),V?F.push(S,E):F.push(E,S));const B=F.map(([e,t])=>p(d+e,a+t,y[0],y[1])),q={feature:M,mode:h,imageSize:y,footprintExtent:w},L=g?await this.viewModel.getMapPointForFeature(B,q):await this.viewModel.getMapPoint(B);r(i);const O=[L.map(({x:e,y:t})=>[e,t,1])];O[0].push(O[0][0]);const U=new s({rings:O,spatialReference:L[0].spatialReference}),k=this.viewModel.view?.spatialReference.equals(U.spatialReference)?U:await n(U,this.viewModel.view.spatialReference,i);this.viewModel.updateCurrentCoveragePolygon(new e({geometry:k,attributes:{imageID:C},symbol:u.clone(),visible:f}))}}}export{g as default};
