define(["dialogHelper","loading","connectionManager","globalize","actionsheet","emby-input","paper-icon-button-light","emby-button","listViewStyle","material-icons","formDialogStyle","emby-scroller"],function(dialogHelper,loading,connectionManager,globalize,actionsheet){"use strict";return function(options){var currentMappingOptions;function onChannelsElementClick(e){var channelId,providerChannelId,isManualMapping,menuItems,btnMap=e.target.closest(".btnMap");btnMap&&(channelId=btnMap.getAttribute("data-id"),providerChannelId=btnMap.getAttribute("data-providerid"),isManualMapping="true"===btnMap.getAttribute("data-manual"),menuItems=currentMappingOptions.ProviderChannels.map(function(m){return{name:m.Name,id:m.Id,selected:m.Id.toLowerCase()===providerChannelId.toLowerCase()}}),providerChannelId&&isManualMapping&&menuItems.unshift({name:"Clear Manual Mapping",id:""}),actionsheet.show({positionTo:btnMap,items:menuItems}).then(function(newChannelId){!function(button,channelId,providerChannelId){loading.show();var providerId=options.providerId;connectionManager.getApiClient(options.serverId).ajax({type:"POST",url:ApiClient.getUrl("LiveTv/ChannelMappings"),data:{providerId:providerId,tunerChannelId:channelId,providerChannelId:providerChannelId},dataType:"json"}).then(function(mapping){var listItem=button.closest(".listItem");button.setAttribute("data-providerid",mapping.ProviderChannelId),mapping.IsManualMapping?button.setAttribute("data-manual","true"):button.removeAttribute("data-manual"),listItem.querySelector(".listItemBodyText-secondary").innerHTML=getMappingSecondaryName(mapping,currentMappingOptions.ProviderName),loading.hide()})}(btnMap,channelId,newChannelId)}))}function getMappingSecondaryName(mapping,providerName){return(mapping.ProviderChannelName||"")+" - "+providerName}function initEditor(dlg,options){var serverId,providerId,apiClient;serverId=options.serverId,providerId=options.providerId,(apiClient=connectionManager.getApiClient(serverId)).getJSON(apiClient.getUrl("LiveTv/ChannelMappingOptions",{providerId:providerId})).then(function(result){currentMappingOptions=result;var channelsElement=dlg.querySelector(".channels");channelsElement.innerHTML=result.TunerChannels.map(function(channel){return function(channel,providerName){var html="";return html+='<div class="listItem">',html+='<i class="md-icon listItemIcon">dvr</i>',html+='<div class="listItemBody two-line">',html+='<h3 class="listItemBodyText">',html+=channel.Name,html+="</h3>",html+='<div class="listItemBodyText-secondary listItemBodyText">',channel.ProviderChannelName&&(html+=getMappingSecondaryName(channel,providerName)),html+="</div>",html+="</div>",html+='<button class="btnMap autoSize" is="paper-icon-button-light" type="button" data-manual="'+(channel.IsManualMapping||!1).toString()+'" data-id="'+channel.Id+'" data-providerid="'+channel.ProviderChannelId+'"><i class="md-icon">edit</i></button>',html+="</div>"}(channel,result.ProviderName)}).join(""),channelsElement.addEventListener("click",onChannelsElementClick)})}this.show=function(){var dialogOptions={removeOnClose:!0,size:"small"},dlg=dialogHelper.createDialog(dialogOptions);dlg.classList.add("formDialog"),dlg.classList.add("ui-body-a"),dlg.classList.add("background-theme-a");var html="";return html+='<div class="formDialogHeader">',html+='<button is="paper-icon-button-light" class="btnCancel autoSize" tabindex="-1"><i class="md-icon">&#xE5C4;</i></button>',html+='<h3 class="formDialogHeaderTitle">',html+=globalize.translate("MapChannels"),html+="</h3>",html+="</div>",html+=function(){var html="";return html+='<div is="emby-scroller" data-horizontal="false" data-centerfocus="card" class="formDialogContent">',html+='<div class="scrollSlider">',html+='<form class="dialogContentInner dialog-content-centered padded-left padded-right">',html+="<h1>"+globalize.translate("HeaderChannels")+"</h1>",html+='<div class="channels paperList">',html+="</div>",html+="</form>",html+="</div>",html+="</div>"}(),dlg.innerHTML=html,initEditor(dlg,options),dlg.querySelector(".btnCancel").addEventListener("click",function(){dialogHelper.close(dlg)}),dialogHelper.open(dlg)}}});