/* COPYRIGHT Esri - https://js.arcgis.com/5.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{d as m,c as u,u as b}from"../../../../../../chunks/boundedPlane.js";import{fromPositionAndNormal as h,projectPoint as g}from"../../../../../../geometry/support/plane.js";import{sv3d as f}from"../../../../../../geometry/support/vectorStacks.js";import{mainAxis as B}from"../../geometryUtils.js";import{manipulatedObjectGeometry as y}from"../../manipulatedObjectUtils.js";import{evaluateElevationAlignmentAtPoint as M}from"../../../../layers/graphics/elevationAlignmentUtils.js";import{ElevationContext as j}from"../../../../layers/graphics/ElevationContext.js";import{apply as v,applyInverse as _,calculateOrientedBounds as x}from"../../../../../interactive/editGeometry/support/editPlaneUtils.js";import{autoSize2D as R}from"../../../../../support/extentUtils.js";class C{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=y(t),o=s.pointsOfInterest?.centerOnSurfaceFrequent.location??i?.extent?.center;return o?D*s.pixelSizeAt(o):0}backupMapBounds(){u(this.mapBounds,this.mapBoundsStart)}updateDisplayBounds(){this._calculateDisplayBounds(),this._onDisplayBoundsChanged?.()}updateMapBoundsFromOperation(s){v(s,this.mapBounds),this.updateDisplayBounds()}updateMapBoundsFromOperationInverse(s){_(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=B(a);t(n,n,-1);const r=i?s.pixelSizeAt(i,a.spatialReference):0,p=O*r,d=e.coordinateHelper.hasZ()&&"on-the-ground"!==o.elevationInfo.mode;x(n,e,p,this.mapBounds,d)}_calculateDisplayBounds(){const{view:s,attachmentOrigin:t}=this._tool,i=t?.z??M(this.mapBounds.origin,s.elevationProvider,j.fromElevationInfo(this._tool.object.elevationInfo),s.renderCoordsHelper);U(this.mapBounds,i??0,s.renderCoordsHelper,this.spatialReference,this.displayBoundsMargin,this.displayBounds)}}const D=10,O=80;function U(s,t,c,l,u=0,B){B||(B=m());const y=f.get();i(y,s.origin,s.basis1),i(y,y,s.basis2),c.toRenderCoords(y,l,y);const M=f.get();i(M,s.origin,s.basis1),o(M,M,s.basis2),c.toRenderCoords(M,l,M);const j=f.get();o(j,s.origin,s.basis1),o(j,j,s.basis2),c.toRenderCoords(j,l,j);const v=f.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(f.get(),y,M,.5),x=a(f.get(),j,v,.5);e(x,x,-1),a(B.basis1,_,x,.5);const R=a(f.get(),y,v,.5),C=a(f.get(),M,j,.5);e(C,C,-1),a(B.basis2,R,C,.5);const D=n(f.get(),B.basis1,B.basis2);n(B.basis2,D,B.basis1),r(B.basis1,B.basis1);const O=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 U=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,O+u),e(B.basis2,B.basis2,U+u);const S=d(f.get(),s.origin);return S[2]=t,c.toRenderCoords(S,l,S),h(S,D,B.plane),g(B.plane,B.origin,B.origin),b(B),B}function S(s,t,i,e){return o(L,o(L,s.origin,s.basis1),s.basis2),c(H,L,s.basis1,2),c(A,H,s.basis2,2),c(I,L,s.basis2,2),L[2]=H[2]=A[2]=I[2]=t,R({topLeft:I,topRight:A,bottomRight:H,bottomLeft:L,spatialReference:i,automaticLengthMeasurementUtils:e})}const I=l(),A=l(),H=l(),L=l();export{C as TransformBounds,S as mapPlaneAutoSize2D};
