/* COPYRIGHT Esri - https://js.arcgis.com/5.0/LICENSE.txt */
import{deepClone as e}from"../../../arcade/deepClone.js";import"../../../core/RandomLCG.js";import"../../../core/Logger.js";import"../../../core/has.js";import{create as t}from"../../../core/libs/gl-matrix-2/factories/vec3f64.js";import a from"../../../geometry/Point.js";import n from"../../../geometry/Polygon.js";import{fromArray as r,intersectLine as i,fromPoints as o}from"../../../geometry/support/plane.js";import s from"./updateElevation.js";import{pointToArray as c,or as m}from"./utils.js";import{worldToImagePanoramic as p,worldToImage as f}from"./worldToImage.js";import{normalizeDegrees as l}from"../../../widgets/OrientedImageryViewer/components/imageNavigationUtils.js";async function g(e,n,r,s=w){const{cameraLocation:p,farPlaneVertices:f}=r,l=c(p),g=new Array;await d(f,r,g);let h=o(g[0],g[1],g[2]),u=e.clone();for(let w=0;w<s;w++){const s=t();if(!i(h,l,c(e),s))break;let f=0,w=null;if(({error:f,imagePoint:w,pointWithZ:u}=await j(n,new a(s,p.spatialReference),r)),f<=1)break;const[d,b]=y(p.spatialReference,g);if(m(d<=1,b<=1))break;g.splice(0,g.length,...await x(d,b,new a(s,p.spatialReference),r)),h=o(g[0],g[1],g[2])}return c(u)}async function h(e,n,o,s=w){const{cameraLocation:p,farPlaneVertices:f}=o,l=c(p),g=new Array;await d(f,o,g);const h=[0,0,0,0];let u=r(h,g.flat(),!1),j=e.clone();for(let w=0;w<s;w++){const s=t();if(!i(h,l,c(e),s)||!u)break;let f,w=0;if(({error:w,imagePoint:f,pointWithZ:j}=await b(n,new a(s,p.spatialReference),o)),w<=1)break;const[d,R]=y(p.spatialReference,g);if(m(d<=1,R<=1))break;g.splice(0,g.length,...await x(d,R,new a(s,p.spatialReference),o)),u=r(h,g.flat(),!1)}return c(j)}const w=10,u=10;function y(e,t){const a=new n({spatialReference:e});a.addRing(t);return[a.extent?.width?a.extent.width/u:1,a.extent?.height?a.extent.height/u:1]}async function d(t,a,n){const r=e(t),i=await s(r,a);n.push(...i.map(c))}async function j(e,t,a){const n=await s(t,a),r=f(n,a);return{error:R(r,e),imagePoint:r,pointWithZ:n}}async function b(e,t,a){const n=await s(t,a),r=p(n,a),i=l(r.heading+a.cameraHeading);return{error:Math.abs(i-e.heading)+Math.abs(r.pitch-e.pitch),imagePoint:r,pointWithZ:n}}const R=(e,t)=>Math.abs(e.x-t.x)+Math.abs(e.y-t.y);async function x(e,t,n,r){const i=[[-e,-t],[e,-t],[e,t],[-e,t]].map(([e,t])=>new a([n.x+e,n.y+t],n.spatialReference));return s(i,r).then(e=>e.map(e=>e.toArray()))}export{g as rectifyMapPoint,h as rectifyMapPointPanoramic};
