import*as Common from"../common/common.js";import{elementDragStart}from"./UIUtils.js";export class ResizerWidget extends Common.ObjectWrapper.ObjectWrapper{constructor(){super(),this._isEnabled=!0,this._elements=new Set,this._installDragOnMouseDownBound=this._installDragOnMouseDown.bind(this),this._cursor="nwse-resize"}isEnabled(){return this._isEnabled}setEnabled(t){this._isEnabled=t,this.updateElementCursors()}elements(){return[...this._elements]}addElement(t){this._elements.has(t)||(this._elements.add(t),t.addEventListener("mousedown",this._installDragOnMouseDownBound,!1),this._updateElementCursor(t))}removeElement(t){this._elements.has(t)&&(this._elements.delete(t),t.removeEventListener("mousedown",this._installDragOnMouseDownBound,!1),t.style.removeProperty("cursor"))}updateElementCursors(){this._elements.forEach(this._updateElementCursor.bind(this))}_updateElementCursor(t){this._isEnabled?t.style.setProperty("cursor",this.cursor()):t.style.removeProperty("cursor")}cursor(){return this._cursor}setCursor(t){this._cursor=t,this.updateElementCursors()}_installDragOnMouseDown(t){const e=t.target;if(!this._elements.has(e))return!1;elementDragStart(e,this._dragStart.bind(this),(t=>{this._drag(t)}),this._dragEnd.bind(this),this.cursor(),t)}_dragStart(t){return!!this._isEnabled&&(this._startX=t.pageX,this._startY=t.pageY,this.sendDragStart(this._startX,this._startY),!0)}sendDragStart(t,e){this.dispatchEventToListeners(Events.ResizeStart,{startX:t,currentX:t,startY:e,currentY:e})}_drag(t){return this._isEnabled?(this.sendDragMove(this._startX,t.pageX,this._startY,t.pageY,t.shiftKey),t.preventDefault(),!1):(this._dragEnd(t),!0)}sendDragMove(t,e,s,r,i){this.dispatchEventToListeners(Events.ResizeUpdate,{startX:t,currentX:e,startY:s,currentY:r,shiftKey:i})}_dragEnd(t){this.dispatchEventToListeners(Events.ResizeEnd),delete this._startX,delete this._startY}}export const Events={ResizeStart:Symbol("ResizeStart"),ResizeUpdate:Symbol("ResizeUpdate"),ResizeEnd:Symbol("ResizeEnd")};export class SimpleResizerWidget extends ResizerWidget{constructor(){super(),this._isVertical=!0}isVertical(){return this._isVertical}setVertical(t){this._isVertical=t,this.updateElementCursors()}cursor(){return this._isVertical?"ns-resize":"ew-resize"}sendDragStart(t,e){const s=this._isVertical?e:t;this.dispatchEventToListeners(Events.ResizeStart,{startPosition:s,currentPosition:s})}sendDragMove(t,e,s,r,i){this._isVertical?this.dispatchEventToListeners(Events.ResizeUpdate,{startPosition:s,currentPosition:r,shiftKey:i}):this.dispatchEventToListeners(Events.ResizeUpdate,{startPosition:t,currentPosition:e,shiftKey:i})}}