/* COPYRIGHT Esri - https://js.arcgis.com/5.1.0/LICENSE.txt */
import{on as s}from"../../../../../../core/reactiveUtils.js";import{scale as t}from"../../../../../../core/libs/gl-matrix-2/math/vec2.js";import{add as i,subtract as o,scale as e,lerp as a,cross as n,normalize as r,dot as p,copy as d,scaleAndAdd as c}from"../../../../../../core/libs/gl-matrix-2/math/vec3.js";import{create as l}from"../../../../../../core/libs/gl-matrix-2/factories/vec3f64.js";import u from"../../../../../../geometry/Point.js";import{c as m,a as b,u as g}from"../../../../../../chunks/boundedPlane.js";import{fromPositionAndNormal as h,projectPoint as f}from"../../../../../../geometry/support/plane.js";import{sv3d as B}from"../../../../../../geometry/support/vectorStacks.js";import{mainAxis as y}from"../../geometryUtils.js";import{manipulatedObjectGeometry as M}from"../../manipulatedObjectUtils.js";import{evaluateElevationAlignmentAtPoint as j}from"../../../../layers/graphics/elevationAlignmentUtils.js";import{ElevationContext as v}from"../../../../layers/graphics/ElevationContext.js";import{apply as _,applyInverse as x,calculateOrientedBounds as C}from"../../../../../interactive/editGeometry/support/editPlaneUtils.js";class R{constructor(t,i){this._tool=t,this.mapBounds=m(),this.mapBoundsStart=m(),this.displayBounds=m(),t.addHandles(s(()=>t.object,"modified-externally",()=>this._resetMapBounds(),{onListenerAdd:()=>this._resetMapBounds()})),this._onDisplayBoundsChanged=i}get displayBoundsMargin(){const{view:s,object:t}=this._tool,i=M(t),o=s.pointsOfInterest?.centerOnSurfaceFrequent.location??i?.extent?.center;return o?D*s.pixelSizeAt(o):0}backupMapBounds(){b(this.mapBounds,this.mapBoundsStart)}updateDisplayBounds(){this._calculateDisplayBounds(),this._onDisplayBoundsChanged?.()}updateMapBoundsFromOperation(s){_(s,this.mapBounds),this.updateDisplayBounds()}updateMapBoundsFromOperationInverse(s){x(s,this.mapBounds),this.updateDisplayBounds()}_resetMapBounds(){this._calculateMapBounds(),this.updateDisplayBounds()}_calculateMapBounds(){const{view:s,attachmentOrigin:i,object:o}=this._tool,e=o.operations?.data;if(!e)return;const a=e.geometry;this.spatialReference=a.spatialReference;const n=y(a);t(n,n,-1);const r=i?s.pixelSizeAt(i,a.spatialReference):0,p=w*r,d=e.coordinateHelper.hasZ()&&"on-the-ground"!==o.elevationInfo.mode;C(n,e,p,this.mapBounds,d)}_calculateDisplayBounds(){const{view:s,attachmentOrigin:t}=this._tool,i=t?.z??j(this.mapBounds.origin,s.elevationProvider,v.fromElevationInfo(this._tool.object.elevationInfo),s.renderCoordsHelper);O(this.mapBounds,i??0,s.renderCoordsHelper,this.spatialReference,this.displayBoundsMargin,this.displayBounds)}}const D=10,w=80;function O(s,t,c,l,u=0,b){b||(b=m());const y=B.get();i(y,s.origin,s.basis1),i(y,y,s.basis2),c.toRenderCoords(y,l,y);const M=B.get();i(M,s.origin,s.basis1),o(M,M,s.basis2),c.toRenderCoords(M,l,M);const j=B.get();o(j,s.origin,s.basis1),o(j,j,s.basis2),c.toRenderCoords(j,l,j);const v=B.get();o(v,s.origin,s.basis1),i(v,v,s.basis2),c.toRenderCoords(v,l,v),i(b.origin,y,M),i(b.origin,b.origin,j),i(b.origin,b.origin,v),e(b.origin,b.origin,.25),o(y,y,b.origin),o(M,M,b.origin),o(j,j,b.origin),o(v,v,b.origin);const _=a(B.get(),y,M,.5),x=a(B.get(),j,v,.5);e(x,x,-1),a(b.basis1,_,x,.5);const C=a(B.get(),y,v,.5),R=a(B.get(),M,j,.5);e(R,R,-1),a(b.basis2,C,R,.5);const D=n(B.get(),b.basis1,b.basis2);n(b.basis2,D,b.basis1),r(b.basis1,b.basis1);const w=Math.max(Math.abs(p(b.basis1,y)),Math.abs(p(b.basis1,M)),Math.abs(p(b.basis1,j)),Math.abs(p(b.basis1,v)));r(b.basis2,b.basis2);const O=Math.max(Math.abs(p(b.basis2,y)),Math.abs(p(b.basis2,M)),Math.abs(p(b.basis2,j)),Math.abs(p(b.basis2,v)));e(b.basis1,b.basis1,w+u),e(b.basis2,b.basis2,O+u);const S=d(B.get(),s.origin);return S[2]=t,c.toRenderCoords(S,l,S),h(S,D,b.plane),f(b.plane,b.origin,b.origin),g(b),b}async function S(s,t,i,e){return o(H,o(H,s.origin,s.basis1),s.basis2),c(A,H,s.basis1,2),c(z,A,s.basis2,2),c(U,H,s.basis2,2),H[2]=A[2]=z[2]=U[2]=t,e.autoSize2D(I(P,U,i),I(k,z,i),I(F,A,i),I(E,H,i))}function I(s,t,i){return s.x=t[0],s.y=t[1],s.spatialReference=i,s}const U=l(),z=l(),A=l(),H=l(),P=new u,k=new u,F=new u,E=new u;export{R as TransformBounds,S as mapPlaneAutoSize2D};
