define(["loading","emby-button","emby-input","emby-select","emby-checkbox"],function(loading){"use strict";function onBackClick(){Dashboard.navigate("livetvsetup/livetvstatus.html")}function getTunerDevices(){return ApiClient.getJSON(ApiClient.getUrl("LiveTv/TunerHosts")).catch(function(){return ApiClient.getNamedConfiguration("livetv").then(function(config){return config.TunerHosts})})}return function(page,params){function refreshListings(value){if(!value)return void(page.querySelector("#selectListing").innerHTML="");loading.show(),ApiClient.ajax({type:"GET",url:ApiClient.getUrl("LiveTv/ListingProviders/Lineups",{Id:providerId,Location:value,Country:country,Type:providerType}),dataType:"json"}).then(function(result){page.querySelector("#selectListing").innerHTML=result.map(function(o){return'<option value="'+o.Id+'">'+o.Name+"</option>"}).join(""),listingsId&&(page.querySelector("#selectListing").value=listingsId),loading.hide()},function(result){Dashboard.alert({message:Globalize.translate("ErrorGettingTvLineups")}),refreshListings(""),loading.hide()})}function refreshTunerDevices(page,providerInfo,devices){for(var html="",i=0,length=devices.length;i<length;i++){var device=devices[i];html+='<div class="listItem">';var enabledTuners=providerInfo.EnabledTuners||[],isChecked=providerInfo.EnableAllTuners||-1!==enabledTuners.indexOf(device.Id),checkedAttribute=isChecked?" checked":"";html+='<label class="checkboxContainer listItemCheckboxContainer"><input type="checkbox" is="emby-checkbox" data-id="'+device.Id+'" class="chkTuner" '+checkedAttribute+"/><span></span></label>",html+='<div class="listItemBody two-line">',html+='<div class="listItemBodyText">',html+=device.FriendlyName||device.Type,html+="</div>",html+='<div class="listItemBodyText secondary">',html+=device.Url,html+="</div>",html+="</div>",html+="</div>"}page.querySelector(".tunerList").innerHTML=html,loading.hide()}function submitListingsForm(e){var selectedListingsId=page.querySelector("#selectListing").value;if(!selectedListingsId)return void Dashboard.alert({message:Globalize.translate("ErrorPleaseSelectLineup")});loading.show();var id=providerId;ApiClient.getNamedConfiguration("livetv").then(function(config){var info;info=id?config.ListingProviders.filter(function(i){return i.Id===id})[0]:{Type:providerType},info.ZipCode=page.querySelector(".txtZipCode").value,info.Country=country,info.ListingsId=selectedListingsId,info.EnableAllTuners=page.querySelector(".chkAllTuners").checked,info.EnabledTuners=info.EnableAllTuners?[]:Array.prototype.filter.call(page.querySelectorAll(".chkTuner"),function(i){return i.checked}).map(function(i){return i.getAttribute("data-id")}),ApiClient.ajax({type:"POST",url:ApiClient.getUrl("LiveTv/ListingProviders",{ValidateListings:!0}),data:JSON.stringify(info),contentType:"application/json"}).then(function(result){loading.hide(),Dashboard.processServerConfigurationUpdateResult(),Dashboard.navigate("livetvsetup/livetvstatus.html")},function(){loading.hide(),Dashboard.alert({message:Globalize.translate("ErrorAddingListingsToSchedulesDirect")})})}),e.preventDefault(),e.stopPropagation()}var listingsId,providerId=params.id,country=params.country,providerType="embygn";page.querySelector(".txtZipCode").addEventListener("change",function(){refreshListings(this.value)}),page.querySelector(".chkAllTuners").addEventListener("change",function(e){e.target.checked?page.querySelector(".selectTunersSection").classList.add("hide"):page.querySelector(".selectTunersSection").classList.remove("hide")}),page.querySelector(".btnCancel").addEventListener("click",onBackClick),page.querySelector("form").addEventListener("submit",submitListingsForm),function(){loading.show();var promises=[ApiClient.getNamedConfiguration("livetv"),getTunerDevices()];Promise.all(promises).then(function(responses){var config=responses[0],tunerHosts=responses[1],info=config.ListingProviders.filter(function(i){return i.Id===providerId})[0]||{EnableAllTuners:!0};country=info.Country||params.country,listingsId=info.ListingsId,page.querySelector("#selectListing").value=info.ListingsId||"",page.querySelector(".txtZipCode").value=info.ZipCode||"",page.querySelector(".chkAllTuners").checked=info.EnableAllTuners,page.querySelector(".chkAllTuners").checked?page.querySelector(".selectTunersSection").classList.add("hide"):page.querySelector(".selectTunersSection").classList.remove("hide"),refreshTunerDevices(page,info,tunerHosts),refreshListings(info.ZipCode||"")})}()}});