define(["loading","globalize","emby-button","emby-input","emby-select","emby-checkbox"],function(loading,globalize){"use strict";function onBackClick(){Dashboard.navigate("livetvsetup/livetvstatus.html")}return function(page,params){var listingsId,providerId=params.id,country=params.country,providerType="embygn";function refreshListings(value){value?(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()})):page.querySelector("#selectListing").innerHTML=""}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",function(e){var selectedListingsId=page.querySelector("#selectListing").value;if(selectedListingsId){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}).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()}else Dashboard.alert({message:globalize.translate("ErrorPleaseSelectLineup")})}),function(){loading.show();var promises=[ApiClient.getNamedConfiguration("livetv"),ApiClient.getJSON(ApiClient.getUrl("LiveTv/TunerHosts")).catch(function(){return ApiClient.getNamedConfiguration("livetv").then(function(config){return config.TunerHosts})})];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"),function(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||[],checkedAttribute=providerInfo.EnableAllTuners||-1!==enabledTuners.indexOf(device.Id)?" 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 listItemBodyText-secondary">',html+=device.Url,html+="</div>",html+="</div>",html+="</div>"}page.querySelector(".tunerList").innerHTML=html,loading.hide()}(page,info,tunerHosts),refreshListings(info.ZipCode||"")})}()}});