define(["events"],function(events){"use strict";functionredetectBitrate(instance){stopBitrateDetection(instance),instance.accessToken()&&instance.enableAutomaticBitrateDetection!==!1&&setTimeout(redetectBitrateInternal.bind(instance),6e3)}functionredetectBitrateInternal(){this.accessToken()&&this.detectBitrate()}functionstopBitrateDetection(instance){instance.detectTimeout&&clearTimeout(instance.detectTimeout)}functionreplaceAll(originalString,strReplace,strWith){varreg=newRegExp(strReplace,"ig");returnoriginalString.replace(reg,strWith)}functiononFetchFail(instance,url,response){events.trigger(instance,"requestfail",[{url:url,status:response.status,errorCode:response.headers?response.headers.get("X-Application-Error-Code"):null}])}functionparamsToString(params){varvalues=[];for(varkeyinparams){varvalue=params[key];null!==value&&void0!==value&&""!==value&&values.push(encodeURIComponent(key)+"="+encodeURIComponent(value))}returnvalues.join("&")}functionfetchWithTimeout(url,options,timeoutMs){returnnewPromise(function(resolve,reject){vartimeout=setTimeout(reject,timeoutMs);options=options||{},options.credentials="same-origin",fetch(url,options).then(function(response){clearTimeout(timeout),resolve(response)},function(error){clearTimeout(timeout),reject(error)})})}functiongetFetchPromise(request){varheaders=request.headers||{};"json"===request.dataType&&(headers.accept="application/json");varfetchRequest={headers:headers,method:request.type,credentials:"same-origin"},contentType=request.contentType;returnrequest.data&&("string"==typeofrequest.data?fetchRequest.body=request.data:(fetchRequest.body=paramsToString(request.data),contentType=contentType||"application/x-www-form-urlencoded; charset=UTF-8")),contentType&&(headers["Content-Type"]=contentType),request.timeout?fetchWithTimeout(request.url,fetchRequest,request.timeout):fetch(request.url,fetchRequest)}functionApiClient(serverAddress,appName,appVersion,deviceName,deviceId,devicePixelRatio){if(!serverAddress)thrownewError("Must supply a serverAddress");console.log("ApiClient serverAddress: "+serverAddress),console.log("ApiClient appName: "+appName),console.log("ApiClient appVersion: "+appVersion),console.log("ApiClient deviceName: "+deviceName),console.log("ApiClient deviceId: "+deviceId),this._serverInfo={},this._serverAddress=serverAddress,this._deviceId=deviceId,this._deviceName=deviceName,this._appName=appName,this._appVersion=appVersion,this._devicePixelRatio=devicePixelRatio}functionswitchConnectionMode(instance,connectionMode){varcurrentServerInfo=instance.serverInfo(),newConnectionMode=connectionMode;returnnewConnectionMode--,newConnectionMode<0&&(newConnectionMode=MediaBrowser.ConnectionMode.Manual),MediaBrowser.ServerInfo.getServerAddress(currentServerInfo,newConnectionMode)?newConnectionMode:(newConnectionMode--,newConnectionMode<0&&(newConnectionMode=MediaBrowser.ConnectionMode.Manual),MediaBrowser.ServerInfo.getServerAddress(currentServerInfo,newConnectionMode)?newConnectionMode:connectionMode)}functiontryReconnectInternal(instance,resolve,reject,connectionMode,currentRetryCount){connectionMode=switchConnectionMode(instance,connectionMode);varurl=MediaBrowser.ServerInfo.getServerAddress(instance.serverInfo(),connectionMode);console.log("Attempting reconnection to "+url);vartimeout=connectionMode===MediaBrowser.ConnectionMode.Local?7e3:15e3;fetchWithTimeout(url+"/system/info/public",{method:"GET",accept:"application/json"},timeout).then(function(){console.log("Reconnect succeeded to "+url),instance.serverInfo().LastConnectionMode=connectionMode,instance.serverAddress(url),resolve()},function(){if(console.log("Reconnect attempt failed to "+url),currentRetryCount<5){varnewConnectionMode=switchConnectionMode(instance,connectionMode);setTimeout(function(){tryReconnectInternal(instance,resolve,reject,newConnectionMode,currentRetryCount+1)},300)}elsereject()})}functiontryReconnect(instance){returnnewPromise(function(resolve,reject){setTimeout(function(){tryReconnectInternal(instance,resolve,reject,instance.serverInfo().LastCo
})},ApiClient.prototype.addMediaPath=function(virtualFolderName,mediaPath,networkSharePath,refreshLibrary){if(!virtualFolderName)thrownewError("null virtualFolderName");if(!mediaPath)thrownewError("null mediaPath");varurl="Library/VirtualFolders/Paths",pathInfo={Path:mediaPath};returnnetworkSharePath&&(pathInfo.NetworkPath=networkSharePath),url=this.getUrl(url,{refreshLibrary:!!refreshLibrary}),this.ajax({type:"POST",url:url,data:JSON.stringify({Name:virtualFolderName,PathInfo:pathInfo}),contentType:"application/json"})},ApiClient.prototype.updateMediaPath=function(virtualFolderName,pathInfo){if(!virtualFolderName)thrownewError("null virtualFolderName");if(!pathInfo)thrownewError("null pathInfo");varurl="Library/VirtualFolders/Paths/Update";returnurl=this.getUrl(url),this.ajax({type:"POST",url:url,data:JSON.stringify({Name:virtualFolderName,PathInfo:pathInfo}),contentType:"application/json"})},ApiClient.prototype.removeMediaPath=function(virtualFolderName,mediaPath,refreshLibrary){if(!virtualFolderName)thrownewError("null virtualFolderName");if(!mediaPath)thrownewError("null mediaPath");varurl="Library/VirtualFolders/Paths";returnurl=this.getUrl(url,{refreshLibrary:!!refreshLibrary,path:mediaPath,name:virtualFolderName}),this.ajax({type:"DELETE",url:url})},ApiClient.prototype.deleteUser=function(id){if(!id)thrownewError("null id");varurl=this.getUrl("Users/"+id);returnthis.ajax({type:"DELETE",url:url})},ApiClient.prototype.deleteUserImage=function(userId,imageType,imageIndex){if(!userId)thrownewError("null userId");if(!imageType)thrownewError("null imageType");varurl=this.getUrl("Users/"+userId+"/Images/"+imageType);returnnull!=imageIndex&&(url+="/"+imageIndex),this.ajax({type:"DELETE",url:url})},ApiClient.prototype.deleteItemImage=function(itemId,imageType,imageIndex){if(!imageType)thrownewError("null imageType");varurl=this.getUrl("Items/"+itemId+"/Images");returnurl+="/"+imageType,null!=imageIndex&&(url+="/"+imageIndex),this.ajax({type:"DELETE",url:url})},ApiClient.prototype.deleteItem=function(itemId){if(!itemId)thrownewError("null itemId");varurl=this.getUrl("Items/"+itemId);returnthis.ajax({type:"DELETE",url:url})},ApiClient.prototype.stopActiveEncodings=function(playSessionId){varoptions={deviceId:this.deviceId()};playSessionId&&(options.PlaySessionId=playSessionId);varurl=this.getUrl("Videos/ActiveEncodings",options);returnthis.ajax({type:"DELETE",url:url})},ApiClient.prototype.reportCapabilities=function(options){varurl=this.getUrl("Sessions/Capabilities/Full");returnthis.ajax({type:"POST",url:url,data:JSON.stringify(options),contentType:"application/json"})},ApiClient.prototype.updateItemImageIndex=function(itemId,imageType,imageIndex,newIndex){if(!imageType)thrownewError("null imageType");varoptions={newIndex:newIndex},url=this.getUrl("Items/"+itemId+"/Images/"+imageType+"/"+imageIndex+"/Index",options);returnthis.ajax({type:"POST",url:url})},ApiClient.prototype.getItemImageInfos=function(itemId){varurl=this.getUrl("Items/"+itemId+"/Images");returnthis.getJSON(url)},ApiClient.prototype.getCriticReviews=function(itemId,options){if(!itemId)thrownewError("null itemId");varurl=this.getUrl("Items/"+itemId+"/CriticReviews",options);returnthis.getJSON(url)},ApiClient.prototype.getItemDownloadUrl=function(itemId){if(!itemId)thrownewError("itemId cannot be empty");varurl="Items/"+itemId+"/Download";returnthis.getUrl(url,{api_key:this.accessToken()})},ApiClient.prototype.getSessions=function(options){varurl=this.getUrl("Sessions",options);returnthis.getJSON(url)},ApiClient.prototype.uploadUserImage=function(userId,imageType,file){if(!userId)thrownewError("null userId");if(!imageType)thrownewError("null imageType");if(!file)thrownewError("File must be an image.");if("image/png"!==file.type&&"image/jpeg"!==file.type&&"image/jpeg"!==file.type)thrownewError("File must be an image.");varinstance=this;returnnewPromise(function(resolve,reject){varreader=newFileReader;reader.onerror=function(){reject()},reader.onabort=function(){reject()},reader.onload=funct