define(["exports","./../dom.js","./../layoutmanager.js","./../common/inputmanager.js","./../emby-apiclient/events.js","./../common/methodtimer.js","./../focusmanager.js"],function(_exports,_dom,_layoutmanager,_inputmanager,_events,_methodtimer,_focusmanager){Object.defineProperty(_exports,"__esModule",{value:!0}),_exports.default=void 0;var isMouseIdle,mouseManager={},lastMouseInputTime=0;var lastPointerMoveData,TopElement=document.body;function removeIdleClasses(){isMouseIdle=!1,TopElement.classList.remove("mouseIdle"),_events.default.trigger(mouseManager,"mouseidlestop")}function addIdleClasses(){isMouseIdle=!0,TopElement.classList.add("mouseIdle"),_events.default.trigger(mouseManager,"mouseidlestart")}function onPointerMove(e){var obj,eventX=e.screenX,e=e.screenY;void 0===eventX&&void 0===e||((obj=lastPointerMoveData)?Math.abs(eventX-obj.x)<10&&Math.abs(e-obj.y)<10||(obj.x=eventX,obj.y=e,lastMouseInputTime=Date.now(),isMouseIdle&&removeIdleClasses()):lastPointerMoveData={x:eventX,y:e})}var mouseInterval,SupportsTouchEvent="ontouchstart"in document.documentElement,DefaultPointerType="undefined"!=typeof PointerEvent&&"pointerType"in PointerEvent.prototype?null:SupportsTouchEvent?"touch":"mouse";function onPointerEnter(e){"mouse"!==(e.pointerType||DefaultPointerType)||isMouseIdle||(e=e.target).closest&&!e.closest(".nohoverfocus")&&(e=_focusmanager.default.focusableParent(e,!1))&&_focusmanager.default.focus(e)}function onMouseInterval(){!isMouseIdle&&5e3<=Date.now()-lastMouseInputTime&&addIdleClasses()}function stopMouseListening(){mouseInterval&&(mouseInterval.destroy(),mouseInterval=null,_events.default.trigger(mouseManager,"mouselisteningstop")),removeIdleClasses(),_dom.default.removeEventListener(document,"pointermove",onPointerMove,{passive:!0}),_dom.default.removeEventListener(document,"mousemove",onPointerMove,{passive:!0})}function startMouseListening(){(_layoutmanager.default.tv?addIdleClasses:removeIdleClasses)(),mouseInterval||(mouseInterval=new _methodtimer.default({onInterval:onMouseInterval,timeoutMs:5e3,type:"interval"}),_events.default.trigger(mouseManager,"mouselisteningstart")),window.PointerEvent?_dom.default.addEventListener(document,"pointermove",onPointerMove,{passive:!0}):_dom.default.addEventListener(document,"mousemove",onPointerMove,{passive:!0})}var listeners=[];function requestMouseListening(listener){listeners.includes(listener)||(0===listeners.length&&startMouseListening(),listeners.push(listener))}function releaseMouseListening(listener){listener=listeners.indexOf(listener);-1!==listener&&listeners.splice(listener,1),listeners.length||stopMouseListening()}function initMouse(){(_layoutmanager.default.tv?requestMouseListening:releaseMouseListening)("tv"),_dom.default.removeEventListener(document,window.PointerEvent?"pointerenter":"mouseenter",onPointerEnter,{capture:!0,passive:!0}),_layoutmanager.default.tv}function sendCommandFromEvent(name,e){_inputmanager.default.trigger(name,{sourceElement:e.target,originalEvent:e})}window.addEventListener("mouseup",function(e){switch(e.button){case 3:e.stopPropagation(),e.preventDefault(),sendCommandFromEvent("back",e);break;case 4:e.stopPropagation(),e.preventDefault(),sendCommandFromEvent("forward",e)}}),initMouse(),mouseManager.requestMouseListening=requestMouseListening,mouseManager.releaseMouseListening=releaseMouseListening,mouseManager.lastMouseInputTime=function(){return lastMouseInputTime},mouseManager.isListening=function(){return null!=mouseInterval},mouseManager.init=function(){require(["css!mouselistening|modules/input/mouse.css","css!tv,mouselistening|modules/input/mouse_tv.css"]),initMouse()},_events.default.on(_layoutmanager.default,"modechange",initMouse);_exports.default=mouseManager});