/* COPYRIGHT Esri - https://js.arcgis.com/5.1.0/LICENSE.txt */
import t from"../../../../../core/Handles.js";import{set as i,normalize as e}from"../../../../../core/libs/gl-matrix-2/math/vec2.js";import{create as r}from"../../../../../core/libs/gl-matrix-2/factories/vec2f64.js";import{copy as a,scaleAndAdd as s,subtract as n,length as o,dot as l}from"../../../../../core/libs/gl-matrix-2/math/vec3.js";import{create as c}from"../../../../../core/libs/gl-matrix-2/factories/vec3f64.js";import{c as h,a as p}from"../../../../../chunks/boundedPlane.js";import{getInfo as u}from"../../../../../geometry/support/spatialReferenceUtils.js";import{Manipulation as _}from"./Manipulation.js";import{onGrabChangedHandle as d}from"./utils.js";import{createManipulatorDragEventPipeline as m,screenToMap as f}from"../../../../interactive/dragEventPipeline.js";import{GraphicManipulator as g}from"../../../../interactive/GraphicManipulator.js";import{apply as v}from"../../../../interactive/editGeometry/support/editPlaneUtils.js";const b=10,x=1e-6,C=.3;function y(t){const i=o(t.basis1),e=o(t.basis2);return C*Math.min(i,e)}class j extends _{constructor(i){super(),this._handles=new t,this._planeStart=h(),this._displayPlaneStart=h(),this._originCache=c(),this._axisCache=r(),this._renderStartCache=c(),this._renderEndCache=c(),this._resizeOriginCache=c(),this._view=i.view,this._tool=i.tool,this._graphic=i.graphic,this._direction=i.direction,this._preserveAspectRatio=i.preserveAspectRatio,this._manipulator=this._createManipulator(),this._handles.add([this._manipulator.events.on("grab-changed",t=>d(t,this._manipulator))]),this.forEachManipulator(t=>this._tool.manipulators.add(t))}destroy(){this._handles.destroy(),this.forEachManipulator(t=>{this._tool.manipulators.remove(t),t.destroy()}),this._tool=null,this._view=null,this._graphic=null,this._manipulator=null,this._direction=null,this._handles=null,this._planeStart=null,this._displayPlaneStart=null,this._originCache=null,this._axisCache=null,this._renderStartCache=null,this._renderEndCache=null,this._resizeOriginCache=null,this._preserveAspectRatio=null}forEachManipulator(t){t(this._manipulator,1)}createDragPipeline(t,r){let c=null,h=null,_=null,d=0,g=null,C=null;const j=this._planeStart,w=this._displayPlaneStart,S=this._direction;return m(this._manipulator,(m,E)=>{E.next(i=>{if("start"===i.action){m.cursor="grabbing";const i=t();c=i.plane,h=i.displayPlane,_=i.editGeometryOperations,d=b*this._view.resolution,p(c,j),p(h,w);const e=u(_.data.spatialReference);g=e?e.valid[1]-e.valid[0]-3*b*this._view.resolution:null}return i}).next(f(this._view)).next(t=>{const i=a(this._renderStartCache,[t.mapStart.x,t.mapStart.y,0]),e=a(this._renderEndCache,[t.mapEnd.x,t.mapEnd.y,0]),r=a(this._resizeOriginCache,w.origin);s(r,r,w.basis1,-S[0]),s(r,r,w.basis2,-S[1]),n(e,e,r),n(i,i,r);const c=0!==S[0]&&0!==S[1],p=y(w),u=y(h)/p,_=(t,r)=>{if(0===t)return 1;let a=o(r),s=.5*t*l(r,e)/a;const n=s<0?-1:1;if(c){s+=(a-.5*t*l(r,i)/a)*n*u}const h=a<1.5*d?1:x;return a=Math.max(a-d,x),n>0&&(s-=b*this._view.resolution),n*Math.max(n*(s/a),h)},m=_(S[0],w.basis1),f=_(S[1],w.basis2);return{...t,direction:S,factor1:m,factor2:f}}).next(this._preserveAspectRatio.createDragEventPipelineStep(),this._preserveAspectRatio.next).next(t=>{const n=a(this._originCache,j.origin);s(n,n,j.basis1,-S[0]),s(n,n,j.basis2,-S[1]);const o=i(this._axisCache,j.basis1[0],j.basis1[1]);e(o,o);const l=_.data.allVerticesUnordered,h="start"===t.action?0:1,u=_.scaleVertices(l,n,o,t.factor1,t.factor2,h,1);return g&&g<_.data.geometry.extent.width&&C?_.updateVertices(l,C):(p(j,c),v(u,c),C=u.operation,r(t,u)),t}).next(t=>("end"===t.action&&(m.cursor="grab"),t))})}_createManipulator(){return new g({view:this._view,graphic:this._graphic,selectable:!0,cursor:"grab"})}}export{j as ScaleManipulation,y as calculateDiagonalResizeHandleScale};
