define(["loading","mainTabsManager","globalize","formHelper","emby-checkbox","emby-linkbutton","emby-input","emby-button","emby-select"],function(loading,mainTabsManager,globalize,formHelper){"use strict";function alertText(options){require(["alert"],function(alert){alert(options)})}function onSubmitFail(response){loading.hide(),response&&404===response.status?alertText("The metadata path entered could not be found. Please ensure the path is valid and try again."):response&&500===response.status&&alertText("The metadata path entered is not valid. Please ensure the path exists and that Emby server has write access to the folder.")}function onSubmit(e){loading.show();var form=this;return ApiClient.getServerConfiguration().then(function(config){config.PreferredMetadataLanguage=form.querySelector("#selectLanguage").value,config.MetadataCountryCode=form.querySelector("#selectCountry").value,config.SaveMetadataHidden=form.querySelector("#chkSaveMetadataHidden").checked,config.MetadataPath=form.querySelector("#txtMetadataPath").value,config.MetadataNetworkPath=form.querySelector("#txtMetadataNetworkPath").value,config.DisplaySpecialsWithinSeasons=form.querySelector(".chkDisplaySpecialsWithinSeasons").checked,config.EnableExternalContentInSuggestions=form.querySelector(".chkExternalContentInSuggestions").checked,config.EnableOriginalTrackTitles="original"===form.querySelector("#selectTrackTitleDisplay").value,ApiClient.updateServerConfiguration(config).then(formHelper.handleConfigurationSavedResponse,onSubmitFail)}),function(form){ApiClient.getNamedConfiguration("metadata").then(function(config){config.UseFileCreationTimeForDateAdded="1"===form.querySelector("#selectDateAdded").value,ApiClient.updateNamedConfiguration("metadata",config)})}(form),e.preventDefault(),e.stopPropagation(),!1}function getTabs(){return[{href:"librarysetup/library.html",name:globalize.translate("HeaderLibraries")},{href:"librarysetup/advanced.html",name:globalize.translate("TabAdvanced")}]}return function(view,params){view.querySelector("#btnSelectMetadataPath").addEventListener("click",function(){require(["directorybrowser"],function(directoryBrowser){var picker=new directoryBrowser;picker.show({path:view.querySelector("#txtMetadataPath").value,networkSharePath:view.querySelector("#txtMetadataNetworkPath").value,callback:function(path,networkPath){path&&(view.querySelector("#txtMetadataPath").value=path,view.querySelector("#txtMetadataNetworkPath").value=networkPath),picker.close()},validateWriteable:!0,header:globalize.translate("HeaderSelectMetadataPath"),instruction:globalize.translate("HeaderSelectMetadataPathHelp"),enableNetworkSharePath:!0})})}),view.querySelector(".librarySettingsForm").addEventListener("submit",onSubmit),view.addEventListener("viewshow",function(){mainTabsManager.setTabs(this,1,getTabs),loading.show();var select,page=this;Promise.all([ApiClient.getServerConfiguration(),(select=page.querySelector("#selectLanguage"),ApiClient.getCultures().then(function(languages){var html="";html+="<option value=''></option>";for(var i=0,length=languages.length;i<length;i++){var culture=languages[i];html+="<option value='"+culture.TwoLetterISOLanguageName+"'>"+culture.DisplayName+"</option>"}select.innerHTML=html})),function(select){return ApiClient.getCountries().then(function(allCountries){var html="";html+="<option value=''></option>";for(var i=0,length=allCountries.length;i<length;i++){var culture=allCountries[i];html+="<option value='"+culture.TwoLetterISORegionName+"'>"+culture.DisplayName+"</option>"}select.innerHTML=html})}(page.querySelector("#selectCountry"))]).then(function(responses){!function(page,config){page.querySelector("#chkSaveMetadataHidden").checked=config.SaveMetadataHidden,page.querySelector("#txtMetadataPath").value=config.MetadataPath||"",page.querySelector("#txtMetadataNetworkPath").value=config.MetadataNetworkPath||"",page.querySelector(".chkDisplaySpecialsWithinSeasons").checked=config.DisplaySpecialsWithinSeasons,page.querySelector(".chkExternalContentInSuggestions").checked=config.EnableExternalContentInSuggestions,page.querySelector("#selectTrackTitleDisplay").value=config.EnableOriginalTrackTitles?"original":"",page.querySelector("#selectLanguage").value=config.PreferredMetadataLanguage||"",page.querySelector("#selectCountry").value=config.MetadataCountryCode||"",loading.hide()}(page,responses[0])}),ApiClient.getNamedConfiguration("metadata").then(function(metadata){!function(page,config){page.querySelector("#selectDateAdded").value=config.UseFileCreationTimeForDateAdded?"1":"0"}(page,metadata)}),ApiClient.getSystemInfo().then(function(info){"Windows"!==info.OperatingSystem||ApiClient.isMinServerVersion("4.6.0.11")?page.querySelector(".fldSaveMetadataHidden").classList.add("hide"):page.querySelector(".fldSaveMetadataHidden").classList.remove("hide")})})}});