define(["jQuery","loading","mainTabsManager","globalize","emby-checkbox","emby-linkbutton","emby-collapse","emby-input","emby-button","emby-select"],function($,loading,mainTabsManager,globalize){"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(){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=$("#txtMetadataPath",form).val(),config.MetadataNetworkPath=$("#txtMetadataNetworkPath",form).val(),config.DisplaySpecialsWithinSeasons=form.querySelector(".chkDisplaySpecialsWithinSeasons").checked,config.EnableExternalContentInSuggestions=form.querySelector(".chkExternalContentInSuggestions").checked,config.EnableOriginalTrackTitles="original"===form.querySelector("#selectTrackTitleDisplay").value,ApiClient.updateServerConfiguration(config).then(Dashboard.processServerConfigurationUpdateResult,onSubmitFail)}),function(form){ApiClient.getNamedConfiguration("metadata").then(function(config){config.UseFileCreationTimeForDateAdded="1"===$("#selectDateAdded",form).val(),ApiClient.updateNamedConfiguration("metadata",config)})}(form),!1}function getTabs(){return[{href:"librarysetup/library.html",name:globalize.translate("HeaderLibraries")},{href:"librarysetup/advanced.html",name:globalize.translate("TabAdvanced")}]}return function(view,params){$("#btnSelectMetadataPath",view).on("click.selectDirectory",function(){require(["directorybrowser"],function(directoryBrowser){var picker=new directoryBrowser;picker.show({path:$("#txtMetadataPath",view).val(),networkSharePath:$("#txtMetadataNetworkPath",view).val(),callback:function(path,networkPath){path&&($("#txtMetadataPath",view).val(path),$("#txtMetadataNetworkPath",view).val(networkPath)),picker.close()},validateWriteable:!0,header:globalize.translate("HeaderSelectMetadataPath"),instruction:globalize.translate("HeaderSelectMetadataPathHelp"),enableNetworkSharePath:!0})})}),$(".librarySettingsForm").off("submit",onSubmit).on("submit",onSubmit),view.addEventListener("viewshow",function(){mainTabsManager.setTabs(this,1,getTabs),loading.show();var page=this;Promise.all([ApiClient.getServerConfiguration(),function(select){return 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})}(page.querySelector("#selectLanguage")),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){config.MergeMetadataAndImagesByName?$(".fldImagesByName",page).hide():$(".fldImagesByName",page).show(),page.querySelector("#chkSaveMetadataHidden").checked=config.SaveMetadataHidden,$("#txtMetadataPath",page).val(config.MetadataPath||""),$("#txtMetadataNetworkPath",page).val(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){$("#selectDateAdded",page).val(config.UseFileCreationTimeForDateAdded?"1":"0")}(page,metadata)}),ApiClient.getSystemInfo().then(function(info){"Windows"===info.OperatingSystem?page.querySelector(".fldSaveMetadataHidden").classList.remove("hide"):page.querySelector(".fldSaveMetadataHidden").classList.add("hide")})})}});