import{EventEmitter as e}from"events";import{project as t}from"../components/flow-editor";export class Input extends e{constructor(){super(),this.selected=[],this.selectables=[],this.keysCurrentlyHeld=[],this.onkeydown=e=>{this.keysCurrentlyHeld.includes(e.key)||this.keysCurrentlyHeld.push(e.key)},this.onkeyup=e=>{let t=this.keysCurrentlyHeld.indexOf(e.key);-1!==t&&this.keysCurrentlyHeld.splice(t,1)},window.addEventListener("keydown",this.onkeydown),window.addEventListener("keyup",this.onkeyup),window.addEventListener("blur",()=>{this.clearSelection(),this.keysCurrentlyHeld=[]}),t.addListener("cleared",()=>{this.clearSelection()}),this.setMaxListeners(1024)}select(e,t=!1){this.multiSelectedEnabled||t||this.clearSelection(),!this.selected.includes(e)&&(this.emit("selected",e),e.selected&&this.selected.push(e))}deselect(e){let t=this.selected.indexOf(e);-1===t?console.warn("Attempt to deselect nonexistent node"):(this.emit("deselected",e),e.selected||this.selected.splice(t,1))}handleSelection(e,t=!1){!this.multiSelectedEnabled&&this.selected.length>1?this.select(e):this.selected.includes(e)&&!t?this.deselect(e):this.select(e)}clearSelection(e=!1){(!this.multiSelectedEnabled||e)&&(this.selected.forEach(e=>this.emit("deselected",e)),this.selected=[],this.emit("selectioncleared"))}isHeld(e){return this.keysCurrentlyHeld.includes(e)}get multiSelectedEnabled(){return this.isHeld("Shift")||this.isHeld("Control")}}