define(["dom","connectionManager","globalize","appRouter","formHelper","emby-checkbox","emby-select","emby-input","emby-button"],function(dom,connectionManager,globalize,appRouter,formHelper){"use strict";function onBackClick(){appRouter.back()}function fillItems(elem,items,cssClass,idPrefix,currentList,isEnabledList){var html='<div class="checkboxList paperList" style="padding: .5em 1em;">';html+=items.map(function(u){var checkedHtml=(isEnabledList?-1!==currentList.indexOf(u.Id):-1===currentList.indexOf(u.Id))?' checked="checked"':"";return'<label><input is="emby-checkbox" class="'+cssClass+'" type="checkbox" data-itemid="'+u.Id+'"'+checkedHtml+"/><span>"+u.Name+"</span></label>"}).join(""),html+="</div>",elem.innerHTML=html}function onSelectUsersChange(){var view=this.closest(".page");"Custom"===this.value?view.querySelector(".selectCustomUsers").classList.remove("hide"):view.querySelector(".selectCustomUsers").classList.add("hide")}return function(view,params){var apiClient=connectionManager.currentApiClient();view.querySelector(".btnCancel").addEventListener("click",onBackClick),view.querySelector("#selectUsers").addEventListener("change",onSelectUsersChange),view.querySelector(".notificationSettingForm").addEventListener("submit",function(e){return function(page){var type=params.type,promise1=apiClient.getNamedConfiguration("notifications"),promise2=apiClient.getJSON(apiClient.getUrl("Notifications/Types"));Promise.all([promise1,promise2]).then(function(responses){var notificationOptions=responses[0],types=responses[1],notificationConfig=notificationOptions.Options.filter(function(n){return n.Type===type})[0];notificationConfig||(notificationConfig={Type:type},notificationOptions.Options.push(notificationConfig));types.filter(function(n){return n.Type===type})[0];notificationConfig.Enabled=page.querySelector("#chkEnabled").checked,notificationConfig.SendToUserMode=page.querySelector("#selectUsers").value,notificationConfig.DisabledMonitorUsers=Array.prototype.filter.call(page.querySelectorAll(".chkMonitor"),function(c){return!c.checked}).map(function(c){return c.getAttribute("data-itemid")}),notificationConfig.SendToUsers=Array.prototype.filter.call(page.querySelectorAll(".chkSendTo"),function(c){return c.checked}).map(function(c){return c.getAttribute("data-itemid")}),notificationConfig.DisabledServices=Array.prototype.filter.call(page.querySelectorAll(".chkService"),function(c){return!c.checked}).map(function(c){return c.getAttribute("data-itemid")}),apiClient.updateNamedConfiguration("notifications",notificationOptions).then(function(r){formHelper.handleConfigurationSavedResponse(),appRouter.show("notificationsettings.html")})})}(this.closest(".page")),e.preventDefault(),e.stopPropagation(),!1}),view.querySelector(".additionalServicesDescription").innerHTML=globalize.translate("AdditionalNotificationServices",'<a is="emby-linkbutton" href="plugincatalog" class="button-link">',"</a>"),view.addEventListener("viewshow",function(){var page,type,promise1,promise2,promise3,promise4;page=this,type=params.type,promise1=apiClient.getUsersQueryResult(),promise2=apiClient.getNamedConfiguration("notifications"),promise3=apiClient.getJSON(apiClient.getUrl("Notifications/Types")),promise4=apiClient.getJSON(apiClient.getUrl("Notifications/Services")),Promise.all([promise1,promise2,promise3,promise4]).then(function(responses){var users=responses[0].Items,notificationOptions=responses[1],types=responses[2],services=responses[3],notificationConfig=notificationOptions.Options.filter(function(n){return n.Type===type})[0],typeInfo=types.filter(function(n){return n.Type===type})[0]||{};typeInfo.IsBasedOnUserEvent?page.querySelector(".monitorUsers").classList.remove("hide"):page.querySelector(".monitorUsers").classList.add("hide"),page.querySelector(".notificationType").innerHTML=typeInfo.Name||"Unknown Notification",notificationConfig=notificationConfig||{DisabledMonitorUsers:[],SendToUsers:[],DisabledServices:[],SendToUserMode:"Admins"},fillItems(page.querySelector(".monitorUsersList"),users,"chkMonitor",0,notificationConfig.DisabledMonitorUsers),fillItems(page.querySelector(".sendToUsersList"),users,"chkSendTo",0,notificationConfig.SendToUsers,!0),fillItems(page.querySelector(".servicesList"),services,"chkService",0,notificationConfig.DisabledServices),page.querySelector("#chkEnabled").checked=notificationConfig.Enabled||!1;var selectUsers=page.querySelector("#selectUsers");selectUsers.value=notificationConfig.SendToUserMode,onSelectUsersChange.call(selectUsers)})})}});