define(["exports","./../emby-apiclient/connectionmanager.js","./../common/globalize.js","./../layoutmanager.js","./../dialoghelper/dialoghelper.js","./../emby-elements/emby-scroller/emby-scroller.js","./../emby-elements/emby-select/emby-select.js","./../emby-elements/emby-input/emby-input.js","./../emby-elements/emby-toggle/emby-toggle.js","./../emby-elements/emby-button/paper-icon-button-light.js","./../emby-elements/emby-dialogclosebutton/emby-dialogclosebutton.js","./../common/servicelocator.js","./../focusmanager.js","./../common/appsettings.js","./../registrationservices/registrationservices.js","./../approuter.js"],function(_exports,_connectionmanager,_globalize,_layoutmanager,_dialoghelper,_embyScroller,_embySelect,_embyInput,_embyToggle,_paperIconButtonLight,_embyDialogclosebutton,_servicelocator,_focusmanager,_appsettings,_registrationservices,_approuter){var currentDialogOptions;function showAlert(options){Emby.importModule("./modules/common/dialogs/alert.js").then(function(alert){return alert(options)})}function showSubmissionToast(mode){var options,mode="convert"===mode?_globalize.default.translate("ConvertingDots"):_globalize.default.translate("DownloadingDots");options=mode,Emby.importModule("./modules/toast/toast.js").then(function(toast){return toast(options)})}function syncNow(){require(["localsync"],function(localSync){localSync.sync()})}function setJobValues(job,form){var txtBitrate=form.querySelector(".txtBitrate"),txtBitrate=(txtBitrate=txtBitrate?txtBitrate.value:null)&&1e6*parseFloat(txtBitrate),txtBitrate=(job.Bitrate=txtBitrate,form.querySelector(".selectQuality")),txtBitrate=(txtBitrate&&(job.Quality=txtBitrate.value,_appsettings.default.set("sync-lastquality",job.Quality||"")),form.querySelector(".selectProfile")),txtBitrate=(txtBitrate&&(job.Profile=txtBitrate.value),form.querySelector(".selectJobContainer")),txtBitrate=(txtBitrate&&(job.Container=txtBitrate.value),form.querySelector(".selectVideoCodec")),txtBitrate=(txtBitrate&&(job.VideoCodec=txtBitrate.value),form.querySelector(".selectAudioCodec")),txtBitrate=(txtBitrate&&(job.AudioCodec=txtBitrate.value),form.querySelector(".txtItemLimit")),txtBitrate=(txtBitrate&&(job.ItemLimit=txtBitrate.value||null),form.querySelector(".chkSyncNewContent")),txtBitrate=(txtBitrate&&(job.SyncNewContent=txtBitrate.checked),form.querySelector(".chkUnwatchedOnly"));txtBitrate&&(job.UnwatchedOnly=txtBitrate.checked)}function renderForm(options){return new Promise(function(resolve,reject){!function(options,defaultTargetId,resolve){var elem=options.elem,dialogOptions=options.dialogOptions,targets=dialogOptions.Targets,html="",mode=options.mode,targetContainerClass="download"===mode?" hide":"",syncTargetLabel="convert"===mode?_globalize.default.translate("LabelConvertTo"):_globalize.default.translate("LabelDownloadTo");"download"===mode?targets=targets.filter(function(t){return defaultTargetId===t.Id}):mode&&(defaultTargetId=_appsettings.default.get("lastsync_"+mode+"_targetid"));options.readOnlySyncTarget?html=(html+='
')+'
':(html+='
',targetContainerClass=targets.length?"":" required",html=(html+='",targets.length||(html+='
'+_globalize.default.translate("LabelSyncNoTargetsHelp")+"
"),_servicelocator.appHost.supports("externallinks")&&(html+='
'+_globalize.default.translate("LearnMore")+"
"),html+="
");targetContainerClass=!1;options.readOnlySyncTarget&&-1===dialogOptions.Options.indexOf("UnwatchedOnly")&&-1===dialogOptions.Options.indexOf("SyncNewContent")&&-1===dialogOptions.Options.indexOf("ItemLimit")&&(targetContainerClass=!0);html=(html=(html=(html=(html=(html=(html=(html=(html=(html+='
')+'')+'
')+'
')+'
')+'
')+'
')+'
')+'")+'
')+'
')+'
')+'
')+'
';syncTargetLabel="";dialogOptions.Options.includes("UnwatchedOnly")||(syncTargetLabel+=" hide");html=(html+='
')+'",html+="convert"===mode?'
'+_globalize.default.translate("ConvertUnwatchedVideosOnlyHelp")+"
":'
'+_globalize.default.translate("SyncUnwatchedVideosOnlyHelp")+"
";html+="
",-1!==dialogOptions.Options.indexOf("SyncNewContent")&&(html=(html=(html=(html+='
')+'")+("convert"===mode?'
'+_globalize.default.translate("AutomaticallyConvertNewContentHelp")+"
":'
'+_globalize.default.translate("AutomaticallySyncNewContentHelp")+"
")+"
");-1!==dialogOptions.Options.indexOf("ItemLimit")&&(html=(html=(html+='
')+'')+("convert"===mode?'
'+_globalize.default.translate("ConvertItemLimitHelp")+"
":'
'+_globalize.default.translate("DownloadItemLimitHelp")+"
")+"
");elem.innerHTML=html;targets=elem.querySelector(".selectSyncTarget");targets&&(targets.addEventListener("change",function(){var form,targetId;form=elem,targetId=this.value,(0,options.dialogOptionsFn)(targetId).then(function(options){return function(form,options){currentDialogOptions=options;var fldProfile=form.querySelector(".fldProfile"),selectProfile=form.querySelector(".selectProfile"),unwatchedOnlyToggleContainer=form.querySelector(".unwatchedOnlyToggleContainer");options.Options.includes("UnwatchedOnly")?null!=unwatchedOnlyToggleContainer&&unwatchedOnlyToggleContainer.classList.remove("hide"):null!=unwatchedOnlyToggleContainer&&unwatchedOnlyToggleContainer.classList.add("hide");options.ProfileOptions.length&&-1!==options.Options.indexOf("Profile")?(fldProfile&&fldProfile.classList.remove("hide"),selectProfile&&selectProfile.setAttribute("required","required")):(fldProfile&&fldProfile.classList.add("hide"),selectProfile&&selectProfile.removeAttribute("required"));setQualityFieldVisible(form,0"+o.Name+""}).join(""),selectProfile.dispatchEvent(new CustomEvent("change",{bubbles:!0})));unwatchedOnlyToggleContainer=form.querySelector(".selectQuality");{var lastQuality;unwatchedOnlyToggleContainer&&(unwatchedOnlyToggleContainer.innerHTML=options.QualityOptions.map(function(o){var selectedAttribute=o.IsDefault?" selected":"";return'"}).join(""),(lastQuality=_appsettings.default.get("sync-lastquality"))&&options.QualityOptions.filter(function(i){return i.Id===lastQuality}).length&&(unwatchedOnlyToggleContainer.value=lastQuality),unwatchedOnlyToggleContainer.dispatchEvent(new CustomEvent("change",{bubbles:!0})))}}(form,options)}).then(resolve)}),targets.dispatchEvent(new CustomEvent("change",{bubbles:!0})));targetContainerClass=elem.querySelector(".selectProfile");targetContainerClass&&(targetContainerClass.addEventListener("change",function(){var form=elem,profileId=this.value,options=currentDialogOptions||{},option=(options.ProfileOptions||[]).filter(function(o){return o.Id===profileId})[0],qualityOptions=options.QualityOptions||[];option?(form.querySelector(".profileDescription").innerHTML=option.Description||"",setQualityFieldVisible(form,0')+''+'

',"convert"===options.mode?_globalize.default.translate("Convert"):_globalize.default.translate("Download")),submitted=(dlgElementOptions=dlgElementOptions+syncButtonLabel+"

",_servicelocator.appHost.supports("externallinks")&&(dlgElementOptions+=''),dlg.innerHTML=dlgElementOptions=(dlgElementOptions=(dlgElementOptions=(dlgElementOptions=(dlgElementOptions=(dlgElementOptions+="")+'
'+'
')+'
'+'
')+'
'+('"))+"
"+"
")+"
"+"
",!1),syncButtonLabel=(dlg.querySelector("form").addEventListener("submit",function(e){return submitted=function(dlg,apiClient,userId,syncOptions,form){if(!userId)throw new Error("userId cannot be null");if(!syncOptions)throw new Error("syncOptions cannot be null");if(!form)throw new Error("form cannot be null");var selectSyncTarget=form.querySelector(".selectSyncTarget"),target=null==selectSyncTarget?void 0:selectSyncTarget.value;if(!target)return showAlert(_globalize.default.translate("PleaseSelectDeviceToSyncTo")),!1;var mode=syncOptions.mode,selectedIndex=("download"!==mode&&_appsettings.default.set("lastsync_"+mode+"_targetid",target),selectSyncTarget.selectedIndex);if(-1!==selectedIndex){selectedIndex=null==(selectSyncTarget=selectSyncTarget.options[selectedIndex])?void 0:selectSyncTarget.innerHTML;if(null!=selectedIndex&&selectedIndex.toLowerCase().includes("windows")&&!apiClient.isMinServerVersion("4.8.4"))return showAlert("To use the download feature, please update Emby Server to at least 4.8.4 or greater."),!0}return setJobValues(selectSyncTarget={userId:userId,TargetId:target,ParentId:syncOptions.ParentId,Category:syncOptions.Category},form),syncOptions.items&&syncOptions.items.length&&(selectSyncTarget.ItemIds=(syncOptions.items||[]).map(function(i){return i.Id||i}).join(",")),apiClient.createSyncJob(selectSyncTarget).then(function(){_dialoghelper.default.close(dlg),showSubmissionToast(mode),"download"===mode&&syncNow()}),!0}(dlg,apiClient,userId,options,this),e.preventDefault(),!1}),_dialoghelper.default.open(dlg));return renderForm({elem:dlg.querySelector(".formFields"),dialogOptions:dialogOptions,dialogOptionsFn:dialogOptionsFn,mode:options.mode,apiClient:apiClient}),syncButtonLabel.then(function(){return submitted?Promise.resolve():Promise.reject()})})}(options)})},renderForm:renderForm,setJobValues:setJobValues}});