define(["exports","./../emby-apiclient/events.js","./../emby-apiclient/connectionmanager.js","./appsettings.js"],function(_exports,_events,_connectionmanager,_appsettings){Object.defineProperty(_exports,"__esModule",{value:!0}),_exports.default=void 0;var currentCulture,currentDateTimeCulture,currentLocales,cacheParam,allTranslations={},allStrings={},rtlLocales=["ar","he","fa","ur","ug"],arabicLocales=["ar","fa","ur","ug"];function getCurrentLocale(){return currentCulture}function normalizeNavigatorLanguage(lang){return lang=lang&&lang.split("@")[0]}function updateCurrentCulture(){var dir,dateTimeCulture,culture=_appsettings.default.language()||function(){var culture="undefined"==typeof document?null:document.documentElement.getAttribute("data-culture");return culture||(navigator.language?normalizeNavigatorLanguage(navigator.language):navigator.userLanguage?normalizeNavigatorLanguage(navigator.userLanguage):navigator.languages&&navigator.languages.length?normalizeNavigatorLanguage(navigator.languages[0]):"en-us")}();"undefined"!=typeof document&&CSS.supports("text-align","start")&&CSS.supports("inset-inline-start","0")&&(dir=rtlLocales.includes((culture||"").toLowerCase())?"rtl":"ltr","rtl"==(document.dir=dir)?document.documentElement.classList.add("rtl"):document.documentElement.classList.remove("rtl"),arabicLocales.includes((culture||"").toLowerCase())?document.documentElement.classList.add("rtl-arabic"):document.documentElement.classList.remove("rtl-arabic")),currentCulture=normalizeLocaleName(culture),currentLocales=[currentCulture];try{dateTimeCulture=_appsettings.default.dateTimeLocale()}catch(err){}currentDateTimeCulture=dateTimeCulture?normalizeLocaleName(dateTimeCulture):currentCulture,function(culture){for(var i in allTranslations)ensureTranslation(allTranslations[i],culture)}(currentCulture),_connectionmanager.default.setCurrentLocale(currentCulture)}function ensureTranslation(translationInfo,culture){return translationInfo.dictionaries[culture]?Promise.resolve():function(translations,lang){lang=normalizeLocaleName(lang);var filtered=translations.filter(function(t){return normalizeLocaleName(t.lang)===lang});filtered.length||(filtered=translations.filter(function(t){return"en-us"===normalizeLocaleName(t.lang)}));if(!filtered.length)return Promise.resolve();translations=filtered[0].path;cacheParam&&(translations=(translations+=-1===translations.indexOf("?")?"?":"&")+cacheParam);if(window.VegaReadFileAsString)return window.VegaReadFileAsString(translations).then(function(data){return JSON.parse(data)});return("undefined"==typeof XMLHttpRequest?function(url){return fetch(url).then(function(response){return response.json()})}:function(url){return new Promise(function(resolve,reject){var xhr=new XMLHttpRequest;xhr.open("GET",url,!0),xhr.onload=function(e){this.status<400?resolve(JSON.parse(this.response)):resolve()},xhr.onerror=function(){resolve()},xhr.send()})})(translations)}(translationInfo.translations,culture).then(function(dictionary){translationInfo.dictionaries[culture]=!0,dictionary&&(allStrings[culture]?allStrings[culture]=Object.assign(dictionary,allStrings[culture]||{}):allStrings[culture]=dictionary)})}function normalizeLocaleName(culture){var parts=(culture=culture.replace("_","-")).split("-"),parts=(culture=2===parts.length&&parts[0].toLowerCase()===parts[1].toLowerCase()?parts[0].toLowerCase():culture).toLowerCase();return"ca-es"===parts?"ca":"sv-se"===parts?"sv":parts}function register(options){allTranslations[options.name]={translations:options.strings||options.translations,dictionaries:{}}}function translateKey(key){var dictionary=allStrings[currentCulture];if(dictionary){dictionary=dictionary[key];if(dictionary)return dictionary}return key}function translate(key){for(var val=translateKey(key),i=1;i<arguments.length;i++)val=val.replaceAll("{"+(i-1)+"}",arguments[i]);return val}function translateHtml(html){var endIndex,startIndex=html.indexOf("${");return-1===startIndex||-1===(endIndex=html.indexOf("}",startIndex+=2))?html:(endIndex=translateKey(startIndex=html.substring(startIndex,endIndex)),translateHtml(html=html.replace("${"+startIndex+"}",endIndex)))}updateCurrentCulture(),_events.default.on(_connectionmanager.default,"localusersignedin",updateCurrentCulture),_events.default.on(_appsettings.default,"change",function(e,name){switch(name){case"language":case"datetimelocale":updateCurrentCulture()}});_exports.default={getString:translate,translate:translate,translateDocument:translateHtml,translateHtml:translateHtml,loadStrings:function(options){var locale=currentCulture;return"string"==typeof options?ensureTranslation(allTranslations[options],locale):(register(options),ensureTranslation(allTranslations[options.name],locale))},getCurrentLocale:getCurrentLocale,getCurrentDateTimeLocale:function(){return currentDateTimeCulture},getCurrentLocales:function(){return currentLocales},register:register,setCacheParam:function(value){cacheParam=value}}});