define(["globalize","apphost","loading","alert","emby-linkbutton"],function(globalize,appHost,loading,alert){"use strict";function resolvePromise(){return Promise.resolve()}function rejectPromise(){return Promise.reject()}function showConnectServerUnreachableErrorMessage(){var text=globalize.translate("ErrorConnectServerUnreachable","https://connect.emby.media");return alert({text:text})}function showLinkUserErrorMessage(username,statusCode){var html,text;return 502===statusCode?showConnectServerUnreachableErrorMessage():(409===statusCode?text=globalize.translate("EmbyConnectUserAlreadyLinked"):username?(appHost.supports("externallinks")&&(html=globalize.translate("ErrorAddingEmbyConnectAccount1",'<a is="emby-linkbutton" class="button-link" href="https://emby.media/connect" target="_blank">https://emby.media/connect</a>'),html+="<br/><br/>"+globalize.translate("ErrorAddingEmbyConnectAccount2","support@emby.media")),text=globalize.translate("ErrorAddingEmbyConnectAccount1","https://emby.media/connect"),text+="\n\n"+globalize.translate("ErrorAddingEmbyConnectAccount2","support@emby.media")):html=text=globalize.translate("DefaultErrorMessage"),alert({text:text,html:html}))}return{updateUserLink:function(apiClient,user,newConnectUsername){var currentConnectUsername=user.ConnectUserName||"",enteredConnectUsername=newConnectUsername,linkUrl=apiClient.getUrl("Users/"+user.Id+"/Connect/Link");return currentConnectUsername&&!enteredConnectUsername?apiClient.ajax({type:"DELETE",url:linkUrl}).then(function(){return loading.hide(),alert({text:globalize.translate("MessageEmbyAccontRemoved"),title:globalize.translate("HeaderEmbyAccountRemoved")}).catch(resolvePromise)},function(response){return loading.hide(),502===(response?response.status:0)?showConnectServerUnreachableErrorMessage().then(rejectPromise):alert({text:globalize.translate("ErrorRemovingEmbyConnectAccount")}).then(rejectPromise)}):currentConnectUsername!==enteredConnectUsername?apiClient.ajax({type:"POST",url:linkUrl,data:{ConnectUsername:enteredConnectUsername},dataType:"json"}).then(function(result){var msgKey=result.IsPending?"MessagePendingEmbyAccountAdded":"MessageEmbyAccountAdded";return loading.hide(),alert({text:globalize.translate(msgKey),title:globalize.translate("HeaderEmbyAccountAdded")}).catch(resolvePromise)},function(response){loading.hide();var statusCode=response?response.status:0;return 502===statusCode?showConnectServerUnreachableErrorMessage().then(rejectPromise):showLinkUserErrorMessage(".",statusCode).then(rejectPromise)}):Promise.reject()},showLinkUserErrorMessage:showLinkUserErrorMessage,showConnectServerUnreachableErrorMessage:showConnectServerUnreachableErrorMessage}});