define(["dom","loading","globalize","appRouter","emby-checkbox","emby-input","emby-button"],function(dom,loading,globalize,appRouter){"use strict";function loadUser(page){page.querySelector(".txtUserName").value="",loading.show();var promise4=ApiClient.getJSON(ApiClient.getUrl("Library/SelectableMediaFolders",{IsHidden:!1})),promise5=ApiClient.getJSON(ApiClient.getUrl("Channels"));Promise.all([promise4,promise5]).then(function(responses){!function(page,mediaFolders){var html="";html+='<h3 class="checkboxListLabel">'+globalize.translate("HeaderLibraries")+"</h3>",html+='<div class="checkboxList paperList" style="padding:.5em 1em;">';for(var i=0,length=mediaFolders.length;i<length;i++){var folder=mediaFolders[i];html+='<label><input type="checkbox" is="emby-checkbox" class="chkFolder" data-id="'+folder.Id+'" checked="checked"/><span>'+folder.Name+"</span></label>"}html+="</div>",page.querySelector(".folderAccess").innerHTML=html,page.querySelector(".chkEnableAllFolders").checked=!0,page.querySelector(".chkEnableAllFolders").dispatchEvent(new CustomEvent("change",{bubbles:!0,cancelable:!1}))}(page,responses[0]),function(page,channels){var html="";html+='<h3 class="checkboxListLabel">'+globalize.translate("HeaderChannels")+"</h3>",html+='<div class="checkboxList paperList" style="padding:.5em 1em;">';for(var i=0,length=channels.length;i<length;i++){var folder=channels[i];html+='<label><input type="checkbox" is="emby-checkbox" class="chkChannel" data-id="'+folder.Id+'" checked="checked"/><span>'+folder.Name+"</span></label>"}html+="</div>",page.querySelector(".channelAccess").innerHTML=html,page.querySelector(".channelAccess").classList.remove("hide"),channels.length?page.querySelector(".channelAccessContainer").classList.remove("hide"):page.querySelector(".channelAccessContainer").classList.add("hide"),page.querySelector(".chkEnableAllChannels").checked=!0,page.querySelector(".chkEnableAllChannels").dispatchEvent(new CustomEvent("change",{bubbles:!0,cancelable:!1}))}(page,responses[1].Items),loading.hide()})}function saveUser(page){var name=page.querySelector(".txtUserName").value;ApiClient.createUser(name).then(function(user){user.Policy.IsAdministrator=page.querySelector(".chkIsAdmin").checked,user.Policy.EnableSubtitleDownloading=user.Policy.IsAdministrator,user.Policy.EnableSubtitleManagement=user.Policy.IsAdministrator,user.Policy.EnableContentDeletion=user.Policy.IsAdministrator,user.Policy.EnableAllFolders=page.querySelector(".chkEnableAllFolders").checked,user.Policy.EnabledFolders=user.Policy.EnableAllFolders?[]:Array.prototype.filter.call(page.querySelectorAll(".chkFolder"),function(i){return i.checked}).map(function(i){return i.getAttribute("data-id")}),user.Policy.EnableAllChannels=page.querySelector(".chkEnableAllChannels").checked,user.Policy.EnabledChannels=user.Policy.EnableAllChannels?[]:Array.prototype.filter.call(page.querySelectorAll(".chkChannel"),function(i){return i.checked}).map(function(i){return i.getAttribute("data-id")}),ApiClient.updateUserPolicy(user.Id,user.Policy).then(function(){!function(page,user){var enteredConnectUsername=page.querySelector(".txtConnectUserName").value;enteredConnectUsername?require(["connectHelper"],function(connectHelper){connectHelper.updateUserLink(ApiClient,user,enteredConnectUsername).then(function(){appRouter.show("users/user?userId="+user.Id)})}):appRouter.show("users/user?userId="+user.Id)}(page,user)})},function(response){var options;400===response.status?(options={text:page.querySelector(".labelNewUserNameHelp").innerHTML},require(["alert"]).then(function(responses){responses[0](options)})):require(["toast"],function(toast){toast(globalize.translate("DefaultErrorMessage"))}),loading.hide()})}function onSubmit(e){var page=this.closest(".page");return loading.show(),saveUser(page),e.preventDefault(),e.stopPropagation(),!1}return function(view,params){view.querySelector(".btnCancel").addEventListener("click",function(){appRouter.back()}),view.querySelector(".chkEnableAllChannels").addEventListener("change",function(){this.checked?view.querySelector(".channelAccessListContainer").classList.add("hide"):view.querySelector(".channelAccessListContainer").classList.remove("hide")}),view.querySelector(".chkEnableAllFolders").addEventListener("change",function(){this.checked?view.querySelector(".folderAccessListContainer").classList.add("hide"):view.querySelector(".folderAccessListContainer").classList.remove("hide")}),view.querySelector(".newUserProfileForm").addEventListener("submit",onSubmit),view.addEventListener("viewshow",function(){loadUser(this)})}});