Liferay.Loader.define("commerce-organization-web@5.0.46/js/utils/DndHandler",["module","exports","require","./constants"],(function(t,e,a){window;{Object.defineProperty(e,"__esModule",{value:!0}),e.hasPositionChanged=hasPositionChanged,e.default=void 0;var s=a("./constants");function hasPositionChanged(t,e){if(!e)return!1;return Math.max(Math.abs(t.x-e.x),Math.abs(t.y-e.y))>s.DRAGGING_THRESHOLD}class DndHandler{constructor(){this._mouseStartPositions=null,this._dragging=!1,this._chartItems=null,this._dragHandle=null}_handleMouseDown(t){this._mouseStartPositions={x:t.offsetX,y:t.offsetY}}_createDragHandle(t,e,a){this._dragHandle=a.append("g").attr("transform",t.attributes.transform.value).attr("class","dnd-handle");const s=this._dragHandle.append("g").attr("class","dnd-handle-content"),n=t.cloneNode(!0);n.classList.remove("highlighted"),n.removeAttribute("transform");const d=Math.min(3,e-1),o=n.querySelector("rect");for(let t=d;t>0;t--)s.append((()=>o.cloneNode())).attr("x",3*t).attr("y",3*t);s.append((()=>n))}_handleMouseMove(t,e,a,n,d,o,r){if(!this._dragging&&hasPositionChanged(this._mouseStartPositions,{x:t.offsetX,y:t.offsetY})){this._dragging=!0;const t=new Map;this._chartItems=d.selectAll(".chart-item"),this._chartItems.each((e=>{if(["user","account","add"].includes(e.data.type)&&t.set(e.data.chartNodeId,e),n.has(e.data.chartNodeId)){t.set(e.data.chartNodeId,e),e.parent&&t.set(e.parent.data.chartNodeId,e.parent);e.descendants().forEach((e=>{t.set(e.data.chartNodeId,e)}))}})),this._createDragHandle(a,n.size,d),this._chartItems.each(((e,a,s)=>{t.has(e.data.chartNodeId)?s[a].classList.add("drop-not-allowed"):s[a].classList.add("drop-allowed")})),r.classList.add("dragging")}if(this._dragHandle){const a={x:(this._mouseStartPositions.x-t.offsetX)*(s.ZOOM_EXTENT[1]/o)*-1+e.y,y:(this._mouseStartPositions.y-t.offsetY)*(s.ZOOM_EXTENT[1]/o)*-1+e.x};this._dragHandle.attr("transform","translate(".concat(a.x,", ").concat(a.y,")")).classed("dragging",!0)}}_handleMouseUp(t,e,a,s,n){if(!this._dragging)return n({d:a,event:t,type:"click"});this._mouseStartPositions=null,this._dragging=!1,s.classList.remove("dragging");const d=e.target.closest(".drop-allowed");return this._chartItems.each(((t,e,a)=>{a[e].classList.remove("drop-not-allowed"),a[e].classList.remove("drop-allowed")})),this._dragHandle.remove(),n({d:a,event:t,target:null==d?void 0:d.__data__,type:"drop"})}handleMouseEvent(t,e,a,s,n,d){return new Promise((o=>{this._handleMouseDown(t);const r=a.has(e.data.chartNodeId)?a:new Set([e.data.chartNodeId]),i=t.currentTarget,_handleMouseMove=t=>this._handleMouseMove(t,e,i,r,n,d,s);s.addEventListener("mousemove",_handleMouseMove,this),window.addEventListener("mouseup",(a=>{s.removeEventListener("mousemove",_handleMouseMove),this._handleMouseUp(t,a,e,s,o)}),{once:!0})}))}}e.default=DndHandler}}));
//# sourceMappingURL=DndHandler.js.map