define(["events","appStorage"],function(events,appStorage){"use strict";returnfunction(key){functionensure(){if(!credentials){varjson=appStorage.getItem(key)||"{}";console.log("credentials initialized with: "+json),credentials=JSON.parse(json),credentials.Servers=credentials.Servers||[]}}functionget(){returnensure(),credentials}functionset(data){data?(credentials=data,appStorage.setItem(key,JSON.stringify(data))):self.clear(),events.trigger(self,"credentialsupdated")}varself=this,credentials=null;key=key||"servercredentials3",self.clear=function(){credentials=null,appStorage.removeItem(key)},self.credentials=function(data){returndata&&set(data),get()},self.addOrUpdateServer=function(list,server){if(!server.Id)thrownewError("Server.Id cannot be null or empty");varexisting=list.filter(function(s){returns.Id===server.Id})[0];returnexisting?(existing.DateLastAccessed=Math.max(existing.DateLastAccessed||0,server.DateLastAccessed||0),existing.UserLinkType=server.UserLinkType,server.AccessToken&&(existing.AccessToken=server.AccessToken,existing.UserId=server.UserId),server.ExchangeToken&&(existing.ExchangeToken=server.ExchangeToken),server.RemoteAddress&&(existing.RemoteAddress=server.RemoteAddress),server.ManualAddress&&(existing.ManualAddress=server.ManualAddress),server.LocalAddress&&(existing.LocalAddress=server.LocalAddress),server.Name&&(existing.Name=server.Name),server.WakeOnLanInfos&&server.WakeOnLanInfos.length&&(existing.WakeOnLanInfos=server.WakeOnLanInfos),null!=server.LastConnectionMode&&(existing.LastConnectionMode=server.LastConnectionMode),server.ConnectServerId&&(existing.ConnectServerId=server.ConnectServerId),existing):(list.push(server),server)},self.addOrUpdateUser=function(server,user){server.Users=server.Users||[];varexisting=server.Users.filter(function(s){returns.Id===user.Id})[0];existing?existing.IsSignedInOffline=!0:server.Users.push(user)}}});