define(["events"],function(events){"use strict";returnfunction(serverAddress,clientName,applicationVersion,deviceName,deviceId,devicePixelRatio){functionredetectBitrate(){stopBitrateDetection(),self.accessToken()&&self.enableAutomaticBitrateDetection!==!1&&setTimeout(redetectBitrateInternal,6e3)}functionredetectBitrateInternal(){self.detectBitrate()}functionstopBitrateDetection(){detectTimeout&&clearTimeout(detectTimeout)}functiononFetchFail(url,response){events.trigger(self,"requestfail",[{url:url,status:response.status,errorCode:response.headers?response.headers.get("X-Application-Error-Code"):null}])}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)}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)})})}functionparamsToString(params){varvalues=[];for(varkeyinparams){varvalue=params[key];null!==value&&void0!==value&&""!==value&&values.push(encodeURIComponent(key)+"="+encodeURIComponent(value))}returnvalues.join("&")}functionswitchConnectionMode(connectionMode){varcurrentServerInfo=self.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(resolve,reject,connectionMode,currentRetryCount){connectionMode=switchConnectionMode(connectionMode);varurl=MediaBrowser.ServerInfo.getServerAddress(self.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),self.serverInfo().LastConnectionMode=connectionMode,self.serverAddress(url),resolve()},function(){if(console.log("Reconnect attempt failed to "+url),currentRetryCount<5){varnewConnectionMode=switchConnectionMode(connectionMode);setTimeout(function(){tryReconnectInternal(resolve,reject,newConnectionMode,currentRetryCount+1)},300)}elsereject()})}functiontryReconnect(){returnnewPromise(function(resolve,reject){setTimeout(function(){tryReconnectInternal(resolve,reject,self.serverInfo().LastConnectionMode,0)},300)})}functionreplaceAll(originalString,strReplace,strWith){varreg=newRegExp(strReplace,"ig");returnoriginalString.replace(reg,strWith)}functiononWebSocketMessage(msg){if("UserDeleted"===msg.MessageType)currentUser=null;elseif("UserUpdated"===msg.MessageType||"UserConfigurationUpdated"===msg.MessageType){varuser=msg.Data;user.Id===self.getCurrentUserId()&&(currentUser=null)}events.trigger(self,"websocketmessage",[msg])}functionnormalizeReturnBitrate(bitrate){if(!bitrate)returnlastDetectedBitrate?lastDetectedBitrate:Promise.reject();varresult=Math.round(.8*bitrate);returnlastDetectedBitrate=result,lastDetectedBitrateTime=(newDate).getTime(),result}functiondetectBitrateInternal(tests,index,currentBitrate){if(index>=tests.length)returnnormalizeReturnBitrate(currentBitr
contentType:"image/"+file.name.substring(file.name.lastIndexOf(".")+1)}).then(function(result){resolve(result)},function(){reject()})},reader.readAsDataURL(file)})},self.uploadItemImage=function(itemId,imageType,file){if(!itemId)thrownewError("null itemId");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.");varurl=self.getUrl("Items/"+itemId+"/Images");returnurl+="/"+imageType,newPromise(function(resolve,reject){varreader=newFileReader;reader.onerror=function(){reject()},reader.onabort=function(){reject()},reader.onload=function(e){vardata=e.target.result.split(",")[1];self.ajax({type:"POST",url:url,data:data,contentType:"image/"+file.name.substring(file.name.lastIndexOf(".")+1)}).then(function(result){resolve(result)},function(){reject()})},reader.readAsDataURL(file)})},self.getInstalledPlugins=function(){varoptions={},url=self.getUrl("Plugins",options);returnself.getJSON(url)},self.getUser=function(id){if(!id)thrownewError("Must supply a userId");varurl=self.getUrl("Users/"+id);returnself.getJSON(url)},self.getOfflineUser=function(id){if(!id)thrownewError("Must supply a userId");varurl=self.getUrl("Users/"+id+"/Offline");returnself.getJSON(url)},self.getStudio=function(name,userId){if(!name)thrownewError("null name");varoptions={};userId&&(options.userId=userId);varurl=self.getUrl("Studios/"+self.encodeName(name),options);returnself.getJSON(url)},self.getGenre=function(name,userId){if(!name)thrownewError("null name");varoptions={};userId&&(options.userId=userId);varurl=self.getUrl("Genres/"+self.encodeName(name),options);returnself.getJSON(url)},self.getMusicGenre=function(name,userId){if(!name)thrownewError("null name");varoptions={};userId&&(options.userId=userId);varurl=self.getUrl("MusicGenres/"+self.encodeName(name),options);returnself.getJSON(url)},self.getGameGenre=function(name,userId){if(!name)thrownewError("null name");varoptions={};userId&&(options.userId=userId);varurl=self.getUrl("GameGenres/"+self.encodeName(name),options);returnself.getJSON(url)},self.getArtist=function(name,userId){if(!name)thrownewError("null name");varoptions={};userId&&(options.userId=userId);varurl=self.getUrl("Artists/"+self.encodeName(name),options);returnself.getJSON(url)},self.getPerson=function(name,userId){if(!name)thrownewError("null name");varoptions={};userId&&(options.userId=userId);varurl=self.getUrl("Persons/"+self.encodeName(name),options);returnself.getJSON(url)},self.getPublicUsers=function(){varurl=self.getUrl("users/public");returnself.ajax({type:"GET",url:url,dataType:"json"},!1)},self.getUsers=function(options){varurl=self.getUrl("users",options||{});returnself.getJSON(url)},self.getParentalRatings=function(){varurl=self.getUrl("Localization/ParentalRatings");returnself.getJSON(url)},self.getDefaultImageQuality=function(imageType){return"backdrop"===imageType.toLowerCase()?80:90},self.getUserImageUrl=function(userId,options){if(!userId)thrownewError("null userId");options=options||{};varurl="Users/"+userId+"/Images/"+options.type;returnnull!=options.index&&(url+="/"+options.index),normalizeImageOptions(options),deleteoptions.type,deleteoptions.index,self.getUrl(url,options)},self.getImageUrl=function(itemId,options){if(!itemId)thrownewError("itemId cannot be empty");options=options||{};varurl="Items/"+itemId+"/Images/"+options.type;returnnull!=options.index&&(url+="/"+options.index),options.quality=options.quality||self.getDefaultImageQuality(options.type),self.normalizeImageOptions&&self.normalizeImageOptions(options),deleteoptions.type,deleteoptions.index,self.getUrl(url,options)},self.getScaledImageUrl=function(itemId,options){if(!itemId)thrownewError("itemId cannot be empty");options=options||{};varurl="Items/"+itemId+"/Images/"+options.type;returnnull!=options.index&&(url+="/"+options.index),normalizeImageOptions(options),deleteoptions.type,deleteoptions.index,deleteopti