define(["exports","./../dom.js","./../layoutmanager.js","./../common/inputmanager.js","./../emby-apiclient/events.js","./../common/methodtimer.js","./../focusmanager.js","./../approuter.js"],function(_exports,_dom,_layoutmanager,_inputmanager,_events,_methodtimer,_focusmanager,_approuter){var _styleElement;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","mouseIdle-tv"),_events.default.trigger(mouseManager,"mouseidlestop")}function addIdleClasses(){isMouseIdle=!0;var classList=TopElement.classList;_layoutmanager.default.tv?classList.add("mouseIdle","mouseIdle-tv"):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(){_styleElement.remove(),mouseInterval&&(mouseInterval.destroy(),mouseInterval=null),removeIdleClasses(),_dom.default.removeEventListener(document,"pointermove",onPointerMove,{passive:!0}),_dom.default.removeEventListener(document,"mousemove",onPointerMove,{passive:!0})}function startMouseListening(){document.head.appendChild(_styleElement),(_layoutmanager.default.tv?addIdleClasses:removeIdleClasses)(),mouseInterval=mouseInterval||new _methodtimer.default({onInterval:onMouseInterval,timeoutMs:5e3,type:"interval"}),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(){var linkUrl,link;linkUrl=_approuter.default.baseUrl()+"/modules/input/mouse.css",globalThis.urlCacheParam&&(linkUrl+="?"+globalThis.urlCacheParam),(link=document.createElement("link")).setAttribute("rel","stylesheet"),link.setAttribute("type","text/css"),link.setAttribute("href",linkUrl),_styleElement=link,document.head.appendChild(link),(_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},_events.default.on(_layoutmanager.default,"modechange",initMouse);_exports.default=mouseManager});