/* COPYRIGHT Esri - https://js.arcgis.com/5.0/LICENSE.txt */
import t from"../../../../../core/Handles.js";import{copy as i}from"../../../../../core/libs/gl-matrix-2/math/vec3.js";import{create as r}from"../../../../../core/libs/gl-matrix-2/factories/vec3f64.js";import{Manipulation as a}from"./Manipulation.js";import{onGrabChangedHandle as o}from"./utils.js";import{getRotationAngle as e}from"../../../../draw/support/drawUtils.js";import{createManipulatorDragEventPipeline as n,screenToMap as s}from"../../../../interactive/dragEventPipeline.js";import{GraphicManipulator as l}from"../../../../interactive/GraphicManipulator.js";import{apply as h}from"../../../../interactive/editGeometry/support/editPlaneUtils.js";class p extends a{constructor(i){super(),this._handles=new t,this._originCache=r(),this._view=i.view,this._tool=i.tool,this._graphic=i.graphic,this._snapRotation=i.snapRotation,this._manipulator=this._createManipulator(),this._handles.add([this._manipulator.events.on("grab-changed",t=>o(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._manipulator=null,this._snapRotation=null,this._graphic=null,this._handles=null,this._originCache=null}forEachManipulator(t){t(this._manipulator,2)}createDragPipeline(t,r){let a=null,o=null;return n(this._manipulator,(n,l)=>{l.next(i=>{if("start"===i.action){n.cursor="grabbing";const i=t();a=i.plane,o=i.editGeometryOperations}return i}).next(s(this._view)).next(t=>({...t,rotateAngle:e(t.mapStart,t.mapEnd,{x:a.origin[0],y:a.origin[1]},!0)})).next(this._snapRotation.createDragEventPipelineStep(),this._snapRotation.next).next(t=>{const e=i(this._originCache,a.origin),n="start"===t.action?0:1,s=o.rotate(e,t.rotateAngle,n,1);return h(s,a),r(t,s),t}).next(t=>("end"===t.action&&(n.cursor="grab"),t))})}_createManipulator(){const t=this._view,i=this._graphic;return new l({view:t,graphic:i,selectable:!0,cursor:"grab"})}}export{p as RotateManipulation};
