define(["dom","globalize","appRouter","emby-checkbox","emby-select","emby-input","emby-button"],function(dom,globalize,appRouter){"use strict";function onBackClick(){appRouter.back()}var notificationsConfigurationKey="notifications";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 onSubmit(e){return function(page){var type=getParameterByName("type"),promise1=ApiClient.getNamedConfiguration(notificationsConfigurationKey),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(notificationsConfigurationKey,notificationOptions).then(function(r){Dashboard.processServerConfigurationUpdateResult(),Dashboard.navigate("notificationsettings.html")})})}(dom.parentWithClass(this,"page")),e.preventDefault(),e.stopPropagation(),!1}function onViewShow(){!function(page){var type=getParameterByName("type"),promise1=ApiClient.getUsers(),promise2=ApiClient.getNamedConfiguration(notificationsConfigurationKey),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],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)})}(this)}function onSelectUsersChange(){var view=dom.parentWithClass(this,"page");"Custom"===this.value?view.querySelector(".selectCustomUsers").classList.remove("hide"):view.querySelector(".selectCustomUsers").classList.add("hide")}return function(view,params){view.querySelector(".btnCancel").addEventListener("click",onBackClick),view.querySelector("#selectUsers").addEventListener("change",onSelectUsersChange),view.querySelector(".notificationSettingForm").addEventListener("submit",onSubmit),view.querySelector(".additionalServicesDescription").innerHTML=globalize.translate("AdditionalNotificationServices",'<a is="emby-linkbutton" href="plugins/plugincatalog.html" class="button-link">',"</a>"),view.addEventListener("viewshow",onViewShow)}});