import*as Platform from"../platform/platform.js";import i18nBundle from"../third_party/i18n/i18n-bundle.js";export const registerLocaleData=i18nBundle.registerLocaleData;export let registeredLocale;let moduleJSONStrings;function getOrSetModuleJSONStrings(){if(!registeredLocale)throw new Error(`Unsupported locale '${registeredLocale}'`);return moduleJSONStrings=moduleJSONStrings||i18nBundle.getRendererFormattedStrings(registeredLocale),moduleJSONStrings}export function registerLocale(e){registeredLocale=i18nBundle.lookupLocale(e)}export function getLazilyComputedLocalizedString(e,t,r={}){return()=>getLocalizedString(e,t,r)}export function getLocalizedString(e,t,r={}){if(!registeredLocale)throw new Error(`Unsupported locale '${registeredLocale}'`);const n=e(t,r);return i18nBundle.getFormatted(n,registeredLocale)}export function registerUIStrings(e,t){return(r,n)=>{try{return i18nBundle.createMessageInstanceIdFn(e,t)(r,n)}catch(o){if(o instanceof i18nBundle.idNotInMainDictionaryException){const e=Object.getOwnPropertyNames(getOrSetModuleJSONStrings()),t=e.indexOf(r);if(t>=0)return e[t]}return r}}}export function getFormatLocalizedString(e,t,r){if(!registeredLocale)throw new Error(`Unsupported locale '${registeredLocale}'`);const n=e(t,r),o=i18nBundle.getFormatter(n,registeredLocale).getAst().elements,i=[];let a="";for(const l of o){if("argumentElement"===l.type){const e=r[l.id];e&&(i.push(e),l.value="%s")}a+=l.value}return formatLocalized(a,i)}export function formatLocalized(e,t){const r={s:e=>e};return Platform.StringUtilities.format(e,t,r,document.createElement("span"),(function(e,t){return t&&e.appendChild("string"===typeof t?document.createTextNode(t):t),e})).formattedResult}export function serializeUIString(e,t={}){const r={string:e,values:t};return JSON.stringify(r)}export function deserializeUIString(e){return e?JSON.parse(e):{string:"",values:{}}}