import{hasClass as t}from"./lib/dom.js";import{EventHandler as i}from"./lib/events.js";import{elementsFromPoint as e}from"./lib/shadow-dom-utils.js";import{clampNumber as o}from"./lib/utils.js";import{AnimationRunner as s}from"./animation-runner.js";class n{constructor(t){this._host=t,this.onClick=this.onClick.bind(this),this.onClickLog=this.onClickLog.bind(this),this.onKeyDown=this.onKeyDown.bind(this),this.onKeyUp=this.onKeyUp.bind(this),this.onMouseDownGutter=this.onMouseDownGutter.bind(this),this._windowMouseMoveHandler=new i(window,"mousemove",this._onDrag.bind(this)),this._windowMouseUpHandler=new i(window,"mouseup",this._onDragStop.bind(this)),this._arrowKeysAnimationRunner=new s,this._dragAnimationRunner=new s,this._keyModifiers={ctrl:!1,shift:!1},this._dragIndex=null}onKeyDown(t){if("Escape"===t.key&&this._host._onEscape(),"Control"===t.key||"Meta"===t.key)this._keyModifiers.ctrl=!0;else if("Shift"===t.key)this._keyModifiers.shift=!0;else if("ArrowDown"===t.key||"ArrowUp"===t.key){t.preventDefault();const i="ArrowDown"===t.key?"down":"up";if(this._arrowKeysAnimationRunner.isStopped()){let t=!1;this._arrowKeysAnimationRunner.start(((e,o,s)=>{const n=e===o;t||=e-o>100;return!!(n||t&&e-s>25)&&(this._host._onArrow(i),!0)}))}}else if("c"===t.key&&this._keyModifiers.ctrl)this._host._onCopySelectionToClipboard();else if("Enter"===t.key||" "===t.key){t.preventDefault();const i=Number(t.target.closest(".log").dataset.index);this._host._onClickLog(i,this._keyModifiers)}else"a"===t.key&&this._keyModifiers.ctrl?(t.preventDefault(),this._host._onSelectAll()):"Home"===t.key?this._host._onHome(this._keyModifiers.ctrl&&this._keyModifiers.shift):"End"===t.key&&this._host._onEnd(this._keyModifiers.ctrl&&this._keyModifiers.shift)}onKeyUp(t){"Control"===t.key||"Meta"===t.key?this._keyModifiers.ctrl=!1:"Shift"===t.key?this._keyModifiers.shift=!1:"ArrowDown"!==t.key&&"ArrowUp"!==t.key||this._arrowKeysAnimationRunner.stop()}onClick(t){"stopping"!==this._dragState&&this._host._onClick(t)}onClickLog(t){t.stopPropagation();const i=Number(t.target.closest(".log").dataset.index);this._host._onClickLog(i,this._keyModifiers)}onMouseDownGutter(){this._windowMouseMoveHandler.connect(),this._windowMouseUpHandler.connect(),this._dragState="init"}_onDrag(i){if(i.ctrlKey||i.shiftKey)return;const e="init"===this._dragState;this._dragState="dragging";const{position:s,distance:n}=this._getCursorPosition(i,this._host);if("inside"===s||"left"===s||"right"===s){this._dragAnimationRunner.stop();const o=this._getElementsFromPoint(i,s,n).find((i=>i instanceof HTMLElement&&t(i,"log"))),r=Number(o.dataset.index);this._dragIndex!==r&&(this._host._onDrag({logIndex:r},e),this._dragIndex=r)}if("above"===s||"below"===s){const t="above"===s?"up":"down",i=o(n,1,50);this._dragAnimationPeriodMs=Math.ceil(300-280*i/49),this._dragAnimationLogOffset=Math.ceil(9*i/49),this._dragAnimationRunner.isStopped()&&this._dragAnimationRunner.start(((i,o,s)=>!!(i-s>this._dragAnimationPeriodMs)&&(this._host._onDrag({direction:t,offset:this._dragAnimationLogOffset},e),!0)))}}_onDragStop(){this._windowMouseMoveHandler.disconnect(),this._windowMouseUpHandler.disconnect(),this._dragAnimationRunner.stop(),this._dragState="stopping",setTimeout((()=>{this._dragState="stopped"}),0)}_getElementsFromPoint(t,i,o){if("inside"===i)return t.composedPath();if("left"===i||"right"===i){const s=t.clientX+("left"===i?1:-1)*(o+this._host.offsetWidth/2),n=t.clientY;return e(s,n)}return[]}_getCursorPosition(t,i){const e=i.offsetWidth,o=i.offsetHeight,s=this._getRelativeCoordinates(t,i);return s.y<0?{position:"above",distance:-s.y}:s.y>o?{position:"below",distance:s.y-o}:s.x<0?{position:"left",distance:-s.x}:s.x>e?{position:"right",distance:s.x-e}:{position:"inside"}}_getRelativeCoordinates(t,i){const e=t.pageX,o=t.pageY,s={left:i.offsetLeft,top:i.offsetTop};let n=i.offsetParent;for(;null!=n&&n instanceof HTMLElement;)s.left+=n.offsetLeft,s.top+=n.offsetTop,n=n.offsetParent;return{x:e-s.left,y:o-s.top}}}export{n as LogsInputController};
//# sourceMappingURL=logs-input-controller.js.map
