Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@tolgee/core

Package Overview
Dependencies
Maintainers
0
Versions
358
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@tolgee/core - npm Package Compare versions

Comparing version 5.33.2 to 5.33.3-prerelease.2efc0e6b.0

lib/Controller/Events/EventEmitterCombined.d.ts

411

dist/tolgee.cjs.js

@@ -93,5 +93,5 @@ 'use strict';

function EventEmitter(isActive) {
const EventEmitter = (type, isActive) => {
let handlers = [];
return Object.freeze({
return {
listen(handler) {

@@ -110,23 +110,10 @@ const handlerWrapper = (e) => {

if (isActive()) {
handlers.forEach((handler) => handler({ value: data }));
handlers.forEach((handler) => handler({ type: type, value: data }));
}
},
});
}
};
};
function EventEmitterSelective(isActive, getFallbackNs, getDefaultNs) {
const listeners = new Set();
const partialListeners = new Set();
function callHandlers(ns) {
// everything is implicitly subscribed to fallbacks
// as it can always fall through to it
const fallbackNamespaces = new Set(getFallbackNs());
partialListeners.forEach((handler) => {
const nsMatches = ns === undefined ||
(ns === null || ns === void 0 ? void 0 : ns.findIndex((ns) => fallbackNamespaces.has(ns) || handler.namespaces.has(ns))) !== -1;
if (nsMatches) {
handler.fn({ value: undefined });
}
});
}
function EventEmitterCombined(isActive) {
let handlers = [];
let queue = [];

@@ -140,63 +127,30 @@ // merge events in queue into one event

queue = [];
listeners.forEach((handler) => {
handler({ value: undefined });
handlers.forEach((handler) => {
handler(queueCopy);
});
let namespaces = new Set();
queueCopy.forEach((ns) => {
if (ns === undefined) {
// when no ns specified, it affects all namespaces
namespaces = undefined;
}
else if (namespaces !== undefined) {
ns.forEach((ns) => namespaces.add(ns));
}
});
const namespacesArray = namespaces
? Array.from(namespaces.keys())
: undefined;
callHandlers(namespacesArray);
}
return Object.freeze({
emit(ns, delayed) {
if (isActive()) {
queue.push(ns);
if (!delayed) {
solveQueue();
}
else {
setTimeout(solveQueue, 0);
}
}
},
listen(handler) {
listeners.add(handler);
const result = {
unsubscribe: () => {
listeners.delete(handler);
const handlerWrapper = (events) => {
handler(events);
};
handlers.push(handlerWrapper);
return {
unsubscribe() {
handlers = handlers.filter((i) => handlerWrapper !== i);
},
};
return result;
},
listenSome(handler) {
const handlerWrapper = {
fn: (e) => {
handler(e);
},
namespaces: new Set(),
};
partialListeners.add(handlerWrapper);
const result = {
unsubscribe: () => {
partialListeners.delete(handlerWrapper);
},
subscribeNs: (ns) => {
getFallbackArray(ns).forEach((val) => handlerWrapper.namespaces.add(val));
if (ns === undefined) {
// subscribing to default ns
handlerWrapper.namespaces.add(getDefaultNs());
emit(e, delayed) {
if (isActive()) {
if (isActive()) {
queue.push(e);
if (!delayed) {
solveQueue();
}
return result;
},
};
return result;
else {
setTimeout(solveQueue, 0);
}
}
}
},

@@ -206,3 +160,3 @@ });

function Events(getFallbackNs, getDefaultNs) {
function Events() {
let emitterActive = true;

@@ -213,12 +167,12 @@ function isActive() {

const self = Object.freeze({
onPendingLanguageChange: EventEmitter(isActive),
onLanguageChange: EventEmitter(isActive),
onLoadingChange: EventEmitter(isActive),
onFetchingChange: EventEmitter(isActive),
onInitialLoaded: EventEmitter(isActive),
onRunningChange: EventEmitter(isActive),
onCacheChange: EventEmitter(isActive),
onUpdate: EventEmitterSelective(isActive, getFallbackNs, getDefaultNs),
onPermanentChange: EventEmitter(isActive),
onError: EventEmitter(isActive),
onPendingLanguageChange: EventEmitter('pendingLanguage', isActive),
onLanguageChange: EventEmitter('language', isActive),
onLoadingChange: EventEmitter('loading', isActive),
onFetchingChange: EventEmitter('fetching', isActive),
onInitialLoaded: EventEmitter('initialLoad', isActive),
onRunningChange: EventEmitter('running', isActive),
onCacheChange: EventEmitter('cache', isActive),
onPermanentChange: EventEmitter('permanentChange', isActive),
onError: EventEmitter('error', isActive),
onUpdate: EventEmitterCombined(isActive),
setEmitterActive(active) {

@@ -252,5 +206,5 @@ emitterActive = active;

});
self.onInitialLoaded.listen(() => self.onUpdate.emit());
self.onLanguageChange.listen(() => self.onUpdate.emit());
self.onCacheChange.listen(({ value }) => self.onUpdate.emit([value.namespace], true));
self.onInitialLoaded.listen((e) => self.onUpdate.emit(e, false));
self.onLanguageChange.listen((e) => self.onUpdate.emit(e, false));
self.onCacheChange.listen((e) => self.onUpdate.emit(e, true));
return self;

@@ -285,3 +239,3 @@ }

const flattenTranslations = (data) => {
const flattenTranslationsToMap = (data) => {
const result = new Map();

@@ -294,3 +248,3 @@ Object.entries(data).forEach(([key, value]) => {

if (typeof value === 'object') {
flattenTranslations(value).forEach((flatValue, flatKey) => {
flattenTranslationsToMap(value).forEach((flatValue, flatKey) => {
result.set(key + '.' + flatKey, flatValue);

@@ -304,2 +258,5 @@ });

};
const flattenTranslations = (data) => {
return Object.fromEntries(flattenTranslationsToMap(data).entries());
};
const decodeCacheKey = (key) => {

@@ -320,3 +277,3 @@ const [firstPart, ...rest] = key.split(':');

function Cache(events, backendGetRecord, backendGetDevRecord, withDefaultNs, isInitialLoading, fetchingObserver, loadingObserver) {
function Cache(events, backendGetRecord, backendGetDevRecord, withDefaultNs, isInitialLoading, isCacheDisabled, fetchingObserver, loadingObserver) {
const asyncRequests = new Map();

@@ -332,3 +289,3 @@ const cache = new Map();

});
events.onCacheChange.emit(descriptor);
events.onCacheChange.emit(decodeCacheKey(cacheKey));
}

@@ -386,3 +343,12 @@ /**

addStaticData(data) {
if (data) {
if (Array.isArray(data)) {
for (const record of data) {
const key = encodeCacheKey(record);
const existing = cache.get(key);
if (!existing || existing.version === 0) {
addRecordInternal(record, flattenTranslations(record.data), 0);
}
}
}
else if (data) {
staticData = Object.assign(Object.assign({}, staticData), data);

@@ -394,3 +360,3 @@ Object.entries(data).forEach(([key, value]) => {

if (!existing || existing.version === 0) {
addRecordInternal(descriptor, value, 0);
addRecordInternal(descriptor, flattenTranslations(value), 0);
}

@@ -406,3 +372,3 @@ }

addRecord(descriptor, data) {
addRecordInternal(descriptor, data, version);
addRecordInternal(descriptor, flattenTranslations(data), version);
},

@@ -417,8 +383,17 @@ exists(descriptor, strict = false) {

getRecord(descriptor) {
var _a;
return (_a = cache.get(encodeCacheKey(withDefaultNs(descriptor)))) === null || _a === void 0 ? void 0 : _a.data;
const descriptorWithNs = withDefaultNs(descriptor);
const cacheKey = encodeCacheKey(descriptorWithNs);
const cacheRecord = cache.get(cacheKey);
if (!cacheRecord) {
return undefined;
}
return Object.assign(Object.assign({}, descriptorWithNs), { cacheKey, data: cacheRecord.data });
},
getAllRecords() {
const entries = Array.from(cache.entries());
return entries.map(([key]) => self.getRecord(decodeCacheKey(key)));
},
getTranslation(descriptor, key) {
var _a;
return (_a = cache.get(encodeCacheKey(descriptor))) === null || _a === void 0 ? void 0 : _a.data.get(key);
return (_a = cache.get(encodeCacheKey(descriptor))) === null || _a === void 0 ? void 0 : _a.data[key];
},

@@ -429,4 +404,3 @@ getTranslationNs(namespaces, languages, key) {

for (const language of languages) {
const value = (_a = cache
.get(encodeCacheKey({ language, namespace }))) === null || _a === void 0 ? void 0 : _a.data.get(key);
const value = (_a = cache.get(encodeCacheKey({ language, namespace }))) === null || _a === void 0 ? void 0 : _a.data[key];
if (value !== undefined && value !== null) {

@@ -443,4 +417,3 @@ return [namespace];

for (const language of languages) {
const value = (_a = cache
.get(encodeCacheKey({ language, namespace }))) === null || _a === void 0 ? void 0 : _a.data.get(key);
const value = (_a = cache.get(encodeCacheKey({ language, namespace }))) === null || _a === void 0 ? void 0 : _a.data[key];
if (value !== undefined && value !== null) {

@@ -456,4 +429,6 @@ return value;

const record = (_a = cache.get(encodeCacheKey(descriptor))) === null || _a === void 0 ? void 0 : _a.data;
record === null || record === void 0 ? void 0 : record.set(key, value);
events.onCacheChange.emit(Object.assign(Object.assign({}, descriptor), { key }));
if (record === null || record === void 0 ? void 0 : record[key]) {
record[key] = value;
events.onCacheChange.emit(Object.assign(Object.assign({}, descriptor), { key }));
}
},

@@ -472,51 +447,57 @@ isFetching(ns) {

const namespaces = getFallbackArray(ns);
return Boolean(isInitialLoading() ||
Array.from(asyncRequests.keys()).find((key) => {
const descriptor = decodeCacheKey(key);
return ((!namespaces.length ||
namespaces.includes(descriptor.namespace)) &&
!self.exists({
namespace: descriptor.namespace,
language: language,
}));
}));
if (isInitialLoading()) {
return true;
}
const pendingCacheKeys = Array.from(asyncRequests.keys());
return Boolean(pendingCacheKeys.find((key) => {
const descriptor = decodeCacheKey(key);
return ((!namespaces.length || namespaces.includes(descriptor.namespace)) &&
!self.exists({
namespace: descriptor.namespace,
language: language,
}));
}));
},
async loadRecords(descriptors, isDev) {
async loadRecords(descriptors, options) {
const withPromises = descriptors.map((descriptor) => {
const keyObject = withDefaultNs(descriptor);
const cacheKey = encodeCacheKey(keyObject);
if (options === null || options === void 0 ? void 0 : options.useCache) {
const exists = self.exists(keyObject, true);
if (exists) {
return Object.assign(Object.assign({}, keyObject), { new: false, cacheKey, data: self.getRecord(keyObject).data });
}
}
const existingPromise = asyncRequests.get(cacheKey);
if (existingPromise) {
return {
new: false,
promise: existingPromise,
keyObject,
cacheKey,
};
return Object.assign(Object.assign({}, keyObject), { new: false, promise: existingPromise, cacheKey });
}
const dataPromise = fetchData(keyObject, isDev) || Promise.resolve(undefined);
const dataPromise = fetchData(keyObject, !(options === null || options === void 0 ? void 0 : options.noDev)) || Promise.resolve(undefined);
asyncRequests.set(cacheKey, dataPromise);
return {
new: true,
promise: dataPromise,
keyObject,
cacheKey,
};
return Object.assign(Object.assign({}, keyObject), { new: true, promise: dataPromise, cacheKey });
});
fetchingObserver.notify();
loadingObserver.notify();
const results = await Promise.all(withPromises.map((val) => val.promise));
withPromises.forEach((value, i) => {
const promiseChanged = asyncRequests.get(value.cacheKey) !== value.promise;
const promisesToWait = withPromises
.map((val) => val.promise)
.filter(Boolean);
const fetchedData = await Promise.all(promisesToWait);
withPromises.forEach((value) => {
var _a;
if (value.promise) {
value.data = flattenTranslations((_a = fetchedData[0]) !== null && _a !== void 0 ? _a : {});
fetchedData.shift();
}
// if promise has changed in between, it means cache been invalidated or
// new data are being fetched
const promiseChanged = asyncRequests.get(value.cacheKey) !== value.promise;
if (value.new && !promiseChanged) {
asyncRequests.delete(value.cacheKey);
const data = results[i];
if (data) {
self.addRecord(value.keyObject, data);
if (value.data) {
self.addRecord(value, value.data);
}
else if (!self.getRecord(value.keyObject)) {
else if (!self.getRecord(value)) {
// if no data exist, put empty object
self.addRecord(value.keyObject, {});
// so we know we don't have to fetch again
self.addRecord(value, {});
}

@@ -527,8 +508,10 @@ }

loadingObserver.notify();
return withPromises.map((val) => self.getRecord(val.keyObject));
},
getAllRecords() {
const entries = Array.from(cache.entries());
return entries.map(([key, entry]) => {
return Object.assign(Object.assign({}, decodeCacheKey(key)), { data: entry.data });
return withPromises.map((val) => {
var _a;
return ({
language: val.language,
namespace: val.namespace,
data: (_a = val.data) !== null && _a !== void 0 ? _a : {},
cacheKey: val.cacheKey,
});
});

@@ -588,3 +571,2 @@ },

const defaultValues = {
defaultNs: '',
observerOptions: defaultObserverOptions,

@@ -594,4 +576,6 @@ observerType: 'invisible',

apiUrl: DEFAULT_API_URL,
autoLoadRequiredData: true,
fetch: createFetchFunction(),
onTranslationMissing: DEFAULT_MISSING_TRANSLATION,
disableCache: false,
};

@@ -790,2 +774,5 @@ const combineOptions = (...states) => {

const { apiKey, apiUrl, projectId, filterTag } = getInitialOptions();
if (!apiKey || !apiUrl || !self.hasDevBackend()) {
return undefined;
}
return (_a = instances.devBackend) === null || _a === void 0 ? void 0 : _a.getRecord(Object.assign({ apiKey,

@@ -934,2 +921,5 @@ apiUrl,

},
isCacheDisabled() {
return state.initialOptions.disableCache;
},
setInitialLoading(value) {

@@ -985,3 +975,4 @@ state.isInitialLoading = value;

return unique([
...(state.initialOptions.ns || [state.initialOptions.defaultNs]),
self.getDefaultNs(),
...(state.initialOptions.ns || []),
...getFallbackArray(state.initialOptions.fallbackNs),

@@ -1004,4 +995,13 @@ ...state.activeNamespaces.keys(),

},
getNs() {
var _a, _b;
return ((_a = state.initialOptions.ns) === null || _a === void 0 ? void 0 : _a.length)
? state.initialOptions.ns
: [(_b = state.initialOptions.defaultNs) !== null && _b !== void 0 ? _b : ''];
},
getDefaultNs(ns) {
return ns === undefined ? state.initialOptions.defaultNs : ns;
var _a, _b, _c;
return ns === undefined
? (_c = (_a = state.initialOptions.defaultNs) !== null && _a !== void 0 ? _a : (_b = state.initialOptions.ns) === null || _b === void 0 ? void 0 : _b[0]) !== null && _c !== void 0 ? _c : ''
: ns;
},

@@ -1017,6 +1017,9 @@ getAvailableLanguages() {

},
getAvailableNs() {
return state.initialOptions.availableNs;
},
withDefaultNs(descriptor) {
return {
namespace: descriptor.namespace === undefined
? self.getInitialOptions().defaultNs
? self.getDefaultNs()
: descriptor.namespace,

@@ -1071,3 +1074,3 @@ language: descriptor.language,

function Controller({ options }) {
const events = Events(getFallbackNs, getDefaultNs);
const events = Events();
const fetchingObserver = ValueObserver(false, () => cache.isFetching(), events.onFetchingChange.emit);

@@ -1077,3 +1080,3 @@ const loadingObserver = ValueObserver(false, () => self.isLoading(), events.onLoadingChange.emit);

const pluginService = Plugins(state.getLanguage, state.getInitialOptions, state.getAvailableLanguages, getDefaultAndFallbackNs, getTranslationNs, getTranslation, changeTranslation, events);
const cache = Cache(events, pluginService.getBackendRecord, pluginService.getBackendDevRecord, state.withDefaultNs, state.isInitialLoading, fetchingObserver, loadingObserver);
const cache = Cache(events, pluginService.getBackendRecord, pluginService.getBackendDevRecord, state.withDefaultNs, state.isInitialLoading, state.isCacheDisabled, fetchingObserver, loadingObserver);
if (options) {

@@ -1097,3 +1100,3 @@ init(options);

function getDefaultAndFallbackNs(ns) {
return [...getFallbackArray(getDefaultNs(ns)), ...getFallbackNs()];
return unique([...getFallbackArray(getDefaultNs(ns)), ...getFallbackNs()]);
}

@@ -1103,6 +1106,6 @@ // gets all namespaces which need to be loaded

function getRequiredNamespaces(ns) {
return [
return unique([
...getFallbackArray(ns !== null && ns !== void 0 ? ns : getDefaultNs()),
...state.getRequiredNamespaces(),
];
]);
}

@@ -1123,3 +1126,3 @@ function changeTranslation(descriptor, key, value) {

}
function getRequiredRecords(lang, ns) {
function getRequiredDescriptors(lang, ns) {
const languages = state.getFallbackLangs(lang);

@@ -1130,5 +1133,3 @@ const namespaces = getRequiredNamespaces(ns);

namespaces.forEach((namespace) => {
if (!cache.exists({ language, namespace }, true)) {
result.push({ language, namespace });
}
result.push({ language, namespace });
});

@@ -1138,7 +1139,35 @@ });

}
function loadRequiredRecords(lang, ns) {
const descriptors = getRequiredRecords(lang, ns);
if (descriptors.length) {
return valueOrPromise(self.loadRecords(descriptors), () => { });
function getMissingDescriptors(lang, ns) {
return getRequiredDescriptors(lang, ns).filter((descriptor) => !cache.exists(descriptor, true));
}
function getMatrixRecords(options) {
let languages = [];
let namespaces = [];
if (Array.isArray(options.languages)) {
languages = options.languages;
}
else if (options.languages === 'all') {
const availableLanguages = self.getAvailableLanguages();
if (!availableLanguages) {
throw new Error(missingOptionError('availableLanguages'));
}
languages = availableLanguages;
}
if (Array.isArray(options.namespaces)) {
namespaces = options.namespaces;
}
else if (options.namespaces === 'all') {
const availableNs = self.getAvailableNs();
if (!availableNs) {
throw new Error(missingOptionError('availableNs'));
}
namespaces = availableNs;
}
const records = [];
languages.forEach((language) => {
namespaces.forEach((namespace) => {
records.push({ language, namespace });
});
});
return records;
}

@@ -1157,4 +1186,7 @@ function getTranslationNs({ key, ns }) {

const data = valueOrPromise(initializeLanguage(), () => {
// fail if there is no language
return loadRequiredRecords();
const missingDescriptors = getMissingDescriptors();
if (missingDescriptors.length &&
state.getInitialOptions().autoLoadRequiredData) {
return cache.loadRecords(missingDescriptors, { useCache: true });
}
});

@@ -1200,3 +1232,3 @@ if (isPromise(data)) {

}
const self = Object.freeze(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign({}, events), state), pluginService), cache), { init: init, getTranslation: getTranslation, changeTranslation: changeTranslation, getTranslationNs: getTranslationNs, getDefaultAndFallbackNs: getDefaultAndFallbackNs, findPositions: pluginService.findPositions, getRequiredRecords: getRequiredRecords, async changeLanguage(language) {
const self = Object.freeze(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign({}, events), state), pluginService), cache), { init: init, getTranslation: getTranslation, changeTranslation: changeTranslation, getTranslationNs: getTranslationNs, getDefaultAndFallbackNs: getDefaultAndFallbackNs, findPositions: pluginService.findPositions, getRequiredDescriptors: getRequiredDescriptors, async changeLanguage(language) {
if (state.getPendingLanguage() === language &&

@@ -1207,4 +1239,6 @@ state.getLanguage() === language) {

state.setPendingLanguage(language);
if (state.isRunning()) {
await loadRequiredRecords(language);
if (state.isRunning() && state.getInitialOptions().autoLoadRequiredData) {
await cache.loadRecords(getRequiredDescriptors(language), {
useCache: true,
});
}

@@ -1223,11 +1257,11 @@ if (language === state.getPendingLanguage()) {

if (state.isRunning()) {
await loadRequiredRecords(undefined, ns);
await cache.loadRecords(getRequiredDescriptors(undefined, ns), {
useCache: true,
});
}
},
loadRecords(descriptors) {
return cache.loadRecords(descriptors, self.isDev());
async loadRecord(descriptor, options) {
var _a;
return (_a = (await self.loadRecords([descriptor], options))[0]) === null || _a === void 0 ? void 0 : _a.data;
},
async loadRecord(descriptor) {
return (await self.loadRecords([descriptor]))[0];
},
isLoading(ns) {

@@ -1260,2 +1294,13 @@ return cache.isLoading(state.getLanguage(), ns);

},
async loadRequired(options) {
if (!(options === null || options === void 0 ? void 0 : options.language)) {
await initializeLanguage();
}
const requiredRecords = getRequiredDescriptors(options === null || options === void 0 ? void 0 : options.language);
return self.loadRecords(requiredRecords, options);
},
async loadMatrix(options) {
const records = getMatrixRecords(options);
return self.loadRecords(records, options);
},
run() {

@@ -1302,16 +1347,2 @@ checkCorrectConfiguration();

/**
* Listen for specific namespaces changes.
*
* ```
* const sub = tolgee.onUpdate(handler)
*
* // subscribe to selected namespace
* sub.subscribeNs(['common'])
*
* // unsubscribe
* sub.unsubscribe()
* ```
*/
onNsUpdate: controller.onUpdate.listenSome,
/**
* Turn off/on events emitting. Is on by default.

@@ -1354,2 +1385,12 @@ */

/**
* Load records which would be loaded by `run` function
*
* You can provide language if not previously set on tolgee instance
*/
loadRequired: controller.loadRequired,
/**
* Load records in matrix (languages x namespaces)
*/
loadMatrix: controller.loadMatrix,
/**
* Manually load multiple records from `Backend` (or `DevBackend` when in dev mode)

@@ -1382,5 +1423,5 @@ *

/**
* Returns records needed for instance to be `loaded`
* Returns descriptors of records needed for instance to be `loaded`
*/
getRequiredRecords: controller.getRequiredRecords,
getRequiredDescriptors: controller.getRequiredDescriptors,
/**

@@ -1387,0 +1428,0 @@ * @return `true` if tolgee is loading initial data (triggered by `run`).

@@ -1,2 +0,2 @@

"use strict";function e(e){return Boolean(e&&"function"==typeof e.then)}function n(n,t){return e(n)?Promise.resolve(n).then(t):t(n)}function t(n,t,a){function i(e){const a=t(e);throw n.emit(a),console.error(a),a}try{const n=a();return e(n)?n.catch(i):n}catch(e){i(e)}}function a(e){const n=(Array.isArray(e)?e:[e]).map((e=>`'${e}'`)),t=n.slice(-2).join(" or ");return`Tolgee: You need to specify ${[...n.slice(0,-2),t].join(", ")} option`}function i(e){return"string"==typeof e?[e]:Array.isArray(e)?e:void 0}function o(e){return i(e)||[]}function r(e,n){return"object"!=typeof(t=n)||Array.isArray(t)||null===t?o(n):o(null==n?void 0:n[e]);var t}function s(e){return Array.from(new Set(e))}function g(e){return e?e.replace(/\/+$/,""):e}Object.defineProperty(exports,"__esModule",{value:!0});const c=(e,n)=>fetch(e,n);const l=(e=c)=>(n,t)=>{let a=(i=null==t?void 0:t.headers,Object.fromEntries(new Headers(i).entries()));var i;return a["x-api-key"]&&(a=Object.assign({"x-tolgee-sdk-type":"JS","x-tolgee-sdk-version":"prerelease"},a)),e(n,Object.assign(Object.assign({},t),{headers:a}))};function u(e){let n=[];return Object.freeze({listen(e){const t=n=>{e(n)};return n.push(t),{unsubscribe(){n=n.filter((e=>t!==e))}}},emit(t){e()&&n.forEach((e=>e({value:t})))}})}function d(e,n,t){const a=new Set,i=new Set;let r=[];function s(){if(0===r.length)return;const e=r;r=[],a.forEach((e=>{e({value:void 0})}));let t=new Set;e.forEach((e=>{void 0===e?t=void 0:void 0!==t&&e.forEach((e=>t.add(e)))}));!function(e){const t=new Set(n());i.forEach((n=>{(void 0===e||-1!==(null==e?void 0:e.findIndex((e=>t.has(e)||n.namespaces.has(e)))))&&n.fn({value:void 0})}))}(t?Array.from(t.keys()):void 0)}return Object.freeze({emit(n,t){e()&&(r.push(n),t?setTimeout(s,0):s())},listen(e){a.add(e);return{unsubscribe:()=>{a.delete(e)}}},listenSome(e){const n={fn:n=>{e(n)},namespaces:new Set};i.add(n);const a={unsubscribe:()=>{i.delete(n)},subscribeNs:e=>(o(e).forEach((e=>n.namespaces.add(e))),void 0===e&&n.namespaces.add(t()),a)};return a}})}class f extends Error{constructor(e,n,t=!1){const{language:a,namespace:i}=e;super(`Tolgee: Failed to fetch record for "${a}"${i&&` and "${i}"`}`),this.cause=n,this.isDev=t,this.name="RecordFetchError",this.language=a,this.namespace=i}}class p extends Error{constructor(e,n){super(e),this.cause=n,this.name="LanguageDetectorError"}}class v extends Error{constructor(e,n){super(e),this.cause=n,this.name="LanguageStorageError"}}const h=e=>{const n=new Map;return Object.entries(e).forEach((([e,t])=>{null!=t&&("object"!=typeof t?n.set(e,t):h(t).forEach(((t,a)=>{n.set(e+"."+a,t)})))})),n},m=e=>{const[n,...t]=e.split(":");return{language:n,namespace:t.join(":")||""}},b=({language:e,namespace:n})=>n?`${e}:${n}`:e;function y(n,t,a,i,r,g,c){const l=new Map,u=new Map;let d={},p=0;function v(e,t,a){const i=b(e);u.set(i,{data:h(t),version:a}),n.onCacheChange.emit(e)}async function y(i,o){let r;if(o)try{r=await a(i)}catch(e){const t=new f(i,e,!0);n.onError.emit(t),console.warn(t)}return r||(r=await async function(a){function i(e){const t=new f(a,e);throw n.onError.emit(t),console.error(t),t}const o=t(a);if(e(o)){const e=await o.catch(i);if(void 0!==e)return e}const r=d[b(a)];if("function"!=typeof r)return r;try{return await r()}catch(e){i(e)}}(i)),r}const O=Object.freeze({addStaticData(e){e&&(d=Object.assign(Object.assign({},d),e),Object.entries(e).forEach((([e,n])=>{if("function"!=typeof n){const t=m(e),a=u.get(e);a&&0!==a.version||v(t,n,0)}})))},invalidate(){l.clear(),p+=1},addRecord(e,n){v(e,n,p)},exists(e,n=!1){const t=u.get(b(e));return t&&n?t.version===p:Boolean(t)},getRecord(e){var n;return null===(n=u.get(b(i(e))))||void 0===n?void 0:n.data},getTranslation(e,n){var t;return null===(t=u.get(b(e)))||void 0===t?void 0:t.data.get(n)},getTranslationNs(e,n,t){var a;for(const i of e)for(const e of n){const n=null===(a=u.get(b({language:e,namespace:i})))||void 0===a?void 0:a.data.get(t);if(null!=n)return[i]}return s(e)},getTranslationFallback(e,n,t){var a;for(const i of e)for(const e of n){const n=null===(a=u.get(b({language:e,namespace:i})))||void 0===a?void 0:a.data.get(t);if(null!=n)return n}},changeTranslation(e,t,a){var i;const o=null===(i=u.get(b(e)))||void 0===i?void 0:i.data;null==o||o.set(t,a),n.onCacheChange.emit(Object.assign(Object.assign({},e),{key:t}))},isFetching(e){if(r())return!0;if(void 0===e)return l.size>0;const n=o(e);return Boolean(Array.from(l.keys()).find((e=>n.includes(m(e).namespace))))},isLoading(e,n){const t=o(n);return Boolean(r()||Array.from(l.keys()).find((n=>{const a=m(n);return(!t.length||t.includes(a.namespace))&&!O.exists({namespace:a.namespace,language:e})})))},async loadRecords(e,n){const t=e.map((e=>{const t=i(e),a=b(t),o=l.get(a);if(o)return{new:!1,promise:o,keyObject:t,cacheKey:a};const r=y(t,n)||Promise.resolve(void 0);return l.set(a,r),{new:!0,promise:r,keyObject:t,cacheKey:a}}));g.notify(),c.notify();const a=await Promise.all(t.map((e=>e.promise)));return t.forEach(((e,n)=>{const t=l.get(e.cacheKey)!==e.promise;if(e.new&&!t){l.delete(e.cacheKey);const t=a[n];t?O.addRecord(e.keyObject,t):O.getRecord(e.keyObject)||O.addRecord(e.keyObject,{})}})),g.notify(),c.notify(),t.map((e=>O.getRecord(e.keyObject)))},getAllRecords:()=>Array.from(u.entries()).map((([e,n])=>Object.assign(Object.assign({},m(e)),{data:n.data})))});return O}function O(e,n){var t={};for(var a in e)Object.prototype.hasOwnProperty.call(e,a)&&n.indexOf(a)<0&&(t[a]=e[a]);if(null!=e&&"function"==typeof Object.getOwnPropertySymbols){var i=0;for(a=Object.getOwnPropertySymbols(e);i<a.length;i++)n.indexOf(a[i])<0&&Object.prototype.propertyIsEnumerable.call(e,a[i])&&(t[a[i]]=e[a[i]])}return t}const L="invalid",j={defaultNs:"",observerOptions:{tagAttributes:{textarea:["placeholder"],input:["value","placeholder"],img:["alt"],"*":["aria-label","title"]},restrictedElements:["script","style"],highlightKeys:["Alt"],highlightColor:"rgb(255, 0, 0)",highlightWidth:5,inputPrefix:"%-%tolgee:",inputSuffix:"%-%",passToParent:["option","optgroup"],fullKeyEncode:!1},observerType:"invisible",onFormatError:L,apiUrl:"https://app.tolgee.io",fetch:l(),onTranslationMissing:({key:e})=>e},k=(...e)=>{let n={};return e.forEach((e=>{n=Object.assign(Object.assign(Object.assign({},n),e),{observerOptions:Object.assign(Object.assign({},n.observerOptions),null==e?void 0:e.observerOptions)})})),n};function w(e,n){const t=k(j,null==n?void 0:n.initialOptions,e);return t.apiUrl=g(t.apiUrl),(null==e?void 0:e.fetch)&&(t.fetch=l(e.fetch)),{initialOptions:t,activeNamespaces:(null==n?void 0:n.activeNamespaces)||new Map,language:null==n?void 0:n.language,pendingLanguage:null==n?void 0:n.language,isInitialLoading:!1,isRunning:!1}}function E(e,a,i,o,r,s,g,c){const l={ui:void 0},u={formatters:[],finalFormatter:void 0,observer:void 0,devBackend:void 0,backends:[],ui:void 0,languageDetector:void 0,languageStorage:void 0},d=async({keysAndDefaults:e,target:n})=>{var t;const a=e.map((({key:e,ns:n,defaultValue:t})=>({key:e,defaultValue:t,fallbackNamespaces:o(n),namespace:r({key:e,ns:n})[0],translation:s({key:e,ns:n})})));null===(t=u.ui)||void 0===t||t.handleElementClick(a,n)},f=(e,n)=>{var t;return(null===(t=u.observer)||void 0===t?void 0:t.findPositions(e,n))||[]};function h(e){const n=s({key:e.key,ns:e.ns});return D.formatTranslation(Object.assign(Object.assign({},e),{translation:n,formatEnabled:!0}))}function m(){return{fetch:a().fetch}}function b(e){u.observer=null==e?void 0:e()}function y(){return Boolean(u.observer)}function j(e){e&&u.formatters.push(e)}function k(e){u.finalFormatter=e}function w(e){l.ui=e}function E(){return Boolean(l.ui)}function R(e){u.languageStorage=e}function N(e){u.languageDetector=e}function F(e){e&&u.backends.push(e)}function C(e){u.devBackend=e}const D=Object.freeze({addPlugin:function(e,n){n(e,Object.freeze({setFinalFormatter:k,addFormatter:j,setObserver:b,hasObserver:y,setUi:w,hasUi:E,setDevBackend:C,addBackend:F,setLanguageDetector:N,setLanguageStorage:R}))},findPositions:f,run(){var e,n;const{apiKey:t,apiUrl:i,projectId:o,observerOptions:r,tagNewKeys:s,filterTag:p}=a();u.ui=null===(e=l.ui)||void 0===e?void 0:e.call(l,{apiKey:t,apiUrl:i,projectId:o,highlight:D.highlight,changeTranslation:g,findPositions:f,onPermanentChange:e=>c.onPermanentChange.emit(e),tagNewKeys:s,filterTag:p}),null===(n=u.observer)||void 0===n||n.run({mouseHighlight:!0,options:r,translate:h,onClick:d})},stop(){var e;u.ui=void 0,null===(e=u.observer)||void 0===e||e.stop()},getLanguageStorage:()=>u.languageStorage,getInitialLanguage(){const e=i();return n(t(c.onError,(e=>new v("Tolgee: Failed to load language",e)),(()=>{var e;return null===(e=u.languageStorage)||void 0===e?void 0:e.getLanguage(m())})),(n=>e&&!e.includes(n)||!n?function(){if(!u.languageDetector)return;const e=i();return t(c.onError,(e=>new p("Tolgee: Failed to detect language",e)),(()=>{var n;return null===(n=u.languageDetector)||void 0===n?void 0:n.getLanguage(Object.assign({availableLanguages:e},m()))}))}():n))},setStoredLanguage:e=>t(c.onError,(e=>new v("Tolgee: Failed to store language",e)),(()=>{var n;return null===(n=u.languageStorage)||void 0===n?void 0:n.setLanguage(e,m())})),getDevBackend:()=>u.devBackend,getBackendRecord:async({language:e,namespace:n})=>{for(const t of u.backends){const a=await t.getRecord(Object.assign({language:e,namespace:n},m()));if(void 0!==a)return a}},getBackendDevRecord:async({language:e,namespace:n})=>{var t;const{apiKey:i,apiUrl:o,projectId:r,filterTag:s}=a();return null===(t=u.devBackend)||void 0===t?void 0:t.getRecord(Object.assign({apiKey:i,apiUrl:o,projectId:r,language:e,namespace:n,filterTag:s},m()))},getLanguageDetector:()=>u.languageDetector,retranslate(){var e;null===(e=u.observer)||void 0===e||e.retranslate()},highlight:(e,n)=>{var t,a;return(null===(a=null===(t=u.observer)||void 0===t?void 0:t.highlight)||void 0===a?void 0:a.call(t,e,n))||{unhighlight(){}}},unwrap(e){var n;return u.observer?null===(n=u.observer)||void 0===n?void 0:n.unwrap(e):{text:e,keys:[]}},wrap(e){var n;return u.observer?null===(n=u.observer)||void 0===n?void 0:n.wrap(e):e.translation},hasDevBackend:()=>Boolean(D.getDevBackend()),formatTranslation(n){var t,{formatEnabled:i}=n,o=O(n,["formatEnabled"]);const{key:r,translation:s,defaultValue:g,noWrap:c,params:l,ns:d,orEmpty:f}=o,p=null!=s?s:g;let v="";null==s&&(v=a().onTranslationMissing(o));let h=null!=p?p:f?"":v;const m=e(),b=i||!(null===(t=u.observer)||void 0===t?void 0:t.outputNotFormattable),y=e=>u.observer&&!c?u.observer.wrap({key:r,translation:e,defaultValue:g,params:l,ns:d}):e;h=y(h);try{if(p&&m&&b)for(const e of u.formatters)h=e.format({translation:h,language:m,params:l});u.finalFormatter&&p&&m&&b&&(h=u.finalFormatter.format({translation:h,language:m,params:l}))}catch(e){console.error(e);const n=("string"==typeof(j=e)?j:"string"==typeof(null==j?void 0:j.message)?j.message:void 0)||L,t=a().onFormatError,i=typeof t;h="string"===i?t:"function"===i?t(n,o):L,h=y(h)}var j;return h}});return D}const R=(e,n,t)=>{let a=e;return Object.freeze({init(e){a=e},notify(){const e=n();a!==e&&t(e),a=e}})};const N=(e,...n)=>{let t,a={};return"object"==typeof e?a=e:(a.key=e,"string"==typeof n[0]?(a.defaultValue=n[0],t=n[1]):"object"==typeof n[0]&&(t=n[0])),t&&(a=Object.assign(Object.assign({},function(e){var{ns:n,noWrap:t,orEmpty:a,params:i,language:o}=e,r=O(e,["ns","noWrap","orEmpty","params","language"]);const s={ns:n,noWrap:t,orEmpty:a,language:o};return Object.assign(Object.assign({},s),{params:Object.assign({},r)})}(t)),a)),a};function F({options:t}){const i=function(e,n){let t=!0;function a(){return t}const i=Object.freeze({onPendingLanguageChange:u(a),onLanguageChange:u(a),onLoadingChange:u(a),onFetchingChange:u(a),onInitialLoaded:u(a),onRunningChange:u(a),onCacheChange:u(a),onUpdate:d(a,e,n),onPermanentChange:u(a),onError:u(a),setEmitterActive(e){t=e},on:(e,n)=>{switch(e){case"pendingLanguage":return i.onPendingLanguageChange.listen(n);case"language":return i.onLanguageChange.listen(n);case"loading":return i.onLoadingChange.listen(n);case"fetching":return i.onFetchingChange.listen(n);case"initialLoad":return i.onInitialLoaded.listen(n);case"running":return i.onRunningChange.listen(n);case"cache":return i.onCacheChange.listen(n);case"update":return i.onUpdate.listen(n);case"permanentChange":return i.onPermanentChange.listen(n);case"error":return i.onError.listen(n)}}});return i.onInitialLoaded.listen((()=>i.onUpdate.emit())),i.onLanguageChange.listen((()=>i.onUpdate.emit())),i.onCacheChange.listen((({value:e})=>i.onUpdate.emit([e.namespace],!0))),i}(b,O),c=R(!1,(()=>v.isFetching()),i.onFetchingChange.emit),l=R(!1,(()=>T.isLoading()),i.onLoadingChange.emit),f=function(e,n,t){let a,i=w();const c=Object.freeze({init(e){i=w(e,i)},isRunning:()=>i.isRunning,setRunning(e){i.isRunning!==e&&(i.isRunning=e,t.emit(e))},isInitialLoading:()=>i.isInitialLoading,setInitialLoading(e){i.isInitialLoading=e},getLanguage:()=>i.language||i.initialOptions.language,setLanguage(n){i.language!==n&&(i.language=n,e.emit(n))},getPendingLanguage:()=>i.pendingLanguage||c.getLanguage(),setPendingLanguage(e){i.pendingLanguage!==e&&(i.pendingLanguage=e,n.emit(e))},getInitialOptions:()=>Object.assign(Object.assign({},i.initialOptions),a),addActiveNs(e){o(e).forEach((e=>{const n=i.activeNamespaces.get(e);void 0!==n?i.activeNamespaces.set(e,n+1):i.activeNamespaces.set(e,1)}))},removeActiveNs(e){o(e).forEach((e=>{const n=i.activeNamespaces.get(e);void 0!==n&&n>1?i.activeNamespaces.set(e,n-1):i.activeNamespaces.delete(e)}))},getRequiredNamespaces:()=>s([...i.initialOptions.ns||[i.initialOptions.defaultNs],...o(i.initialOptions.fallbackNs),...i.activeNamespaces.keys()]),getFallbackLangs(e){const n=e||c.getLanguage();return n?s([n,...r(n,i.initialOptions.fallbackLanguage)]):[]},getFallbackNs:()=>o(i.initialOptions.fallbackNs),getDefaultNs:e=>void 0===e?i.initialOptions.defaultNs:e,getAvailableLanguages(){if(i.initialOptions.availableLanguages)return i.initialOptions.availableLanguages;if(i.initialOptions.staticData){const e=Object.keys(i.initialOptions.staticData).map((e=>m(e).language));return Array.from(new Set(e))}},withDefaultNs:e=>({namespace:void 0===e.namespace?c.getInitialOptions().defaultNs:e.namespace,language:e.language}),overrideCredentials(e){a=e?Object.assign(Object.assign({},e),{apiUrl:g(e.apiUrl)}):void 0}});return c}(i.onLanguageChange,i.onPendingLanguageChange,i.onRunningChange),p=E(f.getLanguage,f.getInitialOptions,f.getAvailableLanguages,L,P,A,k,i),v=y(i,p.getBackendRecord,p.getBackendDevRecord,f.withDefaultNs,f.isInitialLoading,c,l);let h;function b(){return f.getFallbackNs()}function O(e){return f.getDefaultNs(e)}function L(e){return[...o(O(e)),...b()]}function j(e){return[...o(null!=e?e:O()),...f.getRequiredNamespaces()]}function k(e,n,t){const a=f.withDefaultNs(e),i=v.getTranslation(a,n);return v.changeTranslation(a,n,t),{revert(){v.changeTranslation(a,n,i)}}}function F(e){f.init(e),v.addStaticData(f.getInitialOptions().staticData)}function C(e,n){const t=f.getFallbackLangs(e),a=j(n),i=[];return t.forEach((e=>{a.forEach((n=>{v.exists({language:e,namespace:n},!0)||i.push({language:e,namespace:n})}))})),i}function D(e,t){const a=C(e,t);if(a.length)return n(T.loadRecords(a),(()=>{}))}function P({key:e,ns:n}){const t=f.getFallbackLangs(),a=L(null!=n?n:void 0);return v.getTranslationNs(a,t,e)}function A({key:e,ns:n,language:t}){const a=L(null!=n?n:void 0),i=f.getFallbackLangs(t);return v.getTranslationFallback(a,i,e)}function I(){const t=n(function(){if(f.getLanguage())return;return n(p.getInitialLanguage(),(e=>{const n=e||f.getInitialOptions().defaultLanguage;n&&f.setLanguage(n)}))}(),(()=>D()));if(e(t))return f.setInitialLoading(!0),c.notify(),l.notify(),Promise.resolve(t).then((()=>{f.setInitialLoading(!1),c.notify(),l.notify(),i.onInitialLoaded.emit()}));i.onInitialLoaded.emit()}t&&F(t),i.onUpdate.listen((()=>{f.isRunning()&&p.retranslate()}));const T=Object.freeze(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign({},i),f),p),v),{init:F,getTranslation:A,changeTranslation:k,getTranslationNs:P,getDefaultAndFallbackNs:L,findPositions:p.findPositions,getRequiredRecords:C,async changeLanguage(e){f.getPendingLanguage()===e&&f.getLanguage()===e||(f.setPendingLanguage(e),f.isRunning()&&await D(e),e===f.getPendingLanguage()&&(f.setLanguage(e),await p.setStoredLanguage(e)))},async addActiveNs(e,n){n||f.addActiveNs(e),f.isRunning()&&await D(void 0,e)},loadRecords:e=>v.loadRecords(e,T.isDev()),loadRecord:async e=>(await T.loadRecords([e]))[0],isLoading:e=>v.isLoading(f.getLanguage(),e),isLoaded(e){const n=f.getLanguage();if(!n)return!1;const t=f.getFallbackLangs(n),a=j(e),i=[];return t.forEach((e=>{a.forEach((n=>{v.exists({language:e,namespace:n})||i.push({language:e,namespace:n})}))})),0===i.length},t:(...e)=>{const n=N(...e),t=A(n);return p.formatTranslation(Object.assign(Object.assign({},n),{translation:t}))},isDev:()=>Boolean(f.getInitialOptions().apiKey&&f.getInitialOptions().apiUrl),run:()=>(function(){if((p.getLanguageDetector()||p.getLanguageStorage())&&!f.getAvailableLanguages())throw new Error(a("availableLanguages"));if(!f.getLanguage()&&!f.getInitialOptions().defaultLanguage)throw new Error(a(["defaultLanguage","language"]))}(),f.isRunning()||(f.setRunning(!0),p.run(),h=I()),Promise.resolve(h)),stop(){f.isRunning()&&(p.stop(),f.setRunning(!1))}}));return T}const C=0,D=1,P=2;class A extends Error{constructor(e,n,t){let a,i="";e===C?a="Empty parameter":e===D?(a="Unexpected character",i="Did you forget to use FormatIcu to render ICU message syntax?"):a="Unexpected end",super(`Tolgee parser: ${a} at ${n} in "${t}"`+(i?"\n"+i:"")),this.code=e,this.index=n}}function I(e){return/\s/.test(e)}const T=0,S=1,x=2,U=3,B=4,z=new Set([x,S,T]),K="'",$=new Set(["{","}",K]),M=e=>/[0-9a-zA-Z_]/.test(e);function q(e,n){const[t,a]=function(e){let n=T,t="",a="",i="";const o=[],r=[];let s=0;function g(n){throw new A(n,s,e)}const c=()=>{o.push(t),t=""},l=()=>{""===a&&g(C),r.push(a),a=""};for(s=0;s<e.length;s++)switch(i=e[s],n){case T:i===K?(t+=i,n=S):"{"===i?(c(),n=U):(t+=i,n=T);break;case S:$.has(i)?(t=t.slice(0,-1)+i,n=x):(t+=i,n=T);break;case x:i===K?n=T:(t+=i,n=x);break;case U:"}"===i?(l(),n=T):I(i)?""!==a&&(l(),n=B):(M(i)||g(D),a+=i,n=U);break;case B:"}"==i?n=T:I(i)?n=B:g(D)}return z.has(n)||g(P),c(),[o,r]}(e),i=[t[0]];for(let o=1;o<t.length;o++){const r=null==n?void 0:n[a[o-1]];if(void 0===r)throw new Error(`Missing parameter "${a[o-1]}" in "${e}"`);i.push(String(r)),i.push(t[o])}return i.join("")}exports.FormatSimple=()=>(e,n)=>(n.setFinalFormatter({format:({translation:e,params:n})=>q(e,n)}),e),exports.LanguageDetectorError=p,exports.LanguageStorageError=v,exports.RecordFetchError=f,exports.TolgeeCore=()=>{const e={plugins:[],options:{}},n=Object.freeze({use:t=>(e.plugins.push(t),n),updateDefaults:t=>(e.options=k(e.options,t),n),init(n){const t=function(e){const n=F({options:e});function t(e){const t=n.isRunning();t&&n.stop(),e(),n.isDev()&&n.invalidate(),t&&n.run()}n.isDev()&&n.invalidate();const a=Object.freeze({on:n.on,onNsUpdate:n.onUpdate.listenSome,setEmitterActive:n.setEmitterActive,getLanguage:n.getLanguage,getPendingLanguage:n.getPendingLanguage,changeLanguage:n.changeLanguage,changeTranslation:n.changeTranslation,addActiveNs:n.addActiveNs,removeActiveNs:n.removeActiveNs,loadRecords:n.loadRecords,loadRecord:n.loadRecord,addStaticData:n.addStaticData,getRecord:n.getRecord,getAllRecords:n.getAllRecords,isLoaded:n.isLoaded,getRequiredRecords:n.getRequiredRecords,isInitialLoading:n.isInitialLoading,isLoading:n.isLoading,isFetching:n.isFetching,isRunning:n.isRunning,run:n.run,stop:n.stop,t:n.t,highlight:n.highlight,findPositions:n.findPositions,getInitialOptions:n.getInitialOptions,isDev:n.isDev,wrap:n.wrap,unwrap:n.unwrap,overrideCredentials(e){t((()=>n.overrideCredentials(e)))},addPlugin(e){e&&t((()=>n.addPlugin(a,e)))},updateOptions(e){e&&t((()=>n.init(e)))}});return a}(k(e.options,n));return e.plugins.forEach(t.addPlugin),t}});return n},exports.createFetchFunction=l,exports.getFallback=i,exports.getFallbackArray=o,exports.getTranslateProps=N;
"use strict";function e(e){return Boolean(e&&"function"==typeof e.then)}function n(n,a){return e(n)?Promise.resolve(n).then(a):a(n)}function a(n,a,t){function i(e){const t=a(e);throw n.emit(t),console.error(t),t}try{const n=t();return e(n)?n.catch(i):n}catch(e){i(e)}}function t(e){const n=(Array.isArray(e)?e:[e]).map((e=>`'${e}'`)),a=n.slice(-2).join(" or ");return`Tolgee: You need to specify ${[...n.slice(0,-2),a].join(", ")} option`}function i(e){return"string"==typeof e?[e]:Array.isArray(e)?e:void 0}function r(e){return i(e)||[]}function o(e,n){return"object"!=typeof(a=n)||Array.isArray(a)||null===a?r(n):r(null==n?void 0:n[e]);var a}function s(e){return Array.from(new Set(e))}function g(e){return e?e.replace(/\/+$/,""):e}Object.defineProperty(exports,"__esModule",{value:!0});const l=(e,n)=>fetch(e,n);const c=(e=l)=>(n,a)=>{let t=(i=null==a?void 0:a.headers,Object.fromEntries(new Headers(i).entries()));var i;return t["x-api-key"]&&(t=Object.assign({"x-tolgee-sdk-type":"JS","x-tolgee-sdk-version":"prerelease"},t)),e(n,Object.assign(Object.assign({},a),{headers:t}))},u=(e,n)=>{let a=[];return{listen(e){const n=n=>{e(n)};return a.push(n),{unsubscribe(){a=a.filter((e=>n!==e))}}},emit(t){n()&&a.forEach((n=>n({type:e,value:t})))}}};function d(e){let n=[],a=[];function t(){if(0===a.length)return;const e=a;a=[],n.forEach((n=>{n(e)}))}return Object.freeze({listen(e){const a=n=>{e(n)};return n.push(a),{unsubscribe(){n=n.filter((e=>a!==e))}}},emit(n,i){e()&&e()&&(a.push(n),i?setTimeout(t,0):t())}})}class p extends Error{constructor(e,n,a=!1){const{language:t,namespace:i}=e;super(`Tolgee: Failed to fetch record for "${t}"${i&&` and "${i}"`}`),this.cause=n,this.isDev=a,this.name="RecordFetchError",this.language=t,this.namespace=i}}class f extends Error{constructor(e,n){super(e),this.cause=n,this.name="LanguageDetectorError"}}class v extends Error{constructor(e,n){super(e),this.cause=n,this.name="LanguageStorageError"}}const h=e=>{const n=new Map;return Object.entries(e).forEach((([e,a])=>{null!=a&&("object"!=typeof a?n.set(e,a):h(a).forEach(((a,t)=>{n.set(e+"."+t,a)})))})),n},m=e=>Object.fromEntries(h(e).entries()),b=e=>{const[n,...a]=e.split(":");return{language:n,namespace:a.join(":")||""}},y=({language:e,namespace:n})=>n?`${e}:${n}`:e;function L(n,a,t,i,o,g,l,c){const u=new Map,d=new Map;let f={},v=0;function h(e,a,t){const i=y(e);d.set(i,{data:m(a),version:t}),n.onCacheChange.emit(b(i))}async function L(i,r){let o;if(r)try{o=await t(i)}catch(e){const a=new p(i,e,!0);n.onError.emit(a),console.warn(a)}return o||(o=await async function(t){function i(e){const a=new p(t,e);throw n.onError.emit(a),console.error(a),a}const r=a(t);if(e(r)){const e=await r.catch(i);if(void 0!==e)return e}const o=f[y(t)];if("function"!=typeof o)return o;try{return await o()}catch(e){i(e)}}(i)),o}const O=Object.freeze({addStaticData(e){if(Array.isArray(e))for(const n of e){const e=y(n),a=d.get(e);a&&0!==a.version||h(n,m(n.data),0)}else e&&(f=Object.assign(Object.assign({},f),e),Object.entries(e).forEach((([e,n])=>{if("function"!=typeof n){const a=b(e),t=d.get(e);t&&0!==t.version||h(a,m(n),0)}})))},invalidate(){u.clear(),v+=1},addRecord(e,n){h(e,m(n),v)},exists(e,n=!1){const a=d.get(y(e));return a&&n?a.version===v:Boolean(a)},getRecord(e){const n=i(e),a=y(n),t=d.get(a);if(t)return Object.assign(Object.assign({},n),{cacheKey:a,data:t.data})},getAllRecords:()=>Array.from(d.entries()).map((([e])=>O.getRecord(b(e)))),getTranslation(e,n){var a;return null===(a=d.get(y(e)))||void 0===a?void 0:a.data[n]},getTranslationNs(e,n,a){var t;for(const i of e)for(const e of n){const n=null===(t=d.get(y({language:e,namespace:i})))||void 0===t?void 0:t.data[a];if(null!=n)return[i]}return s(e)},getTranslationFallback(e,n,a){var t;for(const i of e)for(const e of n){const n=null===(t=d.get(y({language:e,namespace:i})))||void 0===t?void 0:t.data[a];if(null!=n)return n}},changeTranslation(e,a,t){var i;const r=null===(i=d.get(y(e)))||void 0===i?void 0:i.data;(null==r?void 0:r[a])&&(r[a]=t,n.onCacheChange.emit(Object.assign(Object.assign({},e),{key:a})))},isFetching(e){if(o())return!0;if(void 0===e)return u.size>0;const n=r(e);return Boolean(Array.from(u.keys()).find((e=>n.includes(b(e).namespace))))},isLoading(e,n){const a=r(n);if(o())return!0;const t=Array.from(u.keys());return Boolean(t.find((n=>{const t=b(n);return(!a.length||a.includes(t.namespace))&&!O.exists({namespace:t.namespace,language:e})})))},async loadRecords(e,n){const a=e.map((e=>{const a=i(e),t=y(a);if(null==n?void 0:n.useCache){if(O.exists(a,!0))return Object.assign(Object.assign({},a),{new:!1,cacheKey:t,data:O.getRecord(a).data})}const r=u.get(t);if(r)return Object.assign(Object.assign({},a),{new:!1,promise:r,cacheKey:t});const o=L(a,!(null==n?void 0:n.noDev))||Promise.resolve(void 0);return u.set(t,o),Object.assign(Object.assign({},a),{new:!0,promise:o,cacheKey:t})}));l.notify(),c.notify();const t=a.map((e=>e.promise)).filter(Boolean),r=await Promise.all(t);return a.forEach((e=>{var n;e.promise&&(e.data=m(null!==(n=r[0])&&void 0!==n?n:{}),r.shift());const a=u.get(e.cacheKey)!==e.promise;e.new&&!a&&(u.delete(e.cacheKey),e.data?O.addRecord(e,e.data):O.getRecord(e)||O.addRecord(e,{}))})),l.notify(),c.notify(),a.map((e=>{var n;return{language:e.language,namespace:e.namespace,data:null!==(n=e.data)&&void 0!==n?n:{},cacheKey:e.cacheKey}}))}});return O}function O(e,n){var a={};for(var t in e)Object.prototype.hasOwnProperty.call(e,t)&&n.indexOf(t)<0&&(a[t]=e[t]);if(null!=e&&"function"==typeof Object.getOwnPropertySymbols){var i=0;for(t=Object.getOwnPropertySymbols(e);i<t.length;i++)n.indexOf(t[i])<0&&Object.prototype.propertyIsEnumerable.call(e,t[i])&&(a[t[i]]=e[t[i]])}return a}const j="invalid",w={observerOptions:{tagAttributes:{textarea:["placeholder"],input:["value","placeholder"],img:["alt"],"*":["aria-label","title"]},restrictedElements:["script","style"],highlightKeys:["Alt"],highlightColor:"rgb(255, 0, 0)",highlightWidth:5,inputPrefix:"%-%tolgee:",inputSuffix:"%-%",passToParent:["option","optgroup"],fullKeyEncode:!1},observerType:"invisible",onFormatError:j,apiUrl:"https://app.tolgee.io",autoLoadRequiredData:!0,fetch:c(),onTranslationMissing:({key:e})=>e,disableCache:!1},R=(...e)=>{let n={};return e.forEach((e=>{n=Object.assign(Object.assign(Object.assign({},n),e),{observerOptions:Object.assign(Object.assign({},n.observerOptions),null==e?void 0:e.observerOptions)})})),n};function k(e,n){const a=R(w,null==n?void 0:n.initialOptions,e);return a.apiUrl=g(a.apiUrl),(null==e?void 0:e.fetch)&&(a.fetch=c(e.fetch)),{initialOptions:a,activeNamespaces:(null==n?void 0:n.activeNamespaces)||new Map,language:null==n?void 0:n.language,pendingLanguage:null==n?void 0:n.language,isInitialLoading:!1,isRunning:!1}}function E(e,t,i,r,o,s,g,l){const c={ui:void 0},u={formatters:[],finalFormatter:void 0,observer:void 0,devBackend:void 0,backends:[],ui:void 0,languageDetector:void 0,languageStorage:void 0},d=async({keysAndDefaults:e,target:n})=>{var a;const t=e.map((({key:e,ns:n,defaultValue:a})=>({key:e,defaultValue:a,fallbackNamespaces:r(n),namespace:o({key:e,ns:n})[0],translation:s({key:e,ns:n})})));null===(a=u.ui)||void 0===a||a.handleElementClick(t,n)},p=(e,n)=>{var a;return(null===(a=u.observer)||void 0===a?void 0:a.findPositions(e,n))||[]};function h(e){const n=s({key:e.key,ns:e.ns});return A.formatTranslation(Object.assign(Object.assign({},e),{translation:n,formatEnabled:!0}))}function m(){return{fetch:t().fetch}}function b(e){u.observer=null==e?void 0:e()}function y(){return Boolean(u.observer)}function L(e){e&&u.formatters.push(e)}function w(e){u.finalFormatter=e}function R(e){c.ui=e}function k(){return Boolean(c.ui)}function E(e){u.languageStorage=e}function D(e){u.languageDetector=e}function C(e){e&&u.backends.push(e)}function N(e){u.devBackend=e}const A=Object.freeze({addPlugin:function(e,n){n(e,Object.freeze({setFinalFormatter:w,addFormatter:L,setObserver:b,hasObserver:y,setUi:R,hasUi:k,setDevBackend:N,addBackend:C,setLanguageDetector:D,setLanguageStorage:E}))},findPositions:p,run(){var e,n;const{apiKey:a,apiUrl:i,projectId:r,observerOptions:o,tagNewKeys:s,filterTag:f}=t();u.ui=null===(e=c.ui)||void 0===e?void 0:e.call(c,{apiKey:a,apiUrl:i,projectId:r,highlight:A.highlight,changeTranslation:g,findPositions:p,onPermanentChange:e=>l.onPermanentChange.emit(e),tagNewKeys:s,filterTag:f}),null===(n=u.observer)||void 0===n||n.run({mouseHighlight:!0,options:o,translate:h,onClick:d})},stop(){var e;u.ui=void 0,null===(e=u.observer)||void 0===e||e.stop()},getLanguageStorage:()=>u.languageStorage,getInitialLanguage(){const e=i();return n(a(l.onError,(e=>new v("Tolgee: Failed to load language",e)),(()=>{var e;return null===(e=u.languageStorage)||void 0===e?void 0:e.getLanguage(m())})),(n=>e&&!e.includes(n)||!n?function(){if(!u.languageDetector)return;const e=i();return a(l.onError,(e=>new f("Tolgee: Failed to detect language",e)),(()=>{var n;return null===(n=u.languageDetector)||void 0===n?void 0:n.getLanguage(Object.assign({availableLanguages:e},m()))}))}():n))},setStoredLanguage:e=>a(l.onError,(e=>new v("Tolgee: Failed to store language",e)),(()=>{var n;return null===(n=u.languageStorage)||void 0===n?void 0:n.setLanguage(e,m())})),getDevBackend:()=>u.devBackend,getBackendRecord:async({language:e,namespace:n})=>{for(const a of u.backends){const t=await a.getRecord(Object.assign({language:e,namespace:n},m()));if(void 0!==t)return t}},getBackendDevRecord:async({language:e,namespace:n})=>{var a;const{apiKey:i,apiUrl:r,projectId:o,filterTag:s}=t();if(i&&r&&A.hasDevBackend())return null===(a=u.devBackend)||void 0===a?void 0:a.getRecord(Object.assign({apiKey:i,apiUrl:r,projectId:o,language:e,namespace:n,filterTag:s},m()))},getLanguageDetector:()=>u.languageDetector,retranslate(){var e;null===(e=u.observer)||void 0===e||e.retranslate()},highlight:(e,n)=>{var a,t;return(null===(t=null===(a=u.observer)||void 0===a?void 0:a.highlight)||void 0===t?void 0:t.call(a,e,n))||{unhighlight(){}}},unwrap(e){var n;return u.observer?null===(n=u.observer)||void 0===n?void 0:n.unwrap(e):{text:e,keys:[]}},wrap(e){var n;return u.observer?null===(n=u.observer)||void 0===n?void 0:n.wrap(e):e.translation},hasDevBackend:()=>Boolean(A.getDevBackend()),formatTranslation(n){var a,{formatEnabled:i}=n,r=O(n,["formatEnabled"]);const{key:o,translation:s,defaultValue:g,noWrap:l,params:c,ns:d,orEmpty:p}=r,f=null!=s?s:g;let v="";null==s&&(v=t().onTranslationMissing(r));let h=null!=f?f:p?"":v;const m=e(),b=i||!(null===(a=u.observer)||void 0===a?void 0:a.outputNotFormattable),y=e=>u.observer&&!l?u.observer.wrap({key:o,translation:e,defaultValue:g,params:c,ns:d}):e;h=y(h);try{if(f&&m&&b)for(const e of u.formatters)h=e.format({translation:h,language:m,params:c});u.finalFormatter&&f&&m&&b&&(h=u.finalFormatter.format({translation:h,language:m,params:c}))}catch(e){console.error(e);const n=("string"==typeof(L=e)?L:"string"==typeof(null==L?void 0:L.message)?L.message:void 0)||j,a=t().onFormatError,i=typeof a;h="string"===i?a:"function"===i?a(n,r):j,h=y(h)}var L;return h}});return A}const D=(e,n,a)=>{let t=e;return Object.freeze({init(e){t=e},notify(){const e=n();t!==e&&a(e),t=e}})};const C=(e,...n)=>{let a,t={};return"object"==typeof e?t=e:(t.key=e,"string"==typeof n[0]?(t.defaultValue=n[0],a=n[1]):"object"==typeof n[0]&&(a=n[0])),a&&(t=Object.assign(Object.assign({},function(e){var{ns:n,noWrap:a,orEmpty:t,params:i,language:r}=e,o=O(e,["ns","noWrap","orEmpty","params","language"]);const s={ns:n,noWrap:a,orEmpty:t,language:r};return Object.assign(Object.assign({},s),{params:Object.assign({},o)})}(a)),t)),t};function N({options:a}){const i=function(){let e=!0;function n(){return e}const a=Object.freeze({onPendingLanguageChange:u("pendingLanguage",n),onLanguageChange:u("language",n),onLoadingChange:u("loading",n),onFetchingChange:u("fetching",n),onInitialLoaded:u("initialLoad",n),onRunningChange:u("running",n),onCacheChange:u("cache",n),onPermanentChange:u("permanentChange",n),onError:u("error",n),onUpdate:d(n),setEmitterActive(n){e=n},on:(e,n)=>{switch(e){case"pendingLanguage":return a.onPendingLanguageChange.listen(n);case"language":return a.onLanguageChange.listen(n);case"loading":return a.onLoadingChange.listen(n);case"fetching":return a.onFetchingChange.listen(n);case"initialLoad":return a.onInitialLoaded.listen(n);case"running":return a.onRunningChange.listen(n);case"cache":return a.onCacheChange.listen(n);case"update":return a.onUpdate.listen(n);case"permanentChange":return a.onPermanentChange.listen(n);case"error":return a.onError.listen(n)}}});return a.onInitialLoaded.listen((e=>a.onUpdate.emit(e,!1))),a.onLanguageChange.listen((e=>a.onUpdate.emit(e,!1))),a.onCacheChange.listen((e=>a.onUpdate.emit(e,!0))),a}(),l=D(!1,(()=>v.isFetching()),i.onFetchingChange.emit),c=D(!1,(()=>x.isLoading()),i.onLoadingChange.emit),p=function(e,n,a){let t,i=k();const l=Object.freeze({init(e){i=k(e,i)},isRunning:()=>i.isRunning,setRunning(e){i.isRunning!==e&&(i.isRunning=e,a.emit(e))},isInitialLoading:()=>i.isInitialLoading,isCacheDisabled:()=>i.initialOptions.disableCache,setInitialLoading(e){i.isInitialLoading=e},getLanguage:()=>i.language||i.initialOptions.language,setLanguage(n){i.language!==n&&(i.language=n,e.emit(n))},getPendingLanguage:()=>i.pendingLanguage||l.getLanguage(),setPendingLanguage(e){i.pendingLanguage!==e&&(i.pendingLanguage=e,n.emit(e))},getInitialOptions:()=>Object.assign(Object.assign({},i.initialOptions),t),addActiveNs(e){r(e).forEach((e=>{const n=i.activeNamespaces.get(e);void 0!==n?i.activeNamespaces.set(e,n+1):i.activeNamespaces.set(e,1)}))},removeActiveNs(e){r(e).forEach((e=>{const n=i.activeNamespaces.get(e);void 0!==n&&n>1?i.activeNamespaces.set(e,n-1):i.activeNamespaces.delete(e)}))},getRequiredNamespaces:()=>s([l.getDefaultNs(),...i.initialOptions.ns||[],...r(i.initialOptions.fallbackNs),...i.activeNamespaces.keys()]),getFallbackLangs(e){const n=e||l.getLanguage();return n?s([n,...o(n,i.initialOptions.fallbackLanguage)]):[]},getFallbackNs:()=>r(i.initialOptions.fallbackNs),getNs(){var e,n;return(null===(e=i.initialOptions.ns)||void 0===e?void 0:e.length)?i.initialOptions.ns:[null!==(n=i.initialOptions.defaultNs)&&void 0!==n?n:""]},getDefaultNs(e){var n,a,t;return void 0===e?null!==(t=null!==(n=i.initialOptions.defaultNs)&&void 0!==n?n:null===(a=i.initialOptions.ns)||void 0===a?void 0:a[0])&&void 0!==t?t:"":e},getAvailableLanguages(){if(i.initialOptions.availableLanguages)return i.initialOptions.availableLanguages;if(i.initialOptions.staticData){const e=Object.keys(i.initialOptions.staticData).map((e=>b(e).language));return Array.from(new Set(e))}},getAvailableNs:()=>i.initialOptions.availableNs,withDefaultNs:e=>({namespace:void 0===e.namespace?l.getDefaultNs():e.namespace,language:e.language}),overrideCredentials(e){t=e?Object.assign(Object.assign({},e),{apiUrl:g(e.apiUrl)}):void 0}});return l}(i.onLanguageChange,i.onPendingLanguageChange,i.onRunningChange),f=E(p.getLanguage,p.getInitialOptions,p.getAvailableLanguages,y,N,A,j,i),v=L(i,f.getBackendRecord,f.getBackendDevRecord,p.withDefaultNs,p.isInitialLoading,p.isCacheDisabled,l,c);let h;function m(e){return p.getDefaultNs(e)}function y(e){return s([...r(m(e)),...p.getFallbackNs()])}function O(e){return s([...r(null!=e?e:m()),...p.getRequiredNamespaces()])}function j(e,n,a){const t=p.withDefaultNs(e),i=v.getTranslation(t,n);return v.changeTranslation(t,n,a),{revert(){v.changeTranslation(t,n,i)}}}function w(e){p.init(e),v.addStaticData(p.getInitialOptions().staticData)}function R(e,n){const a=p.getFallbackLangs(e),t=O(n),i=[];return a.forEach((e=>{t.forEach((n=>{i.push({language:e,namespace:n})}))})),i}function N({key:e,ns:n}){const a=p.getFallbackLangs(),t=y(null!=n?n:void 0);return v.getTranslationNs(t,a,e)}function A({key:e,ns:n,language:a}){const t=y(null!=n?n:void 0),i=p.getFallbackLangs(a);return v.getTranslationFallback(t,i,e)}function F(){const a=n(P(),(()=>{const e=R(n,a).filter((e=>!v.exists(e,!0)));var n,a;if(e.length&&p.getInitialOptions().autoLoadRequiredData)return v.loadRecords(e,{useCache:!0})}));if(e(a))return p.setInitialLoading(!0),l.notify(),c.notify(),Promise.resolve(a).then((()=>{p.setInitialLoading(!1),l.notify(),c.notify(),i.onInitialLoaded.emit()}));i.onInitialLoaded.emit()}function P(){if(p.getLanguage())return;return n(f.getInitialLanguage(),(e=>{const n=e||p.getInitialOptions().defaultLanguage;n&&p.setLanguage(n)}))}a&&w(a),i.onUpdate.listen((()=>{p.isRunning()&&f.retranslate()}));const x=Object.freeze(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign({},i),p),f),v),{init:w,getTranslation:A,changeTranslation:j,getTranslationNs:N,getDefaultAndFallbackNs:y,findPositions:f.findPositions,getRequiredDescriptors:R,async changeLanguage(e){p.getPendingLanguage()===e&&p.getLanguage()===e||(p.setPendingLanguage(e),p.isRunning()&&p.getInitialOptions().autoLoadRequiredData&&await v.loadRecords(R(e),{useCache:!0}),e===p.getPendingLanguage()&&(p.setLanguage(e),await f.setStoredLanguage(e)))},async addActiveNs(e,n){n||p.addActiveNs(e),p.isRunning()&&await v.loadRecords(R(void 0,e),{useCache:!0})},async loadRecord(e,n){var a;return null===(a=(await x.loadRecords([e],n))[0])||void 0===a?void 0:a.data},isLoading:e=>v.isLoading(p.getLanguage(),e),isLoaded(e){const n=p.getLanguage();if(!n)return!1;const a=p.getFallbackLangs(n),t=O(e),i=[];return a.forEach((e=>{t.forEach((n=>{v.exists({language:e,namespace:n})||i.push({language:e,namespace:n})}))})),0===i.length},t:(...e)=>{const n=C(...e),a=A(n);return f.formatTranslation(Object.assign(Object.assign({},n),{translation:a}))},isDev:()=>Boolean(p.getInitialOptions().apiKey&&p.getInitialOptions().apiUrl),async loadRequired(e){(null==e?void 0:e.language)||await P();const n=R(null==e?void 0:e.language);return x.loadRecords(n,e)},async loadMatrix(e){const n=function(e){let n=[],a=[];if(Array.isArray(e.languages))n=e.languages;else if("all"===e.languages){const e=x.getAvailableLanguages();if(!e)throw new Error(t("availableLanguages"));n=e}if(Array.isArray(e.namespaces))a=e.namespaces;else if("all"===e.namespaces){const e=x.getAvailableNs();if(!e)throw new Error(t("availableNs"));a=e}const i=[];return n.forEach((e=>{a.forEach((n=>{i.push({language:e,namespace:n})}))})),i}(e);return x.loadRecords(n,e)},run:()=>(function(){if((f.getLanguageDetector()||f.getLanguageStorage())&&!p.getAvailableLanguages())throw new Error(t("availableLanguages"));if(!p.getLanguage()&&!p.getInitialOptions().defaultLanguage)throw new Error(t(["defaultLanguage","language"]))}(),p.isRunning()||(p.setRunning(!0),f.run(),h=F()),Promise.resolve(h)),stop(){p.isRunning()&&(f.stop(),p.setRunning(!1))}}));return x}const A=0,F=1,P=2;class x extends Error{constructor(e,n,a){let t,i="";e===A?t="Empty parameter":e===F?(t="Unexpected character",i="Did you forget to use FormatIcu to render ICU message syntax?"):t="Unexpected end",super(`Tolgee parser: ${t} at ${n} in "${a}"`+(i?"\n"+i:"")),this.code=e,this.index=n}}function I(e){return/\s/.test(e)}const T=0,S=1,B=2,U=3,K=4,$=new Set([B,S,T]),z="'",q=new Set(["{","}",z]),M=e=>/[0-9a-zA-Z_]/.test(e);function V(e,n){const[a,t]=function(e){let n=T,a="",t="",i="";const r=[],o=[];let s=0;function g(n){throw new x(n,s,e)}const l=()=>{r.push(a),a=""},c=()=>{""===t&&g(A),o.push(t),t=""};for(s=0;s<e.length;s++)switch(i=e[s],n){case T:i===z?(a+=i,n=S):"{"===i?(l(),n=U):(a+=i,n=T);break;case S:q.has(i)?(a=a.slice(0,-1)+i,n=B):(a+=i,n=T);break;case B:i===z?n=T:(a+=i,n=B);break;case U:"}"===i?(c(),n=T):I(i)?""!==t&&(c(),n=K):(M(i)||g(F),t+=i,n=U);break;case K:"}"==i?n=T:I(i)?n=K:g(F)}return $.has(n)||g(P),l(),[r,o]}(e),i=[a[0]];for(let r=1;r<a.length;r++){const o=null==n?void 0:n[t[r-1]];if(void 0===o)throw new Error(`Missing parameter "${t[r-1]}" in "${e}"`);i.push(String(o)),i.push(a[r])}return i.join("")}exports.FormatSimple=()=>(e,n)=>(n.setFinalFormatter({format:({translation:e,params:n})=>V(e,n)}),e),exports.LanguageDetectorError=f,exports.LanguageStorageError=v,exports.RecordFetchError=p,exports.TolgeeCore=()=>{const e={plugins:[],options:{}},n=Object.freeze({use:a=>(e.plugins.push(a),n),updateDefaults:a=>(e.options=R(e.options,a),n),init(n){const a=function(e){const n=N({options:e});function a(e){const a=n.isRunning();a&&n.stop(),e(),n.isDev()&&n.invalidate(),a&&n.run()}n.isDev()&&n.invalidate();const t=Object.freeze({on:n.on,setEmitterActive:n.setEmitterActive,getLanguage:n.getLanguage,getPendingLanguage:n.getPendingLanguage,changeLanguage:n.changeLanguage,changeTranslation:n.changeTranslation,addActiveNs:n.addActiveNs,removeActiveNs:n.removeActiveNs,loadRequired:n.loadRequired,loadMatrix:n.loadMatrix,loadRecords:n.loadRecords,loadRecord:n.loadRecord,addStaticData:n.addStaticData,getRecord:n.getRecord,getAllRecords:n.getAllRecords,isLoaded:n.isLoaded,getRequiredDescriptors:n.getRequiredDescriptors,isInitialLoading:n.isInitialLoading,isLoading:n.isLoading,isFetching:n.isFetching,isRunning:n.isRunning,run:n.run,stop:n.stop,t:n.t,highlight:n.highlight,findPositions:n.findPositions,getInitialOptions:n.getInitialOptions,isDev:n.isDev,wrap:n.wrap,unwrap:n.unwrap,overrideCredentials(e){a((()=>n.overrideCredentials(e)))},addPlugin(e){e&&a((()=>n.addPlugin(t,e)))},updateOptions(e){e&&a((()=>n.init(e)))}});return t}(R(e.options,n));return e.plugins.forEach(a.addPlugin),a}});return n},exports.createFetchFunction=c,exports.getFallback=i,exports.getFallbackArray=r,exports.getTranslateProps=C;
//# sourceMappingURL=tolgee.cjs.min.js.map

@@ -89,5 +89,5 @@ function isPromise(value) {

function EventEmitter(isActive) {
const EventEmitter = (type, isActive) => {
let handlers = [];
return Object.freeze({
return {
listen(handler) {

@@ -106,23 +106,10 @@ const handlerWrapper = (e) => {

if (isActive()) {
handlers.forEach((handler) => handler({ value: data }));
handlers.forEach((handler) => handler({ type: type, value: data }));
}
},
});
}
};
};
function EventEmitterSelective(isActive, getFallbackNs, getDefaultNs) {
const listeners = new Set();
const partialListeners = new Set();
function callHandlers(ns) {
// everything is implicitly subscribed to fallbacks
// as it can always fall through to it
const fallbackNamespaces = new Set(getFallbackNs());
partialListeners.forEach((handler) => {
const nsMatches = ns === undefined ||
(ns === null || ns === void 0 ? void 0 : ns.findIndex((ns) => fallbackNamespaces.has(ns) || handler.namespaces.has(ns))) !== -1;
if (nsMatches) {
handler.fn({ value: undefined });
}
});
}
function EventEmitterCombined(isActive) {
let handlers = [];
let queue = [];

@@ -136,63 +123,30 @@ // merge events in queue into one event

queue = [];
listeners.forEach((handler) => {
handler({ value: undefined });
handlers.forEach((handler) => {
handler(queueCopy);
});
let namespaces = new Set();
queueCopy.forEach((ns) => {
if (ns === undefined) {
// when no ns specified, it affects all namespaces
namespaces = undefined;
}
else if (namespaces !== undefined) {
ns.forEach((ns) => namespaces.add(ns));
}
});
const namespacesArray = namespaces
? Array.from(namespaces.keys())
: undefined;
callHandlers(namespacesArray);
}
return Object.freeze({
emit(ns, delayed) {
if (isActive()) {
queue.push(ns);
if (!delayed) {
solveQueue();
}
else {
setTimeout(solveQueue, 0);
}
}
},
listen(handler) {
listeners.add(handler);
const result = {
unsubscribe: () => {
listeners.delete(handler);
const handlerWrapper = (events) => {
handler(events);
};
handlers.push(handlerWrapper);
return {
unsubscribe() {
handlers = handlers.filter((i) => handlerWrapper !== i);
},
};
return result;
},
listenSome(handler) {
const handlerWrapper = {
fn: (e) => {
handler(e);
},
namespaces: new Set(),
};
partialListeners.add(handlerWrapper);
const result = {
unsubscribe: () => {
partialListeners.delete(handlerWrapper);
},
subscribeNs: (ns) => {
getFallbackArray(ns).forEach((val) => handlerWrapper.namespaces.add(val));
if (ns === undefined) {
// subscribing to default ns
handlerWrapper.namespaces.add(getDefaultNs());
emit(e, delayed) {
if (isActive()) {
if (isActive()) {
queue.push(e);
if (!delayed) {
solveQueue();
}
return result;
},
};
return result;
else {
setTimeout(solveQueue, 0);
}
}
}
},

@@ -202,3 +156,3 @@ });

function Events(getFallbackNs, getDefaultNs) {
function Events() {
let emitterActive = true;

@@ -209,12 +163,12 @@ function isActive() {

const self = Object.freeze({
onPendingLanguageChange: EventEmitter(isActive),
onLanguageChange: EventEmitter(isActive),
onLoadingChange: EventEmitter(isActive),
onFetchingChange: EventEmitter(isActive),
onInitialLoaded: EventEmitter(isActive),
onRunningChange: EventEmitter(isActive),
onCacheChange: EventEmitter(isActive),
onUpdate: EventEmitterSelective(isActive, getFallbackNs, getDefaultNs),
onPermanentChange: EventEmitter(isActive),
onError: EventEmitter(isActive),
onPendingLanguageChange: EventEmitter('pendingLanguage', isActive),
onLanguageChange: EventEmitter('language', isActive),
onLoadingChange: EventEmitter('loading', isActive),
onFetchingChange: EventEmitter('fetching', isActive),
onInitialLoaded: EventEmitter('initialLoad', isActive),
onRunningChange: EventEmitter('running', isActive),
onCacheChange: EventEmitter('cache', isActive),
onPermanentChange: EventEmitter('permanentChange', isActive),
onError: EventEmitter('error', isActive),
onUpdate: EventEmitterCombined(isActive),
setEmitterActive(active) {

@@ -248,5 +202,5 @@ emitterActive = active;

});
self.onInitialLoaded.listen(() => self.onUpdate.emit());
self.onLanguageChange.listen(() => self.onUpdate.emit());
self.onCacheChange.listen(({ value }) => self.onUpdate.emit([value.namespace], true));
self.onInitialLoaded.listen((e) => self.onUpdate.emit(e, false));
self.onLanguageChange.listen((e) => self.onUpdate.emit(e, false));
self.onCacheChange.listen((e) => self.onUpdate.emit(e, true));
return self;

@@ -281,3 +235,3 @@ }

const flattenTranslations = (data) => {
const flattenTranslationsToMap = (data) => {
const result = new Map();

@@ -290,3 +244,3 @@ Object.entries(data).forEach(([key, value]) => {

if (typeof value === 'object') {
flattenTranslations(value).forEach((flatValue, flatKey) => {
flattenTranslationsToMap(value).forEach((flatValue, flatKey) => {
result.set(key + '.' + flatKey, flatValue);

@@ -300,2 +254,5 @@ });

};
const flattenTranslations = (data) => {
return Object.fromEntries(flattenTranslationsToMap(data).entries());
};
const decodeCacheKey = (key) => {

@@ -316,3 +273,3 @@ const [firstPart, ...rest] = key.split(':');

function Cache(events, backendGetRecord, backendGetDevRecord, withDefaultNs, isInitialLoading, fetchingObserver, loadingObserver) {
function Cache(events, backendGetRecord, backendGetDevRecord, withDefaultNs, isInitialLoading, isCacheDisabled, fetchingObserver, loadingObserver) {
const asyncRequests = new Map();

@@ -328,3 +285,3 @@ const cache = new Map();

});
events.onCacheChange.emit(descriptor);
events.onCacheChange.emit(decodeCacheKey(cacheKey));
}

@@ -382,3 +339,12 @@ /**

addStaticData(data) {
if (data) {
if (Array.isArray(data)) {
for (const record of data) {
const key = encodeCacheKey(record);
const existing = cache.get(key);
if (!existing || existing.version === 0) {
addRecordInternal(record, flattenTranslations(record.data), 0);
}
}
}
else if (data) {
staticData = Object.assign(Object.assign({}, staticData), data);

@@ -390,3 +356,3 @@ Object.entries(data).forEach(([key, value]) => {

if (!existing || existing.version === 0) {
addRecordInternal(descriptor, value, 0);
addRecordInternal(descriptor, flattenTranslations(value), 0);
}

@@ -402,3 +368,3 @@ }

addRecord(descriptor, data) {
addRecordInternal(descriptor, data, version);
addRecordInternal(descriptor, flattenTranslations(data), version);
},

@@ -413,8 +379,17 @@ exists(descriptor, strict = false) {

getRecord(descriptor) {
var _a;
return (_a = cache.get(encodeCacheKey(withDefaultNs(descriptor)))) === null || _a === void 0 ? void 0 : _a.data;
const descriptorWithNs = withDefaultNs(descriptor);
const cacheKey = encodeCacheKey(descriptorWithNs);
const cacheRecord = cache.get(cacheKey);
if (!cacheRecord) {
return undefined;
}
return Object.assign(Object.assign({}, descriptorWithNs), { cacheKey, data: cacheRecord.data });
},
getAllRecords() {
const entries = Array.from(cache.entries());
return entries.map(([key]) => self.getRecord(decodeCacheKey(key)));
},
getTranslation(descriptor, key) {
var _a;
return (_a = cache.get(encodeCacheKey(descriptor))) === null || _a === void 0 ? void 0 : _a.data.get(key);
return (_a = cache.get(encodeCacheKey(descriptor))) === null || _a === void 0 ? void 0 : _a.data[key];
},

@@ -425,4 +400,3 @@ getTranslationNs(namespaces, languages, key) {

for (const language of languages) {
const value = (_a = cache
.get(encodeCacheKey({ language, namespace }))) === null || _a === void 0 ? void 0 : _a.data.get(key);
const value = (_a = cache.get(encodeCacheKey({ language, namespace }))) === null || _a === void 0 ? void 0 : _a.data[key];
if (value !== undefined && value !== null) {

@@ -439,4 +413,3 @@ return [namespace];

for (const language of languages) {
const value = (_a = cache
.get(encodeCacheKey({ language, namespace }))) === null || _a === void 0 ? void 0 : _a.data.get(key);
const value = (_a = cache.get(encodeCacheKey({ language, namespace }))) === null || _a === void 0 ? void 0 : _a.data[key];
if (value !== undefined && value !== null) {

@@ -452,4 +425,6 @@ return value;

const record = (_a = cache.get(encodeCacheKey(descriptor))) === null || _a === void 0 ? void 0 : _a.data;
record === null || record === void 0 ? void 0 : record.set(key, value);
events.onCacheChange.emit(Object.assign(Object.assign({}, descriptor), { key }));
if (record === null || record === void 0 ? void 0 : record[key]) {
record[key] = value;
events.onCacheChange.emit(Object.assign(Object.assign({}, descriptor), { key }));
}
},

@@ -468,51 +443,57 @@ isFetching(ns) {

const namespaces = getFallbackArray(ns);
return Boolean(isInitialLoading() ||
Array.from(asyncRequests.keys()).find((key) => {
const descriptor = decodeCacheKey(key);
return ((!namespaces.length ||
namespaces.includes(descriptor.namespace)) &&
!self.exists({
namespace: descriptor.namespace,
language: language,
}));
}));
if (isInitialLoading()) {
return true;
}
const pendingCacheKeys = Array.from(asyncRequests.keys());
return Boolean(pendingCacheKeys.find((key) => {
const descriptor = decodeCacheKey(key);
return ((!namespaces.length || namespaces.includes(descriptor.namespace)) &&
!self.exists({
namespace: descriptor.namespace,
language: language,
}));
}));
},
async loadRecords(descriptors, isDev) {
async loadRecords(descriptors, options) {
const withPromises = descriptors.map((descriptor) => {
const keyObject = withDefaultNs(descriptor);
const cacheKey = encodeCacheKey(keyObject);
if (options === null || options === void 0 ? void 0 : options.useCache) {
const exists = self.exists(keyObject, true);
if (exists) {
return Object.assign(Object.assign({}, keyObject), { new: false, cacheKey, data: self.getRecord(keyObject).data });
}
}
const existingPromise = asyncRequests.get(cacheKey);
if (existingPromise) {
return {
new: false,
promise: existingPromise,
keyObject,
cacheKey,
};
return Object.assign(Object.assign({}, keyObject), { new: false, promise: existingPromise, cacheKey });
}
const dataPromise = fetchData(keyObject, isDev) || Promise.resolve(undefined);
const dataPromise = fetchData(keyObject, !(options === null || options === void 0 ? void 0 : options.noDev)) || Promise.resolve(undefined);
asyncRequests.set(cacheKey, dataPromise);
return {
new: true,
promise: dataPromise,
keyObject,
cacheKey,
};
return Object.assign(Object.assign({}, keyObject), { new: true, promise: dataPromise, cacheKey });
});
fetchingObserver.notify();
loadingObserver.notify();
const results = await Promise.all(withPromises.map((val) => val.promise));
withPromises.forEach((value, i) => {
const promiseChanged = asyncRequests.get(value.cacheKey) !== value.promise;
const promisesToWait = withPromises
.map((val) => val.promise)
.filter(Boolean);
const fetchedData = await Promise.all(promisesToWait);
withPromises.forEach((value) => {
var _a;
if (value.promise) {
value.data = flattenTranslations((_a = fetchedData[0]) !== null && _a !== void 0 ? _a : {});
fetchedData.shift();
}
// if promise has changed in between, it means cache been invalidated or
// new data are being fetched
const promiseChanged = asyncRequests.get(value.cacheKey) !== value.promise;
if (value.new && !promiseChanged) {
asyncRequests.delete(value.cacheKey);
const data = results[i];
if (data) {
self.addRecord(value.keyObject, data);
if (value.data) {
self.addRecord(value, value.data);
}
else if (!self.getRecord(value.keyObject)) {
else if (!self.getRecord(value)) {
// if no data exist, put empty object
self.addRecord(value.keyObject, {});
// so we know we don't have to fetch again
self.addRecord(value, {});
}

@@ -523,8 +504,10 @@ }

loadingObserver.notify();
return withPromises.map((val) => self.getRecord(val.keyObject));
},
getAllRecords() {
const entries = Array.from(cache.entries());
return entries.map(([key, entry]) => {
return Object.assign(Object.assign({}, decodeCacheKey(key)), { data: entry.data });
return withPromises.map((val) => {
var _a;
return ({
language: val.language,
namespace: val.namespace,
data: (_a = val.data) !== null && _a !== void 0 ? _a : {},
cacheKey: val.cacheKey,
});
});

@@ -584,3 +567,2 @@ },

const defaultValues = {
defaultNs: '',
observerOptions: defaultObserverOptions,

@@ -590,4 +572,6 @@ observerType: 'invisible',

apiUrl: DEFAULT_API_URL,
autoLoadRequiredData: true,
fetch: createFetchFunction(),
onTranslationMissing: DEFAULT_MISSING_TRANSLATION,
disableCache: false,
};

@@ -786,2 +770,5 @@ const combineOptions = (...states) => {

const { apiKey, apiUrl, projectId, filterTag } = getInitialOptions();
if (!apiKey || !apiUrl || !self.hasDevBackend()) {
return undefined;
}
return (_a = instances.devBackend) === null || _a === void 0 ? void 0 : _a.getRecord(Object.assign({ apiKey,

@@ -930,2 +917,5 @@ apiUrl,

},
isCacheDisabled() {
return state.initialOptions.disableCache;
},
setInitialLoading(value) {

@@ -981,3 +971,4 @@ state.isInitialLoading = value;

return unique([
...(state.initialOptions.ns || [state.initialOptions.defaultNs]),
self.getDefaultNs(),
...(state.initialOptions.ns || []),
...getFallbackArray(state.initialOptions.fallbackNs),

@@ -1000,4 +991,13 @@ ...state.activeNamespaces.keys(),

},
getNs() {
var _a, _b;
return ((_a = state.initialOptions.ns) === null || _a === void 0 ? void 0 : _a.length)
? state.initialOptions.ns
: [(_b = state.initialOptions.defaultNs) !== null && _b !== void 0 ? _b : ''];
},
getDefaultNs(ns) {
return ns === undefined ? state.initialOptions.defaultNs : ns;
var _a, _b, _c;
return ns === undefined
? (_c = (_a = state.initialOptions.defaultNs) !== null && _a !== void 0 ? _a : (_b = state.initialOptions.ns) === null || _b === void 0 ? void 0 : _b[0]) !== null && _c !== void 0 ? _c : ''
: ns;
},

@@ -1013,6 +1013,9 @@ getAvailableLanguages() {

},
getAvailableNs() {
return state.initialOptions.availableNs;
},
withDefaultNs(descriptor) {
return {
namespace: descriptor.namespace === undefined
? self.getInitialOptions().defaultNs
? self.getDefaultNs()
: descriptor.namespace,

@@ -1067,3 +1070,3 @@ language: descriptor.language,

function Controller({ options }) {
const events = Events(getFallbackNs, getDefaultNs);
const events = Events();
const fetchingObserver = ValueObserver(false, () => cache.isFetching(), events.onFetchingChange.emit);

@@ -1073,3 +1076,3 @@ const loadingObserver = ValueObserver(false, () => self.isLoading(), events.onLoadingChange.emit);

const pluginService = Plugins(state.getLanguage, state.getInitialOptions, state.getAvailableLanguages, getDefaultAndFallbackNs, getTranslationNs, getTranslation, changeTranslation, events);
const cache = Cache(events, pluginService.getBackendRecord, pluginService.getBackendDevRecord, state.withDefaultNs, state.isInitialLoading, fetchingObserver, loadingObserver);
const cache = Cache(events, pluginService.getBackendRecord, pluginService.getBackendDevRecord, state.withDefaultNs, state.isInitialLoading, state.isCacheDisabled, fetchingObserver, loadingObserver);
if (options) {

@@ -1093,3 +1096,3 @@ init(options);

function getDefaultAndFallbackNs(ns) {
return [...getFallbackArray(getDefaultNs(ns)), ...getFallbackNs()];
return unique([...getFallbackArray(getDefaultNs(ns)), ...getFallbackNs()]);
}

@@ -1099,6 +1102,6 @@ // gets all namespaces which need to be loaded

function getRequiredNamespaces(ns) {
return [
return unique([
...getFallbackArray(ns !== null && ns !== void 0 ? ns : getDefaultNs()),
...state.getRequiredNamespaces(),
];
]);
}

@@ -1119,3 +1122,3 @@ function changeTranslation(descriptor, key, value) {

}
function getRequiredRecords(lang, ns) {
function getRequiredDescriptors(lang, ns) {
const languages = state.getFallbackLangs(lang);

@@ -1126,5 +1129,3 @@ const namespaces = getRequiredNamespaces(ns);

namespaces.forEach((namespace) => {
if (!cache.exists({ language, namespace }, true)) {
result.push({ language, namespace });
}
result.push({ language, namespace });
});

@@ -1134,7 +1135,35 @@ });

}
function loadRequiredRecords(lang, ns) {
const descriptors = getRequiredRecords(lang, ns);
if (descriptors.length) {
return valueOrPromise(self.loadRecords(descriptors), () => { });
function getMissingDescriptors(lang, ns) {
return getRequiredDescriptors(lang, ns).filter((descriptor) => !cache.exists(descriptor, true));
}
function getMatrixRecords(options) {
let languages = [];
let namespaces = [];
if (Array.isArray(options.languages)) {
languages = options.languages;
}
else if (options.languages === 'all') {
const availableLanguages = self.getAvailableLanguages();
if (!availableLanguages) {
throw new Error(missingOptionError('availableLanguages'));
}
languages = availableLanguages;
}
if (Array.isArray(options.namespaces)) {
namespaces = options.namespaces;
}
else if (options.namespaces === 'all') {
const availableNs = self.getAvailableNs();
if (!availableNs) {
throw new Error(missingOptionError('availableNs'));
}
namespaces = availableNs;
}
const records = [];
languages.forEach((language) => {
namespaces.forEach((namespace) => {
records.push({ language, namespace });
});
});
return records;
}

@@ -1153,4 +1182,7 @@ function getTranslationNs({ key, ns }) {

const data = valueOrPromise(initializeLanguage(), () => {
// fail if there is no language
return loadRequiredRecords();
const missingDescriptors = getMissingDescriptors();
if (missingDescriptors.length &&
state.getInitialOptions().autoLoadRequiredData) {
return cache.loadRecords(missingDescriptors, { useCache: true });
}
});

@@ -1196,3 +1228,3 @@ if (isPromise(data)) {

}
const self = Object.freeze(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign({}, events), state), pluginService), cache), { init: init, getTranslation: getTranslation, changeTranslation: changeTranslation, getTranslationNs: getTranslationNs, getDefaultAndFallbackNs: getDefaultAndFallbackNs, findPositions: pluginService.findPositions, getRequiredRecords: getRequiredRecords, async changeLanguage(language) {
const self = Object.freeze(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign({}, events), state), pluginService), cache), { init: init, getTranslation: getTranslation, changeTranslation: changeTranslation, getTranslationNs: getTranslationNs, getDefaultAndFallbackNs: getDefaultAndFallbackNs, findPositions: pluginService.findPositions, getRequiredDescriptors: getRequiredDescriptors, async changeLanguage(language) {
if (state.getPendingLanguage() === language &&

@@ -1203,4 +1235,6 @@ state.getLanguage() === language) {

state.setPendingLanguage(language);
if (state.isRunning()) {
await loadRequiredRecords(language);
if (state.isRunning() && state.getInitialOptions().autoLoadRequiredData) {
await cache.loadRecords(getRequiredDescriptors(language), {
useCache: true,
});
}

@@ -1219,11 +1253,11 @@ if (language === state.getPendingLanguage()) {

if (state.isRunning()) {
await loadRequiredRecords(undefined, ns);
await cache.loadRecords(getRequiredDescriptors(undefined, ns), {
useCache: true,
});
}
},
loadRecords(descriptors) {
return cache.loadRecords(descriptors, self.isDev());
async loadRecord(descriptor, options) {
var _a;
return (_a = (await self.loadRecords([descriptor], options))[0]) === null || _a === void 0 ? void 0 : _a.data;
},
async loadRecord(descriptor) {
return (await self.loadRecords([descriptor]))[0];
},
isLoading(ns) {

@@ -1256,2 +1290,13 @@ return cache.isLoading(state.getLanguage(), ns);

},
async loadRequired(options) {
if (!(options === null || options === void 0 ? void 0 : options.language)) {
await initializeLanguage();
}
const requiredRecords = getRequiredDescriptors(options === null || options === void 0 ? void 0 : options.language);
return self.loadRecords(requiredRecords, options);
},
async loadMatrix(options) {
const records = getMatrixRecords(options);
return self.loadRecords(records, options);
},
run() {

@@ -1298,16 +1343,2 @@ checkCorrectConfiguration();

/**
* Listen for specific namespaces changes.
*
* ```
* const sub = tolgee.onUpdate(handler)
*
* // subscribe to selected namespace
* sub.subscribeNs(['common'])
*
* // unsubscribe
* sub.unsubscribe()
* ```
*/
onNsUpdate: controller.onUpdate.listenSome,
/**
* Turn off/on events emitting. Is on by default.

@@ -1350,2 +1381,12 @@ */

/**
* Load records which would be loaded by `run` function
*
* You can provide language if not previously set on tolgee instance
*/
loadRequired: controller.loadRequired,
/**
* Load records in matrix (languages x namespaces)
*/
loadMatrix: controller.loadMatrix,
/**
* Manually load multiple records from `Backend` (or `DevBackend` when in dev mode)

@@ -1378,5 +1419,5 @@ *

/**
* Returns records needed for instance to be `loaded`
* Returns descriptors of records needed for instance to be `loaded`
*/
getRequiredRecords: controller.getRequiredRecords,
getRequiredDescriptors: controller.getRequiredDescriptors,
/**

@@ -1383,0 +1424,0 @@ * @return `true` if tolgee is loading initial data (triggered by `run`).

@@ -1,2 +0,2 @@

function e(e){return Boolean(e&&"function"==typeof e.then)}function n(n,t){return e(n)?Promise.resolve(n).then(t):t(n)}function t(n,t,a){function i(e){const a=t(e);throw n.emit(a),console.error(a),a}try{const n=a();return e(n)?n.catch(i):n}catch(e){i(e)}}function a(e){const n=(Array.isArray(e)?e:[e]).map((e=>`'${e}'`)),t=n.slice(-2).join(" or ");return`Tolgee: You need to specify ${[...n.slice(0,-2),t].join(", ")} option`}function i(e){return"string"==typeof e?[e]:Array.isArray(e)?e:void 0}function o(e){return i(e)||[]}function r(e,n){return"object"!=typeof(t=n)||Array.isArray(t)||null===t?o(n):o(null==n?void 0:n[e]);var t}function s(e){return Array.from(new Set(e))}function g(e){return e?e.replace(/\/+$/,""):e}const c=(e,n)=>fetch(e,n);const l=(e=c)=>(n,t)=>{let a=(i=null==t?void 0:t.headers,Object.fromEntries(new Headers(i).entries()));var i;return a["x-api-key"]&&(a=Object.assign({"x-tolgee-sdk-type":"JS","x-tolgee-sdk-version":"prerelease"},a)),e(n,Object.assign(Object.assign({},t),{headers:a}))};function u(e){let n=[];return Object.freeze({listen(e){const t=n=>{e(n)};return n.push(t),{unsubscribe(){n=n.filter((e=>t!==e))}}},emit(t){e()&&n.forEach((e=>e({value:t})))}})}function d(e,n,t){const a=new Set,i=new Set;let r=[];function s(){if(0===r.length)return;const e=r;r=[],a.forEach((e=>{e({value:void 0})}));let t=new Set;e.forEach((e=>{void 0===e?t=void 0:void 0!==t&&e.forEach((e=>t.add(e)))}));!function(e){const t=new Set(n());i.forEach((n=>{(void 0===e||-1!==(null==e?void 0:e.findIndex((e=>t.has(e)||n.namespaces.has(e)))))&&n.fn({value:void 0})}))}(t?Array.from(t.keys()):void 0)}return Object.freeze({emit(n,t){e()&&(r.push(n),t?setTimeout(s,0):s())},listen(e){a.add(e);return{unsubscribe:()=>{a.delete(e)}}},listenSome(e){const n={fn:n=>{e(n)},namespaces:new Set};i.add(n);const a={unsubscribe:()=>{i.delete(n)},subscribeNs:e=>(o(e).forEach((e=>n.namespaces.add(e))),void 0===e&&n.namespaces.add(t()),a)};return a}})}class f extends Error{constructor(e,n,t=!1){const{language:a,namespace:i}=e;super(`Tolgee: Failed to fetch record for "${a}"${i&&` and "${i}"`}`),this.cause=n,this.isDev=t,this.name="RecordFetchError",this.language=a,this.namespace=i}}class p extends Error{constructor(e,n){super(e),this.cause=n,this.name="LanguageDetectorError"}}class v extends Error{constructor(e,n){super(e),this.cause=n,this.name="LanguageStorageError"}}const h=e=>{const n=new Map;return Object.entries(e).forEach((([e,t])=>{null!=t&&("object"!=typeof t?n.set(e,t):h(t).forEach(((t,a)=>{n.set(e+"."+a,t)})))})),n},m=e=>{const[n,...t]=e.split(":");return{language:n,namespace:t.join(":")||""}},b=({language:e,namespace:n})=>n?`${e}:${n}`:e;function y(n,t,a,i,r,g,c){const l=new Map,u=new Map;let d={},p=0;function v(e,t,a){const i=b(e);u.set(i,{data:h(t),version:a}),n.onCacheChange.emit(e)}async function y(i,o){let r;if(o)try{r=await a(i)}catch(e){const t=new f(i,e,!0);n.onError.emit(t),console.warn(t)}return r||(r=await async function(a){function i(e){const t=new f(a,e);throw n.onError.emit(t),console.error(t),t}const o=t(a);if(e(o)){const e=await o.catch(i);if(void 0!==e)return e}const r=d[b(a)];if("function"!=typeof r)return r;try{return await r()}catch(e){i(e)}}(i)),r}const O=Object.freeze({addStaticData(e){e&&(d=Object.assign(Object.assign({},d),e),Object.entries(e).forEach((([e,n])=>{if("function"!=typeof n){const t=m(e),a=u.get(e);a&&0!==a.version||v(t,n,0)}})))},invalidate(){l.clear(),p+=1},addRecord(e,n){v(e,n,p)},exists(e,n=!1){const t=u.get(b(e));return t&&n?t.version===p:Boolean(t)},getRecord(e){var n;return null===(n=u.get(b(i(e))))||void 0===n?void 0:n.data},getTranslation(e,n){var t;return null===(t=u.get(b(e)))||void 0===t?void 0:t.data.get(n)},getTranslationNs(e,n,t){var a;for(const i of e)for(const e of n){const n=null===(a=u.get(b({language:e,namespace:i})))||void 0===a?void 0:a.data.get(t);if(null!=n)return[i]}return s(e)},getTranslationFallback(e,n,t){var a;for(const i of e)for(const e of n){const n=null===(a=u.get(b({language:e,namespace:i})))||void 0===a?void 0:a.data.get(t);if(null!=n)return n}},changeTranslation(e,t,a){var i;const o=null===(i=u.get(b(e)))||void 0===i?void 0:i.data;null==o||o.set(t,a),n.onCacheChange.emit(Object.assign(Object.assign({},e),{key:t}))},isFetching(e){if(r())return!0;if(void 0===e)return l.size>0;const n=o(e);return Boolean(Array.from(l.keys()).find((e=>n.includes(m(e).namespace))))},isLoading(e,n){const t=o(n);return Boolean(r()||Array.from(l.keys()).find((n=>{const a=m(n);return(!t.length||t.includes(a.namespace))&&!O.exists({namespace:a.namespace,language:e})})))},async loadRecords(e,n){const t=e.map((e=>{const t=i(e),a=b(t),o=l.get(a);if(o)return{new:!1,promise:o,keyObject:t,cacheKey:a};const r=y(t,n)||Promise.resolve(void 0);return l.set(a,r),{new:!0,promise:r,keyObject:t,cacheKey:a}}));g.notify(),c.notify();const a=await Promise.all(t.map((e=>e.promise)));return t.forEach(((e,n)=>{const t=l.get(e.cacheKey)!==e.promise;if(e.new&&!t){l.delete(e.cacheKey);const t=a[n];t?O.addRecord(e.keyObject,t):O.getRecord(e.keyObject)||O.addRecord(e.keyObject,{})}})),g.notify(),c.notify(),t.map((e=>O.getRecord(e.keyObject)))},getAllRecords:()=>Array.from(u.entries()).map((([e,n])=>Object.assign(Object.assign({},m(e)),{data:n.data})))});return O}function O(e,n){var t={};for(var a in e)Object.prototype.hasOwnProperty.call(e,a)&&n.indexOf(a)<0&&(t[a]=e[a]);if(null!=e&&"function"==typeof Object.getOwnPropertySymbols){var i=0;for(a=Object.getOwnPropertySymbols(e);i<a.length;i++)n.indexOf(a[i])<0&&Object.prototype.propertyIsEnumerable.call(e,a[i])&&(t[a[i]]=e[a[i]])}return t}const L="invalid",j={defaultNs:"",observerOptions:{tagAttributes:{textarea:["placeholder"],input:["value","placeholder"],img:["alt"],"*":["aria-label","title"]},restrictedElements:["script","style"],highlightKeys:["Alt"],highlightColor:"rgb(255, 0, 0)",highlightWidth:5,inputPrefix:"%-%tolgee:",inputSuffix:"%-%",passToParent:["option","optgroup"],fullKeyEncode:!1},observerType:"invisible",onFormatError:L,apiUrl:"https://app.tolgee.io",fetch:l(),onTranslationMissing:({key:e})=>e},k=(...e)=>{let n={};return e.forEach((e=>{n=Object.assign(Object.assign(Object.assign({},n),e),{observerOptions:Object.assign(Object.assign({},n.observerOptions),null==e?void 0:e.observerOptions)})})),n};function w(e,n){const t=k(j,null==n?void 0:n.initialOptions,e);return t.apiUrl=g(t.apiUrl),(null==e?void 0:e.fetch)&&(t.fetch=l(e.fetch)),{initialOptions:t,activeNamespaces:(null==n?void 0:n.activeNamespaces)||new Map,language:null==n?void 0:n.language,pendingLanguage:null==n?void 0:n.language,isInitialLoading:!1,isRunning:!1}}function R(e,a,i,o,r,s,g,c){const l={ui:void 0},u={formatters:[],finalFormatter:void 0,observer:void 0,devBackend:void 0,backends:[],ui:void 0,languageDetector:void 0,languageStorage:void 0},d=async({keysAndDefaults:e,target:n})=>{var t;const a=e.map((({key:e,ns:n,defaultValue:t})=>({key:e,defaultValue:t,fallbackNamespaces:o(n),namespace:r({key:e,ns:n})[0],translation:s({key:e,ns:n})})));null===(t=u.ui)||void 0===t||t.handleElementClick(a,n)},f=(e,n)=>{var t;return(null===(t=u.observer)||void 0===t?void 0:t.findPositions(e,n))||[]};function h(e){const n=s({key:e.key,ns:e.ns});return F.formatTranslation(Object.assign(Object.assign({},e),{translation:n,formatEnabled:!0}))}function m(){return{fetch:a().fetch}}function b(e){u.observer=null==e?void 0:e()}function y(){return Boolean(u.observer)}function j(e){e&&u.formatters.push(e)}function k(e){u.finalFormatter=e}function w(e){l.ui=e}function R(){return Boolean(l.ui)}function E(e){u.languageStorage=e}function N(e){u.languageDetector=e}function C(e){e&&u.backends.push(e)}function D(e){u.devBackend=e}const F=Object.freeze({addPlugin:function(e,n){n(e,Object.freeze({setFinalFormatter:k,addFormatter:j,setObserver:b,hasObserver:y,setUi:w,hasUi:R,setDevBackend:D,addBackend:C,setLanguageDetector:N,setLanguageStorage:E}))},findPositions:f,run(){var e,n;const{apiKey:t,apiUrl:i,projectId:o,observerOptions:r,tagNewKeys:s,filterTag:p}=a();u.ui=null===(e=l.ui)||void 0===e?void 0:e.call(l,{apiKey:t,apiUrl:i,projectId:o,highlight:F.highlight,changeTranslation:g,findPositions:f,onPermanentChange:e=>c.onPermanentChange.emit(e),tagNewKeys:s,filterTag:p}),null===(n=u.observer)||void 0===n||n.run({mouseHighlight:!0,options:r,translate:h,onClick:d})},stop(){var e;u.ui=void 0,null===(e=u.observer)||void 0===e||e.stop()},getLanguageStorage:()=>u.languageStorage,getInitialLanguage(){const e=i();return n(t(c.onError,(e=>new v("Tolgee: Failed to load language",e)),(()=>{var e;return null===(e=u.languageStorage)||void 0===e?void 0:e.getLanguage(m())})),(n=>e&&!e.includes(n)||!n?function(){if(!u.languageDetector)return;const e=i();return t(c.onError,(e=>new p("Tolgee: Failed to detect language",e)),(()=>{var n;return null===(n=u.languageDetector)||void 0===n?void 0:n.getLanguage(Object.assign({availableLanguages:e},m()))}))}():n))},setStoredLanguage:e=>t(c.onError,(e=>new v("Tolgee: Failed to store language",e)),(()=>{var n;return null===(n=u.languageStorage)||void 0===n?void 0:n.setLanguage(e,m())})),getDevBackend:()=>u.devBackend,getBackendRecord:async({language:e,namespace:n})=>{for(const t of u.backends){const a=await t.getRecord(Object.assign({language:e,namespace:n},m()));if(void 0!==a)return a}},getBackendDevRecord:async({language:e,namespace:n})=>{var t;const{apiKey:i,apiUrl:o,projectId:r,filterTag:s}=a();return null===(t=u.devBackend)||void 0===t?void 0:t.getRecord(Object.assign({apiKey:i,apiUrl:o,projectId:r,language:e,namespace:n,filterTag:s},m()))},getLanguageDetector:()=>u.languageDetector,retranslate(){var e;null===(e=u.observer)||void 0===e||e.retranslate()},highlight:(e,n)=>{var t,a;return(null===(a=null===(t=u.observer)||void 0===t?void 0:t.highlight)||void 0===a?void 0:a.call(t,e,n))||{unhighlight(){}}},unwrap(e){var n;return u.observer?null===(n=u.observer)||void 0===n?void 0:n.unwrap(e):{text:e,keys:[]}},wrap(e){var n;return u.observer?null===(n=u.observer)||void 0===n?void 0:n.wrap(e):e.translation},hasDevBackend:()=>Boolean(F.getDevBackend()),formatTranslation(n){var t,{formatEnabled:i}=n,o=O(n,["formatEnabled"]);const{key:r,translation:s,defaultValue:g,noWrap:c,params:l,ns:d,orEmpty:f}=o,p=null!=s?s:g;let v="";null==s&&(v=a().onTranslationMissing(o));let h=null!=p?p:f?"":v;const m=e(),b=i||!(null===(t=u.observer)||void 0===t?void 0:t.outputNotFormattable),y=e=>u.observer&&!c?u.observer.wrap({key:r,translation:e,defaultValue:g,params:l,ns:d}):e;h=y(h);try{if(p&&m&&b)for(const e of u.formatters)h=e.format({translation:h,language:m,params:l});u.finalFormatter&&p&&m&&b&&(h=u.finalFormatter.format({translation:h,language:m,params:l}))}catch(e){console.error(e);const n=("string"==typeof(j=e)?j:"string"==typeof(null==j?void 0:j.message)?j.message:void 0)||L,t=a().onFormatError,i=typeof t;h="string"===i?t:"function"===i?t(n,o):L,h=y(h)}var j;return h}});return F}const E=(e,n,t)=>{let a=e;return Object.freeze({init(e){a=e},notify(){const e=n();a!==e&&t(e),a=e}})};const N=(e,...n)=>{let t,a={};return"object"==typeof e?a=e:(a.key=e,"string"==typeof n[0]?(a.defaultValue=n[0],t=n[1]):"object"==typeof n[0]&&(t=n[0])),t&&(a=Object.assign(Object.assign({},function(e){var{ns:n,noWrap:t,orEmpty:a,params:i,language:o}=e,r=O(e,["ns","noWrap","orEmpty","params","language"]);const s={ns:n,noWrap:t,orEmpty:a,language:o};return Object.assign(Object.assign({},s),{params:Object.assign({},r)})}(t)),a)),a};function C({options:t}){const i=function(e,n){let t=!0;function a(){return t}const i=Object.freeze({onPendingLanguageChange:u(a),onLanguageChange:u(a),onLoadingChange:u(a),onFetchingChange:u(a),onInitialLoaded:u(a),onRunningChange:u(a),onCacheChange:u(a),onUpdate:d(a,e,n),onPermanentChange:u(a),onError:u(a),setEmitterActive(e){t=e},on:(e,n)=>{switch(e){case"pendingLanguage":return i.onPendingLanguageChange.listen(n);case"language":return i.onLanguageChange.listen(n);case"loading":return i.onLoadingChange.listen(n);case"fetching":return i.onFetchingChange.listen(n);case"initialLoad":return i.onInitialLoaded.listen(n);case"running":return i.onRunningChange.listen(n);case"cache":return i.onCacheChange.listen(n);case"update":return i.onUpdate.listen(n);case"permanentChange":return i.onPermanentChange.listen(n);case"error":return i.onError.listen(n)}}});return i.onInitialLoaded.listen((()=>i.onUpdate.emit())),i.onLanguageChange.listen((()=>i.onUpdate.emit())),i.onCacheChange.listen((({value:e})=>i.onUpdate.emit([e.namespace],!0))),i}(b,O),c=E(!1,(()=>v.isFetching()),i.onFetchingChange.emit),l=E(!1,(()=>T.isLoading()),i.onLoadingChange.emit),f=function(e,n,t){let a,i=w();const c=Object.freeze({init(e){i=w(e,i)},isRunning:()=>i.isRunning,setRunning(e){i.isRunning!==e&&(i.isRunning=e,t.emit(e))},isInitialLoading:()=>i.isInitialLoading,setInitialLoading(e){i.isInitialLoading=e},getLanguage:()=>i.language||i.initialOptions.language,setLanguage(n){i.language!==n&&(i.language=n,e.emit(n))},getPendingLanguage:()=>i.pendingLanguage||c.getLanguage(),setPendingLanguage(e){i.pendingLanguage!==e&&(i.pendingLanguage=e,n.emit(e))},getInitialOptions:()=>Object.assign(Object.assign({},i.initialOptions),a),addActiveNs(e){o(e).forEach((e=>{const n=i.activeNamespaces.get(e);void 0!==n?i.activeNamespaces.set(e,n+1):i.activeNamespaces.set(e,1)}))},removeActiveNs(e){o(e).forEach((e=>{const n=i.activeNamespaces.get(e);void 0!==n&&n>1?i.activeNamespaces.set(e,n-1):i.activeNamespaces.delete(e)}))},getRequiredNamespaces:()=>s([...i.initialOptions.ns||[i.initialOptions.defaultNs],...o(i.initialOptions.fallbackNs),...i.activeNamespaces.keys()]),getFallbackLangs(e){const n=e||c.getLanguage();return n?s([n,...r(n,i.initialOptions.fallbackLanguage)]):[]},getFallbackNs:()=>o(i.initialOptions.fallbackNs),getDefaultNs:e=>void 0===e?i.initialOptions.defaultNs:e,getAvailableLanguages(){if(i.initialOptions.availableLanguages)return i.initialOptions.availableLanguages;if(i.initialOptions.staticData){const e=Object.keys(i.initialOptions.staticData).map((e=>m(e).language));return Array.from(new Set(e))}},withDefaultNs:e=>({namespace:void 0===e.namespace?c.getInitialOptions().defaultNs:e.namespace,language:e.language}),overrideCredentials(e){a=e?Object.assign(Object.assign({},e),{apiUrl:g(e.apiUrl)}):void 0}});return c}(i.onLanguageChange,i.onPendingLanguageChange,i.onRunningChange),p=R(f.getLanguage,f.getInitialOptions,f.getAvailableLanguages,L,I,P,k,i),v=y(i,p.getBackendRecord,p.getBackendDevRecord,f.withDefaultNs,f.isInitialLoading,c,l);let h;function b(){return f.getFallbackNs()}function O(e){return f.getDefaultNs(e)}function L(e){return[...o(O(e)),...b()]}function j(e){return[...o(null!=e?e:O()),...f.getRequiredNamespaces()]}function k(e,n,t){const a=f.withDefaultNs(e),i=v.getTranslation(a,n);return v.changeTranslation(a,n,t),{revert(){v.changeTranslation(a,n,i)}}}function C(e){f.init(e),v.addStaticData(f.getInitialOptions().staticData)}function D(e,n){const t=f.getFallbackLangs(e),a=j(n),i=[];return t.forEach((e=>{a.forEach((n=>{v.exists({language:e,namespace:n},!0)||i.push({language:e,namespace:n})}))})),i}function F(e,t){const a=D(e,t);if(a.length)return n(T.loadRecords(a),(()=>{}))}function I({key:e,ns:n}){const t=f.getFallbackLangs(),a=L(null!=n?n:void 0);return v.getTranslationNs(a,t,e)}function P({key:e,ns:n,language:t}){const a=L(null!=n?n:void 0),i=f.getFallbackLangs(t);return v.getTranslationFallback(a,i,e)}function A(){const t=n(function(){if(f.getLanguage())return;return n(p.getInitialLanguage(),(e=>{const n=e||f.getInitialOptions().defaultLanguage;n&&f.setLanguage(n)}))}(),(()=>F()));if(e(t))return f.setInitialLoading(!0),c.notify(),l.notify(),Promise.resolve(t).then((()=>{f.setInitialLoading(!1),c.notify(),l.notify(),i.onInitialLoaded.emit()}));i.onInitialLoaded.emit()}t&&C(t),i.onUpdate.listen((()=>{f.isRunning()&&p.retranslate()}));const T=Object.freeze(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign({},i),f),p),v),{init:C,getTranslation:P,changeTranslation:k,getTranslationNs:I,getDefaultAndFallbackNs:L,findPositions:p.findPositions,getRequiredRecords:D,async changeLanguage(e){f.getPendingLanguage()===e&&f.getLanguage()===e||(f.setPendingLanguage(e),f.isRunning()&&await F(e),e===f.getPendingLanguage()&&(f.setLanguage(e),await p.setStoredLanguage(e)))},async addActiveNs(e,n){n||f.addActiveNs(e),f.isRunning()&&await F(void 0,e)},loadRecords:e=>v.loadRecords(e,T.isDev()),loadRecord:async e=>(await T.loadRecords([e]))[0],isLoading:e=>v.isLoading(f.getLanguage(),e),isLoaded(e){const n=f.getLanguage();if(!n)return!1;const t=f.getFallbackLangs(n),a=j(e),i=[];return t.forEach((e=>{a.forEach((n=>{v.exists({language:e,namespace:n})||i.push({language:e,namespace:n})}))})),0===i.length},t:(...e)=>{const n=N(...e),t=P(n);return p.formatTranslation(Object.assign(Object.assign({},n),{translation:t}))},isDev:()=>Boolean(f.getInitialOptions().apiKey&&f.getInitialOptions().apiUrl),run:()=>(function(){if((p.getLanguageDetector()||p.getLanguageStorage())&&!f.getAvailableLanguages())throw new Error(a("availableLanguages"));if(!f.getLanguage()&&!f.getInitialOptions().defaultLanguage)throw new Error(a(["defaultLanguage","language"]))}(),f.isRunning()||(f.setRunning(!0),p.run(),h=A()),Promise.resolve(h)),stop(){f.isRunning()&&(p.stop(),f.setRunning(!1))}}));return T}const D=()=>{const e={plugins:[],options:{}},n=Object.freeze({use:t=>(e.plugins.push(t),n),updateDefaults:t=>(e.options=k(e.options,t),n),init(n){const t=function(e){const n=C({options:e});function t(e){const t=n.isRunning();t&&n.stop(),e(),n.isDev()&&n.invalidate(),t&&n.run()}n.isDev()&&n.invalidate();const a=Object.freeze({on:n.on,onNsUpdate:n.onUpdate.listenSome,setEmitterActive:n.setEmitterActive,getLanguage:n.getLanguage,getPendingLanguage:n.getPendingLanguage,changeLanguage:n.changeLanguage,changeTranslation:n.changeTranslation,addActiveNs:n.addActiveNs,removeActiveNs:n.removeActiveNs,loadRecords:n.loadRecords,loadRecord:n.loadRecord,addStaticData:n.addStaticData,getRecord:n.getRecord,getAllRecords:n.getAllRecords,isLoaded:n.isLoaded,getRequiredRecords:n.getRequiredRecords,isInitialLoading:n.isInitialLoading,isLoading:n.isLoading,isFetching:n.isFetching,isRunning:n.isRunning,run:n.run,stop:n.stop,t:n.t,highlight:n.highlight,findPositions:n.findPositions,getInitialOptions:n.getInitialOptions,isDev:n.isDev,wrap:n.wrap,unwrap:n.unwrap,overrideCredentials(e){t((()=>n.overrideCredentials(e)))},addPlugin(e){e&&t((()=>n.addPlugin(a,e)))},updateOptions(e){e&&t((()=>n.init(e)))}});return a}(k(e.options,n));return e.plugins.forEach(t.addPlugin),t}});return n},F=0,I=1,P=2;class A extends Error{constructor(e,n,t){let a,i="";e===F?a="Empty parameter":e===I?(a="Unexpected character",i="Did you forget to use FormatIcu to render ICU message syntax?"):a="Unexpected end",super(`Tolgee parser: ${a} at ${n} in "${t}"`+(i?"\n"+i:"")),this.code=e,this.index=n}}function T(e){return/\s/.test(e)}const S=0,x=1,U=2,B=3,z=4,K=new Set([U,x,S]),$="'",M=new Set(["{","}",$]),q=e=>/[0-9a-zA-Z_]/.test(e);function V(e,n){const[t,a]=function(e){let n=S,t="",a="",i="";const o=[],r=[];let s=0;function g(n){throw new A(n,s,e)}const c=()=>{o.push(t),t=""},l=()=>{""===a&&g(F),r.push(a),a=""};for(s=0;s<e.length;s++)switch(i=e[s],n){case S:i===$?(t+=i,n=x):"{"===i?(c(),n=B):(t+=i,n=S);break;case x:M.has(i)?(t=t.slice(0,-1)+i,n=U):(t+=i,n=S);break;case U:i===$?n=S:(t+=i,n=U);break;case B:"}"===i?(l(),n=S):T(i)?""!==a&&(l(),n=z):(q(i)||g(I),a+=i,n=B);break;case z:"}"==i?n=S:T(i)?n=z:g(I)}return K.has(n)||g(P),c(),[o,r]}(e),i=[t[0]];for(let o=1;o<t.length;o++){const r=null==n?void 0:n[a[o-1]];if(void 0===r)throw new Error(`Missing parameter "${a[o-1]}" in "${e}"`);i.push(String(r)),i.push(t[o])}return i.join("")}const W=()=>(e,n)=>(n.setFinalFormatter({format:({translation:e,params:n})=>V(e,n)}),e);export{W as FormatSimple,p as LanguageDetectorError,v as LanguageStorageError,f as RecordFetchError,D as TolgeeCore,l as createFetchFunction,i as getFallback,o as getFallbackArray,N as getTranslateProps};
function e(e){return Boolean(e&&"function"==typeof e.then)}function n(n,a){return e(n)?Promise.resolve(n).then(a):a(n)}function a(n,a,t){function i(e){const t=a(e);throw n.emit(t),console.error(t),t}try{const n=t();return e(n)?n.catch(i):n}catch(e){i(e)}}function t(e){const n=(Array.isArray(e)?e:[e]).map((e=>`'${e}'`)),a=n.slice(-2).join(" or ");return`Tolgee: You need to specify ${[...n.slice(0,-2),a].join(", ")} option`}function i(e){return"string"==typeof e?[e]:Array.isArray(e)?e:void 0}function o(e){return i(e)||[]}function r(e,n){return"object"!=typeof(a=n)||Array.isArray(a)||null===a?o(n):o(null==n?void 0:n[e]);var a}function s(e){return Array.from(new Set(e))}function g(e){return e?e.replace(/\/+$/,""):e}const l=(e,n)=>fetch(e,n);const c=(e=l)=>(n,a)=>{let t=(i=null==a?void 0:a.headers,Object.fromEntries(new Headers(i).entries()));var i;return t["x-api-key"]&&(t=Object.assign({"x-tolgee-sdk-type":"JS","x-tolgee-sdk-version":"prerelease"},t)),e(n,Object.assign(Object.assign({},a),{headers:t}))},u=(e,n)=>{let a=[];return{listen(e){const n=n=>{e(n)};return a.push(n),{unsubscribe(){a=a.filter((e=>n!==e))}}},emit(t){n()&&a.forEach((n=>n({type:e,value:t})))}}};function d(e){let n=[],a=[];function t(){if(0===a.length)return;const e=a;a=[],n.forEach((n=>{n(e)}))}return Object.freeze({listen(e){const a=n=>{e(n)};return n.push(a),{unsubscribe(){n=n.filter((e=>a!==e))}}},emit(n,i){e()&&e()&&(a.push(n),i?setTimeout(t,0):t())}})}class f extends Error{constructor(e,n,a=!1){const{language:t,namespace:i}=e;super(`Tolgee: Failed to fetch record for "${t}"${i&&` and "${i}"`}`),this.cause=n,this.isDev=a,this.name="RecordFetchError",this.language=t,this.namespace=i}}class p extends Error{constructor(e,n){super(e),this.cause=n,this.name="LanguageDetectorError"}}class v extends Error{constructor(e,n){super(e),this.cause=n,this.name="LanguageStorageError"}}const h=e=>{const n=new Map;return Object.entries(e).forEach((([e,a])=>{null!=a&&("object"!=typeof a?n.set(e,a):h(a).forEach(((a,t)=>{n.set(e+"."+t,a)})))})),n},m=e=>Object.fromEntries(h(e).entries()),b=e=>{const[n,...a]=e.split(":");return{language:n,namespace:a.join(":")||""}},y=({language:e,namespace:n})=>n?`${e}:${n}`:e;function O(n,a,t,i,r,g,l,c){const u=new Map,d=new Map;let p={},v=0;function h(e,a,t){const i=y(e);d.set(i,{data:m(a),version:t}),n.onCacheChange.emit(b(i))}async function O(i,o){let r;if(o)try{r=await t(i)}catch(e){const a=new f(i,e,!0);n.onError.emit(a),console.warn(a)}return r||(r=await async function(t){function i(e){const a=new f(t,e);throw n.onError.emit(a),console.error(a),a}const o=a(t);if(e(o)){const e=await o.catch(i);if(void 0!==e)return e}const r=p[y(t)];if("function"!=typeof r)return r;try{return await r()}catch(e){i(e)}}(i)),r}const L=Object.freeze({addStaticData(e){if(Array.isArray(e))for(const n of e){const e=y(n),a=d.get(e);a&&0!==a.version||h(n,m(n.data),0)}else e&&(p=Object.assign(Object.assign({},p),e),Object.entries(e).forEach((([e,n])=>{if("function"!=typeof n){const a=b(e),t=d.get(e);t&&0!==t.version||h(a,m(n),0)}})))},invalidate(){u.clear(),v+=1},addRecord(e,n){h(e,m(n),v)},exists(e,n=!1){const a=d.get(y(e));return a&&n?a.version===v:Boolean(a)},getRecord(e){const n=i(e),a=y(n),t=d.get(a);if(t)return Object.assign(Object.assign({},n),{cacheKey:a,data:t.data})},getAllRecords:()=>Array.from(d.entries()).map((([e])=>L.getRecord(b(e)))),getTranslation(e,n){var a;return null===(a=d.get(y(e)))||void 0===a?void 0:a.data[n]},getTranslationNs(e,n,a){var t;for(const i of e)for(const e of n){const n=null===(t=d.get(y({language:e,namespace:i})))||void 0===t?void 0:t.data[a];if(null!=n)return[i]}return s(e)},getTranslationFallback(e,n,a){var t;for(const i of e)for(const e of n){const n=null===(t=d.get(y({language:e,namespace:i})))||void 0===t?void 0:t.data[a];if(null!=n)return n}},changeTranslation(e,a,t){var i;const o=null===(i=d.get(y(e)))||void 0===i?void 0:i.data;(null==o?void 0:o[a])&&(o[a]=t,n.onCacheChange.emit(Object.assign(Object.assign({},e),{key:a})))},isFetching(e){if(r())return!0;if(void 0===e)return u.size>0;const n=o(e);return Boolean(Array.from(u.keys()).find((e=>n.includes(b(e).namespace))))},isLoading(e,n){const a=o(n);if(r())return!0;const t=Array.from(u.keys());return Boolean(t.find((n=>{const t=b(n);return(!a.length||a.includes(t.namespace))&&!L.exists({namespace:t.namespace,language:e})})))},async loadRecords(e,n){const a=e.map((e=>{const a=i(e),t=y(a);if(null==n?void 0:n.useCache){if(L.exists(a,!0))return Object.assign(Object.assign({},a),{new:!1,cacheKey:t,data:L.getRecord(a).data})}const o=u.get(t);if(o)return Object.assign(Object.assign({},a),{new:!1,promise:o,cacheKey:t});const r=O(a,!(null==n?void 0:n.noDev))||Promise.resolve(void 0);return u.set(t,r),Object.assign(Object.assign({},a),{new:!0,promise:r,cacheKey:t})}));l.notify(),c.notify();const t=a.map((e=>e.promise)).filter(Boolean),o=await Promise.all(t);return a.forEach((e=>{var n;e.promise&&(e.data=m(null!==(n=o[0])&&void 0!==n?n:{}),o.shift());const a=u.get(e.cacheKey)!==e.promise;e.new&&!a&&(u.delete(e.cacheKey),e.data?L.addRecord(e,e.data):L.getRecord(e)||L.addRecord(e,{}))})),l.notify(),c.notify(),a.map((e=>{var n;return{language:e.language,namespace:e.namespace,data:null!==(n=e.data)&&void 0!==n?n:{},cacheKey:e.cacheKey}}))}});return L}function L(e,n){var a={};for(var t in e)Object.prototype.hasOwnProperty.call(e,t)&&n.indexOf(t)<0&&(a[t]=e[t]);if(null!=e&&"function"==typeof Object.getOwnPropertySymbols){var i=0;for(t=Object.getOwnPropertySymbols(e);i<t.length;i++)n.indexOf(t[i])<0&&Object.prototype.propertyIsEnumerable.call(e,t[i])&&(a[t[i]]=e[t[i]])}return a}const j="invalid",w={observerOptions:{tagAttributes:{textarea:["placeholder"],input:["value","placeholder"],img:["alt"],"*":["aria-label","title"]},restrictedElements:["script","style"],highlightKeys:["Alt"],highlightColor:"rgb(255, 0, 0)",highlightWidth:5,inputPrefix:"%-%tolgee:",inputSuffix:"%-%",passToParent:["option","optgroup"],fullKeyEncode:!1},observerType:"invisible",onFormatError:j,apiUrl:"https://app.tolgee.io",autoLoadRequiredData:!0,fetch:c(),onTranslationMissing:({key:e})=>e,disableCache:!1},R=(...e)=>{let n={};return e.forEach((e=>{n=Object.assign(Object.assign(Object.assign({},n),e),{observerOptions:Object.assign(Object.assign({},n.observerOptions),null==e?void 0:e.observerOptions)})})),n};function k(e,n){const a=R(w,null==n?void 0:n.initialOptions,e);return a.apiUrl=g(a.apiUrl),(null==e?void 0:e.fetch)&&(a.fetch=c(e.fetch)),{initialOptions:a,activeNamespaces:(null==n?void 0:n.activeNamespaces)||new Map,language:null==n?void 0:n.language,pendingLanguage:null==n?void 0:n.language,isInitialLoading:!1,isRunning:!1}}function E(e,t,i,o,r,s,g,l){const c={ui:void 0},u={formatters:[],finalFormatter:void 0,observer:void 0,devBackend:void 0,backends:[],ui:void 0,languageDetector:void 0,languageStorage:void 0},d=async({keysAndDefaults:e,target:n})=>{var a;const t=e.map((({key:e,ns:n,defaultValue:a})=>({key:e,defaultValue:a,fallbackNamespaces:o(n),namespace:r({key:e,ns:n})[0],translation:s({key:e,ns:n})})));null===(a=u.ui)||void 0===a||a.handleElementClick(t,n)},f=(e,n)=>{var a;return(null===(a=u.observer)||void 0===a?void 0:a.findPositions(e,n))||[]};function h(e){const n=s({key:e.key,ns:e.ns});return A.formatTranslation(Object.assign(Object.assign({},e),{translation:n,formatEnabled:!0}))}function m(){return{fetch:t().fetch}}function b(e){u.observer=null==e?void 0:e()}function y(){return Boolean(u.observer)}function O(e){e&&u.formatters.push(e)}function w(e){u.finalFormatter=e}function R(e){c.ui=e}function k(){return Boolean(c.ui)}function E(e){u.languageStorage=e}function D(e){u.languageDetector=e}function C(e){e&&u.backends.push(e)}function N(e){u.devBackend=e}const A=Object.freeze({addPlugin:function(e,n){n(e,Object.freeze({setFinalFormatter:w,addFormatter:O,setObserver:b,hasObserver:y,setUi:R,hasUi:k,setDevBackend:N,addBackend:C,setLanguageDetector:D,setLanguageStorage:E}))},findPositions:f,run(){var e,n;const{apiKey:a,apiUrl:i,projectId:o,observerOptions:r,tagNewKeys:s,filterTag:p}=t();u.ui=null===(e=c.ui)||void 0===e?void 0:e.call(c,{apiKey:a,apiUrl:i,projectId:o,highlight:A.highlight,changeTranslation:g,findPositions:f,onPermanentChange:e=>l.onPermanentChange.emit(e),tagNewKeys:s,filterTag:p}),null===(n=u.observer)||void 0===n||n.run({mouseHighlight:!0,options:r,translate:h,onClick:d})},stop(){var e;u.ui=void 0,null===(e=u.observer)||void 0===e||e.stop()},getLanguageStorage:()=>u.languageStorage,getInitialLanguage(){const e=i();return n(a(l.onError,(e=>new v("Tolgee: Failed to load language",e)),(()=>{var e;return null===(e=u.languageStorage)||void 0===e?void 0:e.getLanguage(m())})),(n=>e&&!e.includes(n)||!n?function(){if(!u.languageDetector)return;const e=i();return a(l.onError,(e=>new p("Tolgee: Failed to detect language",e)),(()=>{var n;return null===(n=u.languageDetector)||void 0===n?void 0:n.getLanguage(Object.assign({availableLanguages:e},m()))}))}():n))},setStoredLanguage:e=>a(l.onError,(e=>new v("Tolgee: Failed to store language",e)),(()=>{var n;return null===(n=u.languageStorage)||void 0===n?void 0:n.setLanguage(e,m())})),getDevBackend:()=>u.devBackend,getBackendRecord:async({language:e,namespace:n})=>{for(const a of u.backends){const t=await a.getRecord(Object.assign({language:e,namespace:n},m()));if(void 0!==t)return t}},getBackendDevRecord:async({language:e,namespace:n})=>{var a;const{apiKey:i,apiUrl:o,projectId:r,filterTag:s}=t();if(i&&o&&A.hasDevBackend())return null===(a=u.devBackend)||void 0===a?void 0:a.getRecord(Object.assign({apiKey:i,apiUrl:o,projectId:r,language:e,namespace:n,filterTag:s},m()))},getLanguageDetector:()=>u.languageDetector,retranslate(){var e;null===(e=u.observer)||void 0===e||e.retranslate()},highlight:(e,n)=>{var a,t;return(null===(t=null===(a=u.observer)||void 0===a?void 0:a.highlight)||void 0===t?void 0:t.call(a,e,n))||{unhighlight(){}}},unwrap(e){var n;return u.observer?null===(n=u.observer)||void 0===n?void 0:n.unwrap(e):{text:e,keys:[]}},wrap(e){var n;return u.observer?null===(n=u.observer)||void 0===n?void 0:n.wrap(e):e.translation},hasDevBackend:()=>Boolean(A.getDevBackend()),formatTranslation(n){var a,{formatEnabled:i}=n,o=L(n,["formatEnabled"]);const{key:r,translation:s,defaultValue:g,noWrap:l,params:c,ns:d,orEmpty:f}=o,p=null!=s?s:g;let v="";null==s&&(v=t().onTranslationMissing(o));let h=null!=p?p:f?"":v;const m=e(),b=i||!(null===(a=u.observer)||void 0===a?void 0:a.outputNotFormattable),y=e=>u.observer&&!l?u.observer.wrap({key:r,translation:e,defaultValue:g,params:c,ns:d}):e;h=y(h);try{if(p&&m&&b)for(const e of u.formatters)h=e.format({translation:h,language:m,params:c});u.finalFormatter&&p&&m&&b&&(h=u.finalFormatter.format({translation:h,language:m,params:c}))}catch(e){console.error(e);const n=("string"==typeof(O=e)?O:"string"==typeof(null==O?void 0:O.message)?O.message:void 0)||j,a=t().onFormatError,i=typeof a;h="string"===i?a:"function"===i?a(n,o):j,h=y(h)}var O;return h}});return A}const D=(e,n,a)=>{let t=e;return Object.freeze({init(e){t=e},notify(){const e=n();t!==e&&a(e),t=e}})};const C=(e,...n)=>{let a,t={};return"object"==typeof e?t=e:(t.key=e,"string"==typeof n[0]?(t.defaultValue=n[0],a=n[1]):"object"==typeof n[0]&&(a=n[0])),a&&(t=Object.assign(Object.assign({},function(e){var{ns:n,noWrap:a,orEmpty:t,params:i,language:o}=e,r=L(e,["ns","noWrap","orEmpty","params","language"]);const s={ns:n,noWrap:a,orEmpty:t,language:o};return Object.assign(Object.assign({},s),{params:Object.assign({},r)})}(a)),t)),t};function N({options:a}){const i=function(){let e=!0;function n(){return e}const a=Object.freeze({onPendingLanguageChange:u("pendingLanguage",n),onLanguageChange:u("language",n),onLoadingChange:u("loading",n),onFetchingChange:u("fetching",n),onInitialLoaded:u("initialLoad",n),onRunningChange:u("running",n),onCacheChange:u("cache",n),onPermanentChange:u("permanentChange",n),onError:u("error",n),onUpdate:d(n),setEmitterActive(n){e=n},on:(e,n)=>{switch(e){case"pendingLanguage":return a.onPendingLanguageChange.listen(n);case"language":return a.onLanguageChange.listen(n);case"loading":return a.onLoadingChange.listen(n);case"fetching":return a.onFetchingChange.listen(n);case"initialLoad":return a.onInitialLoaded.listen(n);case"running":return a.onRunningChange.listen(n);case"cache":return a.onCacheChange.listen(n);case"update":return a.onUpdate.listen(n);case"permanentChange":return a.onPermanentChange.listen(n);case"error":return a.onError.listen(n)}}});return a.onInitialLoaded.listen((e=>a.onUpdate.emit(e,!1))),a.onLanguageChange.listen((e=>a.onUpdate.emit(e,!1))),a.onCacheChange.listen((e=>a.onUpdate.emit(e,!0))),a}(),l=D(!1,(()=>v.isFetching()),i.onFetchingChange.emit),c=D(!1,(()=>P.isLoading()),i.onLoadingChange.emit),f=function(e,n,a){let t,i=k();const l=Object.freeze({init(e){i=k(e,i)},isRunning:()=>i.isRunning,setRunning(e){i.isRunning!==e&&(i.isRunning=e,a.emit(e))},isInitialLoading:()=>i.isInitialLoading,isCacheDisabled:()=>i.initialOptions.disableCache,setInitialLoading(e){i.isInitialLoading=e},getLanguage:()=>i.language||i.initialOptions.language,setLanguage(n){i.language!==n&&(i.language=n,e.emit(n))},getPendingLanguage:()=>i.pendingLanguage||l.getLanguage(),setPendingLanguage(e){i.pendingLanguage!==e&&(i.pendingLanguage=e,n.emit(e))},getInitialOptions:()=>Object.assign(Object.assign({},i.initialOptions),t),addActiveNs(e){o(e).forEach((e=>{const n=i.activeNamespaces.get(e);void 0!==n?i.activeNamespaces.set(e,n+1):i.activeNamespaces.set(e,1)}))},removeActiveNs(e){o(e).forEach((e=>{const n=i.activeNamespaces.get(e);void 0!==n&&n>1?i.activeNamespaces.set(e,n-1):i.activeNamespaces.delete(e)}))},getRequiredNamespaces:()=>s([l.getDefaultNs(),...i.initialOptions.ns||[],...o(i.initialOptions.fallbackNs),...i.activeNamespaces.keys()]),getFallbackLangs(e){const n=e||l.getLanguage();return n?s([n,...r(n,i.initialOptions.fallbackLanguage)]):[]},getFallbackNs:()=>o(i.initialOptions.fallbackNs),getNs(){var e,n;return(null===(e=i.initialOptions.ns)||void 0===e?void 0:e.length)?i.initialOptions.ns:[null!==(n=i.initialOptions.defaultNs)&&void 0!==n?n:""]},getDefaultNs(e){var n,a,t;return void 0===e?null!==(t=null!==(n=i.initialOptions.defaultNs)&&void 0!==n?n:null===(a=i.initialOptions.ns)||void 0===a?void 0:a[0])&&void 0!==t?t:"":e},getAvailableLanguages(){if(i.initialOptions.availableLanguages)return i.initialOptions.availableLanguages;if(i.initialOptions.staticData){const e=Object.keys(i.initialOptions.staticData).map((e=>b(e).language));return Array.from(new Set(e))}},getAvailableNs:()=>i.initialOptions.availableNs,withDefaultNs:e=>({namespace:void 0===e.namespace?l.getDefaultNs():e.namespace,language:e.language}),overrideCredentials(e){t=e?Object.assign(Object.assign({},e),{apiUrl:g(e.apiUrl)}):void 0}});return l}(i.onLanguageChange,i.onPendingLanguageChange,i.onRunningChange),p=E(f.getLanguage,f.getInitialOptions,f.getAvailableLanguages,y,N,A,j,i),v=O(i,p.getBackendRecord,p.getBackendDevRecord,f.withDefaultNs,f.isInitialLoading,f.isCacheDisabled,l,c);let h;function m(e){return f.getDefaultNs(e)}function y(e){return s([...o(m(e)),...f.getFallbackNs()])}function L(e){return s([...o(null!=e?e:m()),...f.getRequiredNamespaces()])}function j(e,n,a){const t=f.withDefaultNs(e),i=v.getTranslation(t,n);return v.changeTranslation(t,n,a),{revert(){v.changeTranslation(t,n,i)}}}function w(e){f.init(e),v.addStaticData(f.getInitialOptions().staticData)}function R(e,n){const a=f.getFallbackLangs(e),t=L(n),i=[];return a.forEach((e=>{t.forEach((n=>{i.push({language:e,namespace:n})}))})),i}function N({key:e,ns:n}){const a=f.getFallbackLangs(),t=y(null!=n?n:void 0);return v.getTranslationNs(t,a,e)}function A({key:e,ns:n,language:a}){const t=y(null!=n?n:void 0),i=f.getFallbackLangs(a);return v.getTranslationFallback(t,i,e)}function F(){const a=n(I(),(()=>{const e=R(n,a).filter((e=>!v.exists(e,!0)));var n,a;if(e.length&&f.getInitialOptions().autoLoadRequiredData)return v.loadRecords(e,{useCache:!0})}));if(e(a))return f.setInitialLoading(!0),l.notify(),c.notify(),Promise.resolve(a).then((()=>{f.setInitialLoading(!1),l.notify(),c.notify(),i.onInitialLoaded.emit()}));i.onInitialLoaded.emit()}function I(){if(f.getLanguage())return;return n(p.getInitialLanguage(),(e=>{const n=e||f.getInitialOptions().defaultLanguage;n&&f.setLanguage(n)}))}a&&w(a),i.onUpdate.listen((()=>{f.isRunning()&&p.retranslate()}));const P=Object.freeze(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign({},i),f),p),v),{init:w,getTranslation:A,changeTranslation:j,getTranslationNs:N,getDefaultAndFallbackNs:y,findPositions:p.findPositions,getRequiredDescriptors:R,async changeLanguage(e){f.getPendingLanguage()===e&&f.getLanguage()===e||(f.setPendingLanguage(e),f.isRunning()&&f.getInitialOptions().autoLoadRequiredData&&await v.loadRecords(R(e),{useCache:!0}),e===f.getPendingLanguage()&&(f.setLanguage(e),await p.setStoredLanguage(e)))},async addActiveNs(e,n){n||f.addActiveNs(e),f.isRunning()&&await v.loadRecords(R(void 0,e),{useCache:!0})},async loadRecord(e,n){var a;return null===(a=(await P.loadRecords([e],n))[0])||void 0===a?void 0:a.data},isLoading:e=>v.isLoading(f.getLanguage(),e),isLoaded(e){const n=f.getLanguage();if(!n)return!1;const a=f.getFallbackLangs(n),t=L(e),i=[];return a.forEach((e=>{t.forEach((n=>{v.exists({language:e,namespace:n})||i.push({language:e,namespace:n})}))})),0===i.length},t:(...e)=>{const n=C(...e),a=A(n);return p.formatTranslation(Object.assign(Object.assign({},n),{translation:a}))},isDev:()=>Boolean(f.getInitialOptions().apiKey&&f.getInitialOptions().apiUrl),async loadRequired(e){(null==e?void 0:e.language)||await I();const n=R(null==e?void 0:e.language);return P.loadRecords(n,e)},async loadMatrix(e){const n=function(e){let n=[],a=[];if(Array.isArray(e.languages))n=e.languages;else if("all"===e.languages){const e=P.getAvailableLanguages();if(!e)throw new Error(t("availableLanguages"));n=e}if(Array.isArray(e.namespaces))a=e.namespaces;else if("all"===e.namespaces){const e=P.getAvailableNs();if(!e)throw new Error(t("availableNs"));a=e}const i=[];return n.forEach((e=>{a.forEach((n=>{i.push({language:e,namespace:n})}))})),i}(e);return P.loadRecords(n,e)},run:()=>(function(){if((p.getLanguageDetector()||p.getLanguageStorage())&&!f.getAvailableLanguages())throw new Error(t("availableLanguages"));if(!f.getLanguage()&&!f.getInitialOptions().defaultLanguage)throw new Error(t(["defaultLanguage","language"]))}(),f.isRunning()||(f.setRunning(!0),p.run(),h=F()),Promise.resolve(h)),stop(){f.isRunning()&&(p.stop(),f.setRunning(!1))}}));return P}const A=()=>{const e={plugins:[],options:{}},n=Object.freeze({use:a=>(e.plugins.push(a),n),updateDefaults:a=>(e.options=R(e.options,a),n),init(n){const a=function(e){const n=N({options:e});function a(e){const a=n.isRunning();a&&n.stop(),e(),n.isDev()&&n.invalidate(),a&&n.run()}n.isDev()&&n.invalidate();const t=Object.freeze({on:n.on,setEmitterActive:n.setEmitterActive,getLanguage:n.getLanguage,getPendingLanguage:n.getPendingLanguage,changeLanguage:n.changeLanguage,changeTranslation:n.changeTranslation,addActiveNs:n.addActiveNs,removeActiveNs:n.removeActiveNs,loadRequired:n.loadRequired,loadMatrix:n.loadMatrix,loadRecords:n.loadRecords,loadRecord:n.loadRecord,addStaticData:n.addStaticData,getRecord:n.getRecord,getAllRecords:n.getAllRecords,isLoaded:n.isLoaded,getRequiredDescriptors:n.getRequiredDescriptors,isInitialLoading:n.isInitialLoading,isLoading:n.isLoading,isFetching:n.isFetching,isRunning:n.isRunning,run:n.run,stop:n.stop,t:n.t,highlight:n.highlight,findPositions:n.findPositions,getInitialOptions:n.getInitialOptions,isDev:n.isDev,wrap:n.wrap,unwrap:n.unwrap,overrideCredentials(e){a((()=>n.overrideCredentials(e)))},addPlugin(e){e&&a((()=>n.addPlugin(t,e)))},updateOptions(e){e&&a((()=>n.init(e)))}});return t}(R(e.options,n));return e.plugins.forEach(a.addPlugin),a}});return n},F=0,I=1,P=2;class T extends Error{constructor(e,n,a){let t,i="";e===F?t="Empty parameter":e===I?(t="Unexpected character",i="Did you forget to use FormatIcu to render ICU message syntax?"):t="Unexpected end",super(`Tolgee parser: ${t} at ${n} in "${a}"`+(i?"\n"+i:"")),this.code=e,this.index=n}}function x(e){return/\s/.test(e)}const S=0,B=1,U=2,K=3,$=4,z=new Set([U,B,S]),q="'",M=new Set(["{","}",q]),V=e=>/[0-9a-zA-Z_]/.test(e);function W(e,n){const[a,t]=function(e){let n=S,a="",t="",i="";const o=[],r=[];let s=0;function g(n){throw new T(n,s,e)}const l=()=>{o.push(a),a=""},c=()=>{""===t&&g(F),r.push(t),t=""};for(s=0;s<e.length;s++)switch(i=e[s],n){case S:i===q?(a+=i,n=B):"{"===i?(l(),n=K):(a+=i,n=S);break;case B:M.has(i)?(a=a.slice(0,-1)+i,n=U):(a+=i,n=S);break;case U:i===q?n=S:(a+=i,n=U);break;case K:"}"===i?(c(),n=S):x(i)?""!==t&&(c(),n=$):(V(i)||g(I),t+=i,n=K);break;case $:"}"==i?n=S:x(i)?n=$:g(I)}return z.has(n)||g(P),l(),[o,r]}(e),i=[a[0]];for(let o=1;o<a.length;o++){const r=null==n?void 0:n[t[o-1]];if(void 0===r)throw new Error(`Missing parameter "${t[o-1]}" in "${e}"`);i.push(String(r)),i.push(a[o])}return i.join("")}const H=()=>(e,n)=>(n.setFinalFormatter({format:({translation:e,params:n})=>W(e,n)}),e);export{H as FormatSimple,p as LanguageDetectorError,v as LanguageStorageError,f as RecordFetchError,A as TolgeeCore,c as createFetchFunction,i as getFallback,o as getFallbackArray,C as getTranslateProps};
//# sourceMappingURL=tolgee.esm.min.js.map

@@ -1,2 +0,2 @@

!function(e,n){"object"==typeof exports&&"undefined"!=typeof module?n(exports):"function"==typeof define&&define.amd?define(["exports"],n):n((e="undefined"!=typeof globalThis?globalThis:e||self)["@tolgee/core"]={})}(this,(function(e){"use strict";function n(e){return Boolean(e&&"function"==typeof e.then)}function t(e,t){return n(e)?Promise.resolve(e).then(t):t(e)}function a(e,t,a){function i(n){const a=t(n);throw e.emit(a),console.error(a),a}try{const e=a();return n(e)?e.catch(i):e}catch(e){i(e)}}function i(e){const n=(Array.isArray(e)?e:[e]).map((e=>`'${e}'`)),t=n.slice(-2).join(" or ");return`Tolgee: You need to specify ${[...n.slice(0,-2),t].join(", ")} option`}function o(e){return"string"==typeof e?[e]:Array.isArray(e)?e:void 0}function r(e){return o(e)||[]}function s(e,n){return"object"!=typeof(t=n)||Array.isArray(t)||null===t?r(n):r(null==n?void 0:n[e]);var t}function g(e){return Array.from(new Set(e))}function c(e){return e?e.replace(/\/+$/,""):e}const l=(e,n)=>fetch(e,n);const u=(e=l)=>(n,t)=>{let a=(i=null==t?void 0:t.headers,Object.fromEntries(new Headers(i).entries()));var i;return a["x-api-key"]&&(a=Object.assign({"x-tolgee-sdk-type":"JS","x-tolgee-sdk-version":"prerelease"},a)),e(n,Object.assign(Object.assign({},t),{headers:a}))};function d(e){let n=[];return Object.freeze({listen(e){const t=n=>{e(n)};return n.push(t),{unsubscribe(){n=n.filter((e=>t!==e))}}},emit(t){e()&&n.forEach((e=>e({value:t})))}})}function f(e,n,t){const a=new Set,i=new Set;let o=[];function s(){if(0===o.length)return;const e=o;o=[],a.forEach((e=>{e({value:void 0})}));let t=new Set;e.forEach((e=>{void 0===e?t=void 0:void 0!==t&&e.forEach((e=>t.add(e)))}));!function(e){const t=new Set(n());i.forEach((n=>{(void 0===e||-1!==(null==e?void 0:e.findIndex((e=>t.has(e)||n.namespaces.has(e)))))&&n.fn({value:void 0})}))}(t?Array.from(t.keys()):void 0)}return Object.freeze({emit(n,t){e()&&(o.push(n),t?setTimeout(s,0):s())},listen(e){a.add(e);return{unsubscribe:()=>{a.delete(e)}}},listenSome(e){const n={fn:n=>{e(n)},namespaces:new Set};i.add(n);const a={unsubscribe:()=>{i.delete(n)},subscribeNs:e=>(r(e).forEach((e=>n.namespaces.add(e))),void 0===e&&n.namespaces.add(t()),a)};return a}})}class p extends Error{constructor(e,n,t=!1){const{language:a,namespace:i}=e;super(`Tolgee: Failed to fetch record for "${a}"${i&&` and "${i}"`}`),this.cause=n,this.isDev=t,this.name="RecordFetchError",this.language=a,this.namespace=i}}class v extends Error{constructor(e,n){super(e),this.cause=n,this.name="LanguageDetectorError"}}class h extends Error{constructor(e,n){super(e),this.cause=n,this.name="LanguageStorageError"}}const m=e=>{const n=new Map;return Object.entries(e).forEach((([e,t])=>{null!=t&&("object"!=typeof t?n.set(e,t):m(t).forEach(((t,a)=>{n.set(e+"."+a,t)})))})),n},b=e=>{const[n,...t]=e.split(":");return{language:n,namespace:t.join(":")||""}},y=({language:e,namespace:n})=>n?`${e}:${n}`:e;function O(e,t,a,i,o,s,c){const l=new Map,u=new Map;let d={},f=0;function v(n,t,a){const i=y(n);u.set(i,{data:m(t),version:a}),e.onCacheChange.emit(n)}async function h(i,o){let r;if(o)try{r=await a(i)}catch(n){const t=new p(i,n,!0);e.onError.emit(t),console.warn(t)}return r||(r=await async function(a){function i(n){const t=new p(a,n);throw e.onError.emit(t),console.error(t),t}const o=t(a);if(n(o)){const e=await o.catch(i);if(void 0!==e)return e}const r=d[y(a)];if("function"!=typeof r)return r;try{return await r()}catch(e){i(e)}}(i)),r}const O=Object.freeze({addStaticData(e){e&&(d=Object.assign(Object.assign({},d),e),Object.entries(e).forEach((([e,n])=>{if("function"!=typeof n){const t=b(e),a=u.get(e);a&&0!==a.version||v(t,n,0)}})))},invalidate(){l.clear(),f+=1},addRecord(e,n){v(e,n,f)},exists(e,n=!1){const t=u.get(y(e));return t&&n?t.version===f:Boolean(t)},getRecord(e){var n;return null===(n=u.get(y(i(e))))||void 0===n?void 0:n.data},getTranslation(e,n){var t;return null===(t=u.get(y(e)))||void 0===t?void 0:t.data.get(n)},getTranslationNs(e,n,t){var a;for(const i of e)for(const e of n){const n=null===(a=u.get(y({language:e,namespace:i})))||void 0===a?void 0:a.data.get(t);if(null!=n)return[i]}return g(e)},getTranslationFallback(e,n,t){var a;for(const i of e)for(const e of n){const n=null===(a=u.get(y({language:e,namespace:i})))||void 0===a?void 0:a.data.get(t);if(null!=n)return n}},changeTranslation(n,t,a){var i;const o=null===(i=u.get(y(n)))||void 0===i?void 0:i.data;null==o||o.set(t,a),e.onCacheChange.emit(Object.assign(Object.assign({},n),{key:t}))},isFetching(e){if(o())return!0;if(void 0===e)return l.size>0;const n=r(e);return Boolean(Array.from(l.keys()).find((e=>n.includes(b(e).namespace))))},isLoading(e,n){const t=r(n);return Boolean(o()||Array.from(l.keys()).find((n=>{const a=b(n);return(!t.length||t.includes(a.namespace))&&!O.exists({namespace:a.namespace,language:e})})))},async loadRecords(e,n){const t=e.map((e=>{const t=i(e),a=y(t),o=l.get(a);if(o)return{new:!1,promise:o,keyObject:t,cacheKey:a};const r=h(t,n)||Promise.resolve(void 0);return l.set(a,r),{new:!0,promise:r,keyObject:t,cacheKey:a}}));s.notify(),c.notify();const a=await Promise.all(t.map((e=>e.promise)));return t.forEach(((e,n)=>{const t=l.get(e.cacheKey)!==e.promise;if(e.new&&!t){l.delete(e.cacheKey);const t=a[n];t?O.addRecord(e.keyObject,t):O.getRecord(e.keyObject)||O.addRecord(e.keyObject,{})}})),s.notify(),c.notify(),t.map((e=>O.getRecord(e.keyObject)))},getAllRecords:()=>Array.from(u.entries()).map((([e,n])=>Object.assign(Object.assign({},b(e)),{data:n.data})))});return O}function L(e,n){var t={};for(var a in e)Object.prototype.hasOwnProperty.call(e,a)&&n.indexOf(a)<0&&(t[a]=e[a]);if(null!=e&&"function"==typeof Object.getOwnPropertySymbols){var i=0;for(a=Object.getOwnPropertySymbols(e);i<a.length;i++)n.indexOf(a[i])<0&&Object.prototype.propertyIsEnumerable.call(e,a[i])&&(t[a[i]]=e[a[i]])}return t}const j="invalid",k={defaultNs:"",observerOptions:{tagAttributes:{textarea:["placeholder"],input:["value","placeholder"],img:["alt"],"*":["aria-label","title"]},restrictedElements:["script","style"],highlightKeys:["Alt"],highlightColor:"rgb(255, 0, 0)",highlightWidth:5,inputPrefix:"%-%tolgee:",inputSuffix:"%-%",passToParent:["option","optgroup"],fullKeyEncode:!1},observerType:"invisible",onFormatError:j,apiUrl:"https://app.tolgee.io",fetch:u(),onTranslationMissing:({key:e})=>e},w=(...e)=>{let n={};return e.forEach((e=>{n=Object.assign(Object.assign(Object.assign({},n),e),{observerOptions:Object.assign(Object.assign({},n.observerOptions),null==e?void 0:e.observerOptions)})})),n};function E(e,n){const t=w(k,null==n?void 0:n.initialOptions,e);return t.apiUrl=c(t.apiUrl),(null==e?void 0:e.fetch)&&(t.fetch=u(e.fetch)),{initialOptions:t,activeNamespaces:(null==n?void 0:n.activeNamespaces)||new Map,language:null==n?void 0:n.language,pendingLanguage:null==n?void 0:n.language,isInitialLoading:!1,isRunning:!1}}function R(e,n,i,o,r,s,g,c){const l={ui:void 0},u={formatters:[],finalFormatter:void 0,observer:void 0,devBackend:void 0,backends:[],ui:void 0,languageDetector:void 0,languageStorage:void 0},d=async({keysAndDefaults:e,target:n})=>{var t;const a=e.map((({key:e,ns:n,defaultValue:t})=>({key:e,defaultValue:t,fallbackNamespaces:o(n),namespace:r({key:e,ns:n})[0],translation:s({key:e,ns:n})})));null===(t=u.ui)||void 0===t||t.handleElementClick(a,n)},f=(e,n)=>{var t;return(null===(t=u.observer)||void 0===t?void 0:t.findPositions(e,n))||[]};function p(e){const n=s({key:e.key,ns:e.ns});return D.formatTranslation(Object.assign(Object.assign({},e),{translation:n,formatEnabled:!0}))}function m(){return{fetch:n().fetch}}function b(e){u.observer=null==e?void 0:e()}function y(){return Boolean(u.observer)}function O(e){e&&u.formatters.push(e)}function k(e){u.finalFormatter=e}function w(e){l.ui=e}function E(){return Boolean(l.ui)}function R(e){u.languageStorage=e}function N(e){u.languageDetector=e}function F(e){e&&u.backends.push(e)}function C(e){u.devBackend=e}const D=Object.freeze({addPlugin:function(e,n){n(e,Object.freeze({setFinalFormatter:k,addFormatter:O,setObserver:b,hasObserver:y,setUi:w,hasUi:E,setDevBackend:C,addBackend:F,setLanguageDetector:N,setLanguageStorage:R}))},findPositions:f,run(){var e,t;const{apiKey:a,apiUrl:i,projectId:o,observerOptions:r,tagNewKeys:s,filterTag:v}=n();u.ui=null===(e=l.ui)||void 0===e?void 0:e.call(l,{apiKey:a,apiUrl:i,projectId:o,highlight:D.highlight,changeTranslation:g,findPositions:f,onPermanentChange:e=>c.onPermanentChange.emit(e),tagNewKeys:s,filterTag:v}),null===(t=u.observer)||void 0===t||t.run({mouseHighlight:!0,options:r,translate:p,onClick:d})},stop(){var e;u.ui=void 0,null===(e=u.observer)||void 0===e||e.stop()},getLanguageStorage:()=>u.languageStorage,getInitialLanguage(){const e=i();return t(a(c.onError,(e=>new h("Tolgee: Failed to load language",e)),(()=>{var e;return null===(e=u.languageStorage)||void 0===e?void 0:e.getLanguage(m())})),(n=>e&&!e.includes(n)||!n?function(){if(!u.languageDetector)return;const e=i();return a(c.onError,(e=>new v("Tolgee: Failed to detect language",e)),(()=>{var n;return null===(n=u.languageDetector)||void 0===n?void 0:n.getLanguage(Object.assign({availableLanguages:e},m()))}))}():n))},setStoredLanguage:e=>a(c.onError,(e=>new h("Tolgee: Failed to store language",e)),(()=>{var n;return null===(n=u.languageStorage)||void 0===n?void 0:n.setLanguage(e,m())})),getDevBackend:()=>u.devBackend,getBackendRecord:async({language:e,namespace:n})=>{for(const t of u.backends){const a=await t.getRecord(Object.assign({language:e,namespace:n},m()));if(void 0!==a)return a}},getBackendDevRecord:async({language:e,namespace:t})=>{var a;const{apiKey:i,apiUrl:o,projectId:r,filterTag:s}=n();return null===(a=u.devBackend)||void 0===a?void 0:a.getRecord(Object.assign({apiKey:i,apiUrl:o,projectId:r,language:e,namespace:t,filterTag:s},m()))},getLanguageDetector:()=>u.languageDetector,retranslate(){var e;null===(e=u.observer)||void 0===e||e.retranslate()},highlight:(e,n)=>{var t,a;return(null===(a=null===(t=u.observer)||void 0===t?void 0:t.highlight)||void 0===a?void 0:a.call(t,e,n))||{unhighlight(){}}},unwrap(e){var n;return u.observer?null===(n=u.observer)||void 0===n?void 0:n.unwrap(e):{text:e,keys:[]}},wrap(e){var n;return u.observer?null===(n=u.observer)||void 0===n?void 0:n.wrap(e):e.translation},hasDevBackend:()=>Boolean(D.getDevBackend()),formatTranslation(t){var a,{formatEnabled:i}=t,o=L(t,["formatEnabled"]);const{key:r,translation:s,defaultValue:g,noWrap:c,params:l,ns:d,orEmpty:f}=o,p=null!=s?s:g;let v="";null==s&&(v=n().onTranslationMissing(o));let h=null!=p?p:f?"":v;const m=e(),b=i||!(null===(a=u.observer)||void 0===a?void 0:a.outputNotFormattable),y=e=>u.observer&&!c?u.observer.wrap({key:r,translation:e,defaultValue:g,params:l,ns:d}):e;h=y(h);try{if(p&&m&&b)for(const e of u.formatters)h=e.format({translation:h,language:m,params:l});u.finalFormatter&&p&&m&&b&&(h=u.finalFormatter.format({translation:h,language:m,params:l}))}catch(e){console.error(e);const t=("string"==typeof(O=e)?O:"string"==typeof(null==O?void 0:O.message)?O.message:void 0)||j,a=n().onFormatError,i=typeof a;h="string"===i?a:"function"===i?a(t,o):j,h=y(h)}var O;return h}});return D}const N=(e,n,t)=>{let a=e;return Object.freeze({init(e){a=e},notify(){const e=n();a!==e&&t(e),a=e}})};const F=(e,...n)=>{let t,a={};return"object"==typeof e?a=e:(a.key=e,"string"==typeof n[0]?(a.defaultValue=n[0],t=n[1]):"object"==typeof n[0]&&(t=n[0])),t&&(a=Object.assign(Object.assign({},function(e){var{ns:n,noWrap:t,orEmpty:a,params:i,language:o}=e,r=L(e,["ns","noWrap","orEmpty","params","language"]);const s={ns:n,noWrap:t,orEmpty:a,language:o};return Object.assign(Object.assign({},s),{params:Object.assign({},r)})}(t)),a)),a};function C({options:e}){const a=function(e,n){let t=!0;function a(){return t}const i=Object.freeze({onPendingLanguageChange:d(a),onLanguageChange:d(a),onLoadingChange:d(a),onFetchingChange:d(a),onInitialLoaded:d(a),onRunningChange:d(a),onCacheChange:d(a),onUpdate:f(a,e,n),onPermanentChange:d(a),onError:d(a),setEmitterActive(e){t=e},on:(e,n)=>{switch(e){case"pendingLanguage":return i.onPendingLanguageChange.listen(n);case"language":return i.onLanguageChange.listen(n);case"loading":return i.onLoadingChange.listen(n);case"fetching":return i.onFetchingChange.listen(n);case"initialLoad":return i.onInitialLoaded.listen(n);case"running":return i.onRunningChange.listen(n);case"cache":return i.onCacheChange.listen(n);case"update":return i.onUpdate.listen(n);case"permanentChange":return i.onPermanentChange.listen(n);case"error":return i.onError.listen(n)}}});return i.onInitialLoaded.listen((()=>i.onUpdate.emit())),i.onLanguageChange.listen((()=>i.onUpdate.emit())),i.onCacheChange.listen((({value:e})=>i.onUpdate.emit([e.namespace],!0))),i}(m,y),o=N(!1,(()=>v.isFetching()),a.onFetchingChange.emit),l=N(!1,(()=>I.isLoading()),a.onLoadingChange.emit),u=function(e,n,t){let a,i=E();const o=Object.freeze({init(e){i=E(e,i)},isRunning:()=>i.isRunning,setRunning(e){i.isRunning!==e&&(i.isRunning=e,t.emit(e))},isInitialLoading:()=>i.isInitialLoading,setInitialLoading(e){i.isInitialLoading=e},getLanguage:()=>i.language||i.initialOptions.language,setLanguage(n){i.language!==n&&(i.language=n,e.emit(n))},getPendingLanguage:()=>i.pendingLanguage||o.getLanguage(),setPendingLanguage(e){i.pendingLanguage!==e&&(i.pendingLanguage=e,n.emit(e))},getInitialOptions:()=>Object.assign(Object.assign({},i.initialOptions),a),addActiveNs(e){r(e).forEach((e=>{const n=i.activeNamespaces.get(e);void 0!==n?i.activeNamespaces.set(e,n+1):i.activeNamespaces.set(e,1)}))},removeActiveNs(e){r(e).forEach((e=>{const n=i.activeNamespaces.get(e);void 0!==n&&n>1?i.activeNamespaces.set(e,n-1):i.activeNamespaces.delete(e)}))},getRequiredNamespaces:()=>g([...i.initialOptions.ns||[i.initialOptions.defaultNs],...r(i.initialOptions.fallbackNs),...i.activeNamespaces.keys()]),getFallbackLangs(e){const n=e||o.getLanguage();return n?g([n,...s(n,i.initialOptions.fallbackLanguage)]):[]},getFallbackNs:()=>r(i.initialOptions.fallbackNs),getDefaultNs:e=>void 0===e?i.initialOptions.defaultNs:e,getAvailableLanguages(){if(i.initialOptions.availableLanguages)return i.initialOptions.availableLanguages;if(i.initialOptions.staticData){const e=Object.keys(i.initialOptions.staticData).map((e=>b(e).language));return Array.from(new Set(e))}},withDefaultNs:e=>({namespace:void 0===e.namespace?o.getInitialOptions().defaultNs:e.namespace,language:e.language}),overrideCredentials(e){a=e?Object.assign(Object.assign({},e),{apiUrl:c(e.apiUrl)}):void 0}});return o}(a.onLanguageChange,a.onPendingLanguageChange,a.onRunningChange),p=R(u.getLanguage,u.getInitialOptions,u.getAvailableLanguages,L,P,T,k,a),v=O(a,p.getBackendRecord,p.getBackendDevRecord,u.withDefaultNs,u.isInitialLoading,o,l);let h;function m(){return u.getFallbackNs()}function y(e){return u.getDefaultNs(e)}function L(e){return[...r(y(e)),...m()]}function j(e){return[...r(null!=e?e:y()),...u.getRequiredNamespaces()]}function k(e,n,t){const a=u.withDefaultNs(e),i=v.getTranslation(a,n);return v.changeTranslation(a,n,t),{revert(){v.changeTranslation(a,n,i)}}}function w(e){u.init(e),v.addStaticData(u.getInitialOptions().staticData)}function C(e,n){const t=u.getFallbackLangs(e),a=j(n),i=[];return t.forEach((e=>{a.forEach((n=>{v.exists({language:e,namespace:n},!0)||i.push({language:e,namespace:n})}))})),i}function D(e,n){const a=C(e,n);if(a.length)return t(I.loadRecords(a),(()=>{}))}function P({key:e,ns:n}){const t=u.getFallbackLangs(),a=L(null!=n?n:void 0);return v.getTranslationNs(a,t,e)}function T({key:e,ns:n,language:t}){const a=L(null!=n?n:void 0),i=u.getFallbackLangs(t);return v.getTranslationFallback(a,i,e)}function A(){const e=t(function(){if(u.getLanguage())return;return t(p.getInitialLanguage(),(e=>{const n=e||u.getInitialOptions().defaultLanguage;n&&u.setLanguage(n)}))}(),(()=>D()));if(n(e))return u.setInitialLoading(!0),o.notify(),l.notify(),Promise.resolve(e).then((()=>{u.setInitialLoading(!1),o.notify(),l.notify(),a.onInitialLoaded.emit()}));a.onInitialLoaded.emit()}e&&w(e),a.onUpdate.listen((()=>{u.isRunning()&&p.retranslate()}));const I=Object.freeze(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign({},a),u),p),v),{init:w,getTranslation:T,changeTranslation:k,getTranslationNs:P,getDefaultAndFallbackNs:L,findPositions:p.findPositions,getRequiredRecords:C,async changeLanguage(e){u.getPendingLanguage()===e&&u.getLanguage()===e||(u.setPendingLanguage(e),u.isRunning()&&await D(e),e===u.getPendingLanguage()&&(u.setLanguage(e),await p.setStoredLanguage(e)))},async addActiveNs(e,n){n||u.addActiveNs(e),u.isRunning()&&await D(void 0,e)},loadRecords:e=>v.loadRecords(e,I.isDev()),loadRecord:async e=>(await I.loadRecords([e]))[0],isLoading:e=>v.isLoading(u.getLanguage(),e),isLoaded(e){const n=u.getLanguage();if(!n)return!1;const t=u.getFallbackLangs(n),a=j(e),i=[];return t.forEach((e=>{a.forEach((n=>{v.exists({language:e,namespace:n})||i.push({language:e,namespace:n})}))})),0===i.length},t:(...e)=>{const n=F(...e),t=T(n);return p.formatTranslation(Object.assign(Object.assign({},n),{translation:t}))},isDev:()=>Boolean(u.getInitialOptions().apiKey&&u.getInitialOptions().apiUrl),run:()=>(function(){if((p.getLanguageDetector()||p.getLanguageStorage())&&!u.getAvailableLanguages())throw new Error(i("availableLanguages"));if(!u.getLanguage()&&!u.getInitialOptions().defaultLanguage)throw new Error(i(["defaultLanguage","language"]))}(),u.isRunning()||(u.setRunning(!0),p.run(),h=A()),Promise.resolve(h)),stop(){u.isRunning()&&(p.stop(),u.setRunning(!1))}}));return I}const D=0,P=1,T=2;class A extends Error{constructor(e,n,t){let a,i="";e===D?a="Empty parameter":e===P?(a="Unexpected character",i="Did you forget to use FormatIcu to render ICU message syntax?"):a="Unexpected end",super(`Tolgee parser: ${a} at ${n} in "${t}"`+(i?"\n"+i:"")),this.code=e,this.index=n}}function I(e){return/\s/.test(e)}const S=0,x=1,U=2,B=3,z=4,K=new Set([U,x,S]),$="'",M=new Set(["{","}",$]),q=e=>/[0-9a-zA-Z_]/.test(e);function V(e,n){const[t,a]=function(e){let n=S,t="",a="",i="";const o=[],r=[];let s=0;function g(n){throw new A(n,s,e)}const c=()=>{o.push(t),t=""},l=()=>{""===a&&g(D),r.push(a),a=""};for(s=0;s<e.length;s++)switch(i=e[s],n){case S:i===$?(t+=i,n=x):"{"===i?(c(),n=B):(t+=i,n=S);break;case x:M.has(i)?(t=t.slice(0,-1)+i,n=U):(t+=i,n=S);break;case U:i===$?n=S:(t+=i,n=U);break;case B:"}"===i?(l(),n=S):I(i)?""!==a&&(l(),n=z):(q(i)||g(P),a+=i,n=B);break;case z:"}"==i?n=S:I(i)?n=z:g(P)}return K.has(n)||g(T),c(),[o,r]}(e),i=[t[0]];for(let o=1;o<t.length;o++){const r=null==n?void 0:n[a[o-1]];if(void 0===r)throw new Error(`Missing parameter "${a[o-1]}" in "${e}"`);i.push(String(r)),i.push(t[o])}return i.join("")}e.FormatSimple=()=>(e,n)=>(n.setFinalFormatter({format:({translation:e,params:n})=>V(e,n)}),e),e.LanguageDetectorError=v,e.LanguageStorageError=h,e.RecordFetchError=p,e.TolgeeCore=()=>{const e={plugins:[],options:{}},n=Object.freeze({use:t=>(e.plugins.push(t),n),updateDefaults:t=>(e.options=w(e.options,t),n),init(n){const t=function(e){const n=C({options:e});function t(e){const t=n.isRunning();t&&n.stop(),e(),n.isDev()&&n.invalidate(),t&&n.run()}n.isDev()&&n.invalidate();const a=Object.freeze({on:n.on,onNsUpdate:n.onUpdate.listenSome,setEmitterActive:n.setEmitterActive,getLanguage:n.getLanguage,getPendingLanguage:n.getPendingLanguage,changeLanguage:n.changeLanguage,changeTranslation:n.changeTranslation,addActiveNs:n.addActiveNs,removeActiveNs:n.removeActiveNs,loadRecords:n.loadRecords,loadRecord:n.loadRecord,addStaticData:n.addStaticData,getRecord:n.getRecord,getAllRecords:n.getAllRecords,isLoaded:n.isLoaded,getRequiredRecords:n.getRequiredRecords,isInitialLoading:n.isInitialLoading,isLoading:n.isLoading,isFetching:n.isFetching,isRunning:n.isRunning,run:n.run,stop:n.stop,t:n.t,highlight:n.highlight,findPositions:n.findPositions,getInitialOptions:n.getInitialOptions,isDev:n.isDev,wrap:n.wrap,unwrap:n.unwrap,overrideCredentials(e){t((()=>n.overrideCredentials(e)))},addPlugin(e){e&&t((()=>n.addPlugin(a,e)))},updateOptions(e){e&&t((()=>n.init(e)))}});return a}(w(e.options,n));return e.plugins.forEach(t.addPlugin),t}});return n},e.createFetchFunction=u,e.getFallback=o,e.getFallbackArray=r,e.getTranslateProps=F,Object.defineProperty(e,"__esModule",{value:!0})}));
!function(e,n){"object"==typeof exports&&"undefined"!=typeof module?n(exports):"function"==typeof define&&define.amd?define(["exports"],n):n((e="undefined"!=typeof globalThis?globalThis:e||self)["@tolgee/core"]={})}(this,(function(e){"use strict";function n(e){return Boolean(e&&"function"==typeof e.then)}function a(e,a){return n(e)?Promise.resolve(e).then(a):a(e)}function t(e,a,t){function i(n){const t=a(n);throw e.emit(t),console.error(t),t}try{const e=t();return n(e)?e.catch(i):e}catch(e){i(e)}}function i(e){const n=(Array.isArray(e)?e:[e]).map((e=>`'${e}'`)),a=n.slice(-2).join(" or ");return`Tolgee: You need to specify ${[...n.slice(0,-2),a].join(", ")} option`}function o(e){return"string"==typeof e?[e]:Array.isArray(e)?e:void 0}function r(e){return o(e)||[]}function s(e,n){return"object"!=typeof(a=n)||Array.isArray(a)||null===a?r(n):r(null==n?void 0:n[e]);var a}function g(e){return Array.from(new Set(e))}function l(e){return e?e.replace(/\/+$/,""):e}const c=(e,n)=>fetch(e,n);const u=(e=c)=>(n,a)=>{let t=(i=null==a?void 0:a.headers,Object.fromEntries(new Headers(i).entries()));var i;return t["x-api-key"]&&(t=Object.assign({"x-tolgee-sdk-type":"JS","x-tolgee-sdk-version":"prerelease"},t)),e(n,Object.assign(Object.assign({},a),{headers:t}))},d=(e,n)=>{let a=[];return{listen(e){const n=n=>{e(n)};return a.push(n),{unsubscribe(){a=a.filter((e=>n!==e))}}},emit(t){n()&&a.forEach((n=>n({type:e,value:t})))}}};function f(e){let n=[],a=[];function t(){if(0===a.length)return;const e=a;a=[],n.forEach((n=>{n(e)}))}return Object.freeze({listen(e){const a=n=>{e(n)};return n.push(a),{unsubscribe(){n=n.filter((e=>a!==e))}}},emit(n,i){e()&&e()&&(a.push(n),i?setTimeout(t,0):t())}})}class p extends Error{constructor(e,n,a=!1){const{language:t,namespace:i}=e;super(`Tolgee: Failed to fetch record for "${t}"${i&&` and "${i}"`}`),this.cause=n,this.isDev=a,this.name="RecordFetchError",this.language=t,this.namespace=i}}class v extends Error{constructor(e,n){super(e),this.cause=n,this.name="LanguageDetectorError"}}class h extends Error{constructor(e,n){super(e),this.cause=n,this.name="LanguageStorageError"}}const m=e=>{const n=new Map;return Object.entries(e).forEach((([e,a])=>{null!=a&&("object"!=typeof a?n.set(e,a):m(a).forEach(((a,t)=>{n.set(e+"."+t,a)})))})),n},b=e=>Object.fromEntries(m(e).entries()),y=e=>{const[n,...a]=e.split(":");return{language:n,namespace:a.join(":")||""}},L=({language:e,namespace:n})=>n?`${e}:${n}`:e;function O(e,a,t,i,o,s,l,c){const u=new Map,d=new Map;let f={},v=0;function h(n,a,t){const i=L(n);d.set(i,{data:b(a),version:t}),e.onCacheChange.emit(y(i))}async function m(i,o){let r;if(o)try{r=await t(i)}catch(n){const a=new p(i,n,!0);e.onError.emit(a),console.warn(a)}return r||(r=await async function(t){function i(n){const a=new p(t,n);throw e.onError.emit(a),console.error(a),a}const o=a(t);if(n(o)){const e=await o.catch(i);if(void 0!==e)return e}const r=f[L(t)];if("function"!=typeof r)return r;try{return await r()}catch(e){i(e)}}(i)),r}const O=Object.freeze({addStaticData(e){if(Array.isArray(e))for(const n of e){const e=L(n),a=d.get(e);a&&0!==a.version||h(n,b(n.data),0)}else e&&(f=Object.assign(Object.assign({},f),e),Object.entries(e).forEach((([e,n])=>{if("function"!=typeof n){const a=y(e),t=d.get(e);t&&0!==t.version||h(a,b(n),0)}})))},invalidate(){u.clear(),v+=1},addRecord(e,n){h(e,b(n),v)},exists(e,n=!1){const a=d.get(L(e));return a&&n?a.version===v:Boolean(a)},getRecord(e){const n=i(e),a=L(n),t=d.get(a);if(t)return Object.assign(Object.assign({},n),{cacheKey:a,data:t.data})},getAllRecords:()=>Array.from(d.entries()).map((([e])=>O.getRecord(y(e)))),getTranslation(e,n){var a;return null===(a=d.get(L(e)))||void 0===a?void 0:a.data[n]},getTranslationNs(e,n,a){var t;for(const i of e)for(const e of n){const n=null===(t=d.get(L({language:e,namespace:i})))||void 0===t?void 0:t.data[a];if(null!=n)return[i]}return g(e)},getTranslationFallback(e,n,a){var t;for(const i of e)for(const e of n){const n=null===(t=d.get(L({language:e,namespace:i})))||void 0===t?void 0:t.data[a];if(null!=n)return n}},changeTranslation(n,a,t){var i;const o=null===(i=d.get(L(n)))||void 0===i?void 0:i.data;(null==o?void 0:o[a])&&(o[a]=t,e.onCacheChange.emit(Object.assign(Object.assign({},n),{key:a})))},isFetching(e){if(o())return!0;if(void 0===e)return u.size>0;const n=r(e);return Boolean(Array.from(u.keys()).find((e=>n.includes(y(e).namespace))))},isLoading(e,n){const a=r(n);if(o())return!0;const t=Array.from(u.keys());return Boolean(t.find((n=>{const t=y(n);return(!a.length||a.includes(t.namespace))&&!O.exists({namespace:t.namespace,language:e})})))},async loadRecords(e,n){const a=e.map((e=>{const a=i(e),t=L(a);if(null==n?void 0:n.useCache){if(O.exists(a,!0))return Object.assign(Object.assign({},a),{new:!1,cacheKey:t,data:O.getRecord(a).data})}const o=u.get(t);if(o)return Object.assign(Object.assign({},a),{new:!1,promise:o,cacheKey:t});const r=m(a,!(null==n?void 0:n.noDev))||Promise.resolve(void 0);return u.set(t,r),Object.assign(Object.assign({},a),{new:!0,promise:r,cacheKey:t})}));l.notify(),c.notify();const t=a.map((e=>e.promise)).filter(Boolean),o=await Promise.all(t);return a.forEach((e=>{var n;e.promise&&(e.data=b(null!==(n=o[0])&&void 0!==n?n:{}),o.shift());const a=u.get(e.cacheKey)!==e.promise;e.new&&!a&&(u.delete(e.cacheKey),e.data?O.addRecord(e,e.data):O.getRecord(e)||O.addRecord(e,{}))})),l.notify(),c.notify(),a.map((e=>{var n;return{language:e.language,namespace:e.namespace,data:null!==(n=e.data)&&void 0!==n?n:{},cacheKey:e.cacheKey}}))}});return O}function j(e,n){var a={};for(var t in e)Object.prototype.hasOwnProperty.call(e,t)&&n.indexOf(t)<0&&(a[t]=e[t]);if(null!=e&&"function"==typeof Object.getOwnPropertySymbols){var i=0;for(t=Object.getOwnPropertySymbols(e);i<t.length;i++)n.indexOf(t[i])<0&&Object.prototype.propertyIsEnumerable.call(e,t[i])&&(a[t[i]]=e[t[i]])}return a}const w="invalid",R={observerOptions:{tagAttributes:{textarea:["placeholder"],input:["value","placeholder"],img:["alt"],"*":["aria-label","title"]},restrictedElements:["script","style"],highlightKeys:["Alt"],highlightColor:"rgb(255, 0, 0)",highlightWidth:5,inputPrefix:"%-%tolgee:",inputSuffix:"%-%",passToParent:["option","optgroup"],fullKeyEncode:!1},observerType:"invisible",onFormatError:w,apiUrl:"https://app.tolgee.io",autoLoadRequiredData:!0,fetch:u(),onTranslationMissing:({key:e})=>e,disableCache:!1},k=(...e)=>{let n={};return e.forEach((e=>{n=Object.assign(Object.assign(Object.assign({},n),e),{observerOptions:Object.assign(Object.assign({},n.observerOptions),null==e?void 0:e.observerOptions)})})),n};function E(e,n){const a=k(R,null==n?void 0:n.initialOptions,e);return a.apiUrl=l(a.apiUrl),(null==e?void 0:e.fetch)&&(a.fetch=u(e.fetch)),{initialOptions:a,activeNamespaces:(null==n?void 0:n.activeNamespaces)||new Map,language:null==n?void 0:n.language,pendingLanguage:null==n?void 0:n.language,isInitialLoading:!1,isRunning:!1}}function D(e,n,i,o,r,s,g,l){const c={ui:void 0},u={formatters:[],finalFormatter:void 0,observer:void 0,devBackend:void 0,backends:[],ui:void 0,languageDetector:void 0,languageStorage:void 0},d=async({keysAndDefaults:e,target:n})=>{var a;const t=e.map((({key:e,ns:n,defaultValue:a})=>({key:e,defaultValue:a,fallbackNamespaces:o(n),namespace:r({key:e,ns:n})[0],translation:s({key:e,ns:n})})));null===(a=u.ui)||void 0===a||a.handleElementClick(t,n)},f=(e,n)=>{var a;return(null===(a=u.observer)||void 0===a?void 0:a.findPositions(e,n))||[]};function p(e){const n=s({key:e.key,ns:e.ns});return A.formatTranslation(Object.assign(Object.assign({},e),{translation:n,formatEnabled:!0}))}function m(){return{fetch:n().fetch}}function b(e){u.observer=null==e?void 0:e()}function y(){return Boolean(u.observer)}function L(e){e&&u.formatters.push(e)}function O(e){u.finalFormatter=e}function R(e){c.ui=e}function k(){return Boolean(c.ui)}function E(e){u.languageStorage=e}function D(e){u.languageDetector=e}function C(e){e&&u.backends.push(e)}function N(e){u.devBackend=e}const A=Object.freeze({addPlugin:function(e,n){n(e,Object.freeze({setFinalFormatter:O,addFormatter:L,setObserver:b,hasObserver:y,setUi:R,hasUi:k,setDevBackend:N,addBackend:C,setLanguageDetector:D,setLanguageStorage:E}))},findPositions:f,run(){var e,a;const{apiKey:t,apiUrl:i,projectId:o,observerOptions:r,tagNewKeys:s,filterTag:v}=n();u.ui=null===(e=c.ui)||void 0===e?void 0:e.call(c,{apiKey:t,apiUrl:i,projectId:o,highlight:A.highlight,changeTranslation:g,findPositions:f,onPermanentChange:e=>l.onPermanentChange.emit(e),tagNewKeys:s,filterTag:v}),null===(a=u.observer)||void 0===a||a.run({mouseHighlight:!0,options:r,translate:p,onClick:d})},stop(){var e;u.ui=void 0,null===(e=u.observer)||void 0===e||e.stop()},getLanguageStorage:()=>u.languageStorage,getInitialLanguage(){const e=i();return a(t(l.onError,(e=>new h("Tolgee: Failed to load language",e)),(()=>{var e;return null===(e=u.languageStorage)||void 0===e?void 0:e.getLanguage(m())})),(n=>e&&!e.includes(n)||!n?function(){if(!u.languageDetector)return;const e=i();return t(l.onError,(e=>new v("Tolgee: Failed to detect language",e)),(()=>{var n;return null===(n=u.languageDetector)||void 0===n?void 0:n.getLanguage(Object.assign({availableLanguages:e},m()))}))}():n))},setStoredLanguage:e=>t(l.onError,(e=>new h("Tolgee: Failed to store language",e)),(()=>{var n;return null===(n=u.languageStorage)||void 0===n?void 0:n.setLanguage(e,m())})),getDevBackend:()=>u.devBackend,getBackendRecord:async({language:e,namespace:n})=>{for(const a of u.backends){const t=await a.getRecord(Object.assign({language:e,namespace:n},m()));if(void 0!==t)return t}},getBackendDevRecord:async({language:e,namespace:a})=>{var t;const{apiKey:i,apiUrl:o,projectId:r,filterTag:s}=n();if(i&&o&&A.hasDevBackend())return null===(t=u.devBackend)||void 0===t?void 0:t.getRecord(Object.assign({apiKey:i,apiUrl:o,projectId:r,language:e,namespace:a,filterTag:s},m()))},getLanguageDetector:()=>u.languageDetector,retranslate(){var e;null===(e=u.observer)||void 0===e||e.retranslate()},highlight:(e,n)=>{var a,t;return(null===(t=null===(a=u.observer)||void 0===a?void 0:a.highlight)||void 0===t?void 0:t.call(a,e,n))||{unhighlight(){}}},unwrap(e){var n;return u.observer?null===(n=u.observer)||void 0===n?void 0:n.unwrap(e):{text:e,keys:[]}},wrap(e){var n;return u.observer?null===(n=u.observer)||void 0===n?void 0:n.wrap(e):e.translation},hasDevBackend:()=>Boolean(A.getDevBackend()),formatTranslation(a){var t,{formatEnabled:i}=a,o=j(a,["formatEnabled"]);const{key:r,translation:s,defaultValue:g,noWrap:l,params:c,ns:d,orEmpty:f}=o,p=null!=s?s:g;let v="";null==s&&(v=n().onTranslationMissing(o));let h=null!=p?p:f?"":v;const m=e(),b=i||!(null===(t=u.observer)||void 0===t?void 0:t.outputNotFormattable),y=e=>u.observer&&!l?u.observer.wrap({key:r,translation:e,defaultValue:g,params:c,ns:d}):e;h=y(h);try{if(p&&m&&b)for(const e of u.formatters)h=e.format({translation:h,language:m,params:c});u.finalFormatter&&p&&m&&b&&(h=u.finalFormatter.format({translation:h,language:m,params:c}))}catch(e){console.error(e);const a=("string"==typeof(L=e)?L:"string"==typeof(null==L?void 0:L.message)?L.message:void 0)||w,t=n().onFormatError,i=typeof t;h="string"===i?t:"function"===i?t(a,o):w,h=y(h)}var L;return h}});return A}const C=(e,n,a)=>{let t=e;return Object.freeze({init(e){t=e},notify(){const e=n();t!==e&&a(e),t=e}})};const N=(e,...n)=>{let a,t={};return"object"==typeof e?t=e:(t.key=e,"string"==typeof n[0]?(t.defaultValue=n[0],a=n[1]):"object"==typeof n[0]&&(a=n[0])),a&&(t=Object.assign(Object.assign({},function(e){var{ns:n,noWrap:a,orEmpty:t,params:i,language:o}=e,r=j(e,["ns","noWrap","orEmpty","params","language"]);const s={ns:n,noWrap:a,orEmpty:t,language:o};return Object.assign(Object.assign({},s),{params:Object.assign({},r)})}(a)),t)),t};function A({options:e}){const t=function(){let e=!0;function n(){return e}const a=Object.freeze({onPendingLanguageChange:d("pendingLanguage",n),onLanguageChange:d("language",n),onLoadingChange:d("loading",n),onFetchingChange:d("fetching",n),onInitialLoaded:d("initialLoad",n),onRunningChange:d("running",n),onCacheChange:d("cache",n),onPermanentChange:d("permanentChange",n),onError:d("error",n),onUpdate:f(n),setEmitterActive(n){e=n},on:(e,n)=>{switch(e){case"pendingLanguage":return a.onPendingLanguageChange.listen(n);case"language":return a.onLanguageChange.listen(n);case"loading":return a.onLoadingChange.listen(n);case"fetching":return a.onFetchingChange.listen(n);case"initialLoad":return a.onInitialLoaded.listen(n);case"running":return a.onRunningChange.listen(n);case"cache":return a.onCacheChange.listen(n);case"update":return a.onUpdate.listen(n);case"permanentChange":return a.onPermanentChange.listen(n);case"error":return a.onError.listen(n)}}});return a.onInitialLoaded.listen((e=>a.onUpdate.emit(e,!1))),a.onLanguageChange.listen((e=>a.onUpdate.emit(e,!1))),a.onCacheChange.listen((e=>a.onUpdate.emit(e,!0))),a}(),o=C(!1,(()=>v.isFetching()),t.onFetchingChange.emit),c=C(!1,(()=>T.isLoading()),t.onLoadingChange.emit),u=function(e,n,a){let t,i=E();const o=Object.freeze({init(e){i=E(e,i)},isRunning:()=>i.isRunning,setRunning(e){i.isRunning!==e&&(i.isRunning=e,a.emit(e))},isInitialLoading:()=>i.isInitialLoading,isCacheDisabled:()=>i.initialOptions.disableCache,setInitialLoading(e){i.isInitialLoading=e},getLanguage:()=>i.language||i.initialOptions.language,setLanguage(n){i.language!==n&&(i.language=n,e.emit(n))},getPendingLanguage:()=>i.pendingLanguage||o.getLanguage(),setPendingLanguage(e){i.pendingLanguage!==e&&(i.pendingLanguage=e,n.emit(e))},getInitialOptions:()=>Object.assign(Object.assign({},i.initialOptions),t),addActiveNs(e){r(e).forEach((e=>{const n=i.activeNamespaces.get(e);void 0!==n?i.activeNamespaces.set(e,n+1):i.activeNamespaces.set(e,1)}))},removeActiveNs(e){r(e).forEach((e=>{const n=i.activeNamespaces.get(e);void 0!==n&&n>1?i.activeNamespaces.set(e,n-1):i.activeNamespaces.delete(e)}))},getRequiredNamespaces:()=>g([o.getDefaultNs(),...i.initialOptions.ns||[],...r(i.initialOptions.fallbackNs),...i.activeNamespaces.keys()]),getFallbackLangs(e){const n=e||o.getLanguage();return n?g([n,...s(n,i.initialOptions.fallbackLanguage)]):[]},getFallbackNs:()=>r(i.initialOptions.fallbackNs),getNs(){var e,n;return(null===(e=i.initialOptions.ns)||void 0===e?void 0:e.length)?i.initialOptions.ns:[null!==(n=i.initialOptions.defaultNs)&&void 0!==n?n:""]},getDefaultNs(e){var n,a,t;return void 0===e?null!==(t=null!==(n=i.initialOptions.defaultNs)&&void 0!==n?n:null===(a=i.initialOptions.ns)||void 0===a?void 0:a[0])&&void 0!==t?t:"":e},getAvailableLanguages(){if(i.initialOptions.availableLanguages)return i.initialOptions.availableLanguages;if(i.initialOptions.staticData){const e=Object.keys(i.initialOptions.staticData).map((e=>y(e).language));return Array.from(new Set(e))}},getAvailableNs:()=>i.initialOptions.availableNs,withDefaultNs:e=>({namespace:void 0===e.namespace?o.getDefaultNs():e.namespace,language:e.language}),overrideCredentials(e){t=e?Object.assign(Object.assign({},e),{apiUrl:l(e.apiUrl)}):void 0}});return o}(t.onLanguageChange,t.onPendingLanguageChange,t.onRunningChange),p=D(u.getLanguage,u.getInitialOptions,u.getAvailableLanguages,b,k,A,j,t),v=O(t,p.getBackendRecord,p.getBackendDevRecord,u.withDefaultNs,u.isInitialLoading,u.isCacheDisabled,o,c);let h;function m(e){return u.getDefaultNs(e)}function b(e){return g([...r(m(e)),...u.getFallbackNs()])}function L(e){return g([...r(null!=e?e:m()),...u.getRequiredNamespaces()])}function j(e,n,a){const t=u.withDefaultNs(e),i=v.getTranslation(t,n);return v.changeTranslation(t,n,a),{revert(){v.changeTranslation(t,n,i)}}}function w(e){u.init(e),v.addStaticData(u.getInitialOptions().staticData)}function R(e,n){const a=u.getFallbackLangs(e),t=L(n),i=[];return a.forEach((e=>{t.forEach((n=>{i.push({language:e,namespace:n})}))})),i}function k({key:e,ns:n}){const a=u.getFallbackLangs(),t=b(null!=n?n:void 0);return v.getTranslationNs(t,a,e)}function A({key:e,ns:n,language:a}){const t=b(null!=n?n:void 0),i=u.getFallbackLangs(a);return v.getTranslationFallback(t,i,e)}function F(){const e=a(P(),(()=>{const e=R(n,a).filter((e=>!v.exists(e,!0)));var n,a;if(e.length&&u.getInitialOptions().autoLoadRequiredData)return v.loadRecords(e,{useCache:!0})}));if(n(e))return u.setInitialLoading(!0),o.notify(),c.notify(),Promise.resolve(e).then((()=>{u.setInitialLoading(!1),o.notify(),c.notify(),t.onInitialLoaded.emit()}));t.onInitialLoaded.emit()}function P(){if(u.getLanguage())return;return a(p.getInitialLanguage(),(e=>{const n=e||u.getInitialOptions().defaultLanguage;n&&u.setLanguage(n)}))}e&&w(e),t.onUpdate.listen((()=>{u.isRunning()&&p.retranslate()}));const T=Object.freeze(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign({},t),u),p),v),{init:w,getTranslation:A,changeTranslation:j,getTranslationNs:k,getDefaultAndFallbackNs:b,findPositions:p.findPositions,getRequiredDescriptors:R,async changeLanguage(e){u.getPendingLanguage()===e&&u.getLanguage()===e||(u.setPendingLanguage(e),u.isRunning()&&u.getInitialOptions().autoLoadRequiredData&&await v.loadRecords(R(e),{useCache:!0}),e===u.getPendingLanguage()&&(u.setLanguage(e),await p.setStoredLanguage(e)))},async addActiveNs(e,n){n||u.addActiveNs(e),u.isRunning()&&await v.loadRecords(R(void 0,e),{useCache:!0})},async loadRecord(e,n){var a;return null===(a=(await T.loadRecords([e],n))[0])||void 0===a?void 0:a.data},isLoading:e=>v.isLoading(u.getLanguage(),e),isLoaded(e){const n=u.getLanguage();if(!n)return!1;const a=u.getFallbackLangs(n),t=L(e),i=[];return a.forEach((e=>{t.forEach((n=>{v.exists({language:e,namespace:n})||i.push({language:e,namespace:n})}))})),0===i.length},t:(...e)=>{const n=N(...e),a=A(n);return p.formatTranslation(Object.assign(Object.assign({},n),{translation:a}))},isDev:()=>Boolean(u.getInitialOptions().apiKey&&u.getInitialOptions().apiUrl),async loadRequired(e){(null==e?void 0:e.language)||await P();const n=R(null==e?void 0:e.language);return T.loadRecords(n,e)},async loadMatrix(e){const n=function(e){let n=[],a=[];if(Array.isArray(e.languages))n=e.languages;else if("all"===e.languages){const e=T.getAvailableLanguages();if(!e)throw new Error(i("availableLanguages"));n=e}if(Array.isArray(e.namespaces))a=e.namespaces;else if("all"===e.namespaces){const e=T.getAvailableNs();if(!e)throw new Error(i("availableNs"));a=e}const t=[];return n.forEach((e=>{a.forEach((n=>{t.push({language:e,namespace:n})}))})),t}(e);return T.loadRecords(n,e)},run:()=>(function(){if((p.getLanguageDetector()||p.getLanguageStorage())&&!u.getAvailableLanguages())throw new Error(i("availableLanguages"));if(!u.getLanguage()&&!u.getInitialOptions().defaultLanguage)throw new Error(i(["defaultLanguage","language"]))}(),u.isRunning()||(u.setRunning(!0),p.run(),h=F()),Promise.resolve(h)),stop(){u.isRunning()&&(p.stop(),u.setRunning(!1))}}));return T}const F=0,P=1,T=2;class I extends Error{constructor(e,n,a){let t,i="";e===F?t="Empty parameter":e===P?(t="Unexpected character",i="Did you forget to use FormatIcu to render ICU message syntax?"):t="Unexpected end",super(`Tolgee parser: ${t} at ${n} in "${a}"`+(i?"\n"+i:"")),this.code=e,this.index=n}}function x(e){return/\s/.test(e)}const S=0,B=1,U=2,K=3,$=4,z=new Set([U,B,S]),q="'",M=new Set(["{","}",q]),V=e=>/[0-9a-zA-Z_]/.test(e);function W(e,n){const[a,t]=function(e){let n=S,a="",t="",i="";const o=[],r=[];let s=0;function g(n){throw new I(n,s,e)}const l=()=>{o.push(a),a=""},c=()=>{""===t&&g(F),r.push(t),t=""};for(s=0;s<e.length;s++)switch(i=e[s],n){case S:i===q?(a+=i,n=B):"{"===i?(l(),n=K):(a+=i,n=S);break;case B:M.has(i)?(a=a.slice(0,-1)+i,n=U):(a+=i,n=S);break;case U:i===q?n=S:(a+=i,n=U);break;case K:"}"===i?(c(),n=S):x(i)?""!==t&&(c(),n=$):(V(i)||g(P),t+=i,n=K);break;case $:"}"==i?n=S:x(i)?n=$:g(P)}return z.has(n)||g(T),l(),[o,r]}(e),i=[a[0]];for(let o=1;o<a.length;o++){const r=null==n?void 0:n[t[o-1]];if(void 0===r)throw new Error(`Missing parameter "${t[o-1]}" in "${e}"`);i.push(String(r)),i.push(a[o])}return i.join("")}e.FormatSimple=()=>(e,n)=>(n.setFinalFormatter({format:({translation:e,params:n})=>W(e,n)}),e),e.LanguageDetectorError=v,e.LanguageStorageError=h,e.RecordFetchError=p,e.TolgeeCore=()=>{const e={plugins:[],options:{}},n=Object.freeze({use:a=>(e.plugins.push(a),n),updateDefaults:a=>(e.options=k(e.options,a),n),init(n){const a=function(e){const n=A({options:e});function a(e){const a=n.isRunning();a&&n.stop(),e(),n.isDev()&&n.invalidate(),a&&n.run()}n.isDev()&&n.invalidate();const t=Object.freeze({on:n.on,setEmitterActive:n.setEmitterActive,getLanguage:n.getLanguage,getPendingLanguage:n.getPendingLanguage,changeLanguage:n.changeLanguage,changeTranslation:n.changeTranslation,addActiveNs:n.addActiveNs,removeActiveNs:n.removeActiveNs,loadRequired:n.loadRequired,loadMatrix:n.loadMatrix,loadRecords:n.loadRecords,loadRecord:n.loadRecord,addStaticData:n.addStaticData,getRecord:n.getRecord,getAllRecords:n.getAllRecords,isLoaded:n.isLoaded,getRequiredDescriptors:n.getRequiredDescriptors,isInitialLoading:n.isInitialLoading,isLoading:n.isLoading,isFetching:n.isFetching,isRunning:n.isRunning,run:n.run,stop:n.stop,t:n.t,highlight:n.highlight,findPositions:n.findPositions,getInitialOptions:n.getInitialOptions,isDev:n.isDev,wrap:n.wrap,unwrap:n.unwrap,overrideCredentials(e){a((()=>n.overrideCredentials(e)))},addPlugin(e){e&&a((()=>n.addPlugin(t,e)))},updateOptions(e){e&&a((()=>n.init(e)))}});return t}(k(e.options,n));return e.plugins.forEach(a.addPlugin),a}});return n},e.createFetchFunction=u,e.getFallback=o,e.getFallbackArray=r,e.getTranslateProps=N,Object.defineProperty(e,"__esModule",{value:!0})}));
//# sourceMappingURL=tolgee.umd.min.js.map

@@ -1,11 +0,12 @@

import { CacheDescriptor, CacheDescriptorInternal, NsFallback, TranslationsFlat, TranslationValue, TreeTranslationsData, BackendGetRecordInternal } from '../../types';
import { TolgeeStaticData } from '../State/initState';
import { CacheDescriptor, CacheDescriptorInternal, NsFallback, TranslationValue, TreeTranslationsData, BackendGetRecordInternal, LoadOptions, CacheInternalRecord } from '../../types';
import { TolgeeStaticDataProp } from '../State/initState';
import { ValueObserverInstance } from '../ValueObserver';
import { EventsInstance } from '../Events/Events';
export declare function Cache(events: EventsInstance, backendGetRecord: BackendGetRecordInternal, backendGetDevRecord: BackendGetRecordInternal, withDefaultNs: (descriptor: CacheDescriptor) => CacheDescriptorInternal, isInitialLoading: () => boolean, fetchingObserver: ValueObserverInstance<boolean>, loadingObserver: ValueObserverInstance<boolean>): Readonly<{
addStaticData(data: TolgeeStaticData | undefined): void;
export declare function Cache(events: EventsInstance, backendGetRecord: BackendGetRecordInternal, backendGetDevRecord: BackendGetRecordInternal, withDefaultNs: (descriptor: CacheDescriptor) => CacheDescriptorInternal, isInitialLoading: () => boolean, isCacheDisabled: () => boolean, fetchingObserver: ValueObserverInstance<boolean>, loadingObserver: ValueObserverInstance<boolean>): Readonly<{
addStaticData(data: TolgeeStaticDataProp | undefined): void;
invalidate(): void;
addRecord(descriptor: CacheDescriptorInternal, data: TreeTranslationsData): void;
exists(descriptor: CacheDescriptorInternal, strict?: boolean): boolean;
getRecord(descriptor: CacheDescriptor): TranslationsFlat | undefined;
getRecord(descriptor: CacheDescriptor): CacheInternalRecord | undefined;
getAllRecords(): (CacheInternalRecord | undefined)[];
getTranslation(descriptor: CacheDescriptorInternal, key: string): TranslationValue;

@@ -16,10 +17,5 @@ getTranslationNs(namespaces: string[], languages: string[], key: string): string[];

isFetching(ns?: NsFallback): boolean;
isLoading(language: string | undefined, ns?: NsFallback): boolean;
loadRecords(descriptors: CacheDescriptor[], isDev: boolean): Promise<TranslationsFlat[]>;
getAllRecords(): {
data: TranslationsFlat;
language: string;
namespace: string;
}[];
isLoading(language: string, ns?: NsFallback): boolean;
loadRecords(descriptors: CacheDescriptor[], options?: LoadOptions): Promise<CacheInternalRecord[]>;
}>;
export type CacheInstance = ReturnType<typeof Cache>;

@@ -1,4 +0,5 @@

import { CacheDescriptorInternal, TreeTranslationsData } from '../../types';
export declare const flattenTranslations: (data: TreeTranslationsData) => Map<string, string>;
import { CacheDescriptorInternal, TranslationsFlat, TreeTranslationsData } from '../../types';
export declare const flattenTranslationsToMap: (data: TreeTranslationsData) => Map<string, string>;
export declare const flattenTranslations: (data: TreeTranslationsData) => TranslationsFlat;
export declare const decodeCacheKey: (key: string) => CacheDescriptorInternal;
export declare const encodeCacheKey: ({ language, namespace, }: CacheDescriptorInternal) => string;

@@ -1,2 +0,2 @@

import { CacheDescriptor, NsFallback, TolgeeOptions, TFnType, NsType, KeyAndNamespacesInternal } from '../types';
import { CacheDescriptor, NsFallback, TolgeeOptions, TFnType, NsType, KeyAndNamespacesInternal, CacheDescriptorInternal, LoadOptions, LoadRequiredOptions, LoadMatrixOptions } from '../types';
type StateServiceProps = {

@@ -14,7 +14,6 @@ options?: Partial<TolgeeOptions>;

findPositions: import("../types").FindPositionsInterface;
getRequiredRecords: (lang?: string, ns?: NsFallback) => CacheDescriptor[];
getRequiredDescriptors: (lang?: string, ns?: NsFallback) => CacheDescriptorInternal[];
changeLanguage(language: string): Promise<void>;
addActiveNs(ns: NsFallback, forget?: boolean): Promise<void>;
loadRecords(descriptors: CacheDescriptor[]): Promise<import("../types").TranslationsFlat[]>;
loadRecord(descriptor: CacheDescriptor): Promise<import("../types").TranslationsFlat>;
loadRecord(descriptor: CacheDescriptor, options?: LoadOptions): Promise<import("../types").TranslationsFlat>;
isLoading(ns?: NsFallback): boolean;

@@ -24,16 +23,15 @@ isLoaded(ns?: NsFallback): boolean;

isDev(): boolean;
loadRequired(options?: LoadRequiredOptions): Promise<import("../types").CacheInternalRecord[]>;
loadMatrix(options: LoadMatrixOptions): Promise<import("../types").CacheInternalRecord[]>;
run(): Promise<any>;
stop(): void;
addStaticData: (data: import("./State/initState").TolgeeStaticData | undefined) => void;
addStaticData: (data: import("./State/initState").TolgeeStaticDataProp | undefined) => void;
invalidate: () => void;
addRecord: (descriptor: import("../types").CacheDescriptorInternal, data: import("../types").TreeTranslationsData) => void;
exists: (descriptor: import("../types").CacheDescriptorInternal, strict?: boolean) => boolean;
getRecord: (descriptor: CacheDescriptor) => import("../types").TranslationsFlat | undefined;
addRecord: (descriptor: CacheDescriptorInternal, data: import("../types").TreeTranslationsData) => void;
exists: (descriptor: CacheDescriptorInternal, strict?: boolean) => boolean;
getRecord: (descriptor: CacheDescriptor) => import("../types").CacheInternalRecord | undefined;
getAllRecords: () => (import("../types").CacheInternalRecord | undefined)[];
getTranslationFallback: (namespaces: string[], languages: string[], key: string) => string | undefined;
isFetching: (ns?: NsFallback) => boolean;
getAllRecords: () => {
data: import("../types").TranslationsFlat;
language: string;
namespace: string;
}[];
loadRecords: (descriptors: CacheDescriptor[], options?: LoadOptions | undefined) => Promise<import("../types").CacheInternalRecord[]>;
addPlugin: (tolgeeInstance: import("../TolgeeCore").TolgeeInstance, plugin: import("../types").TolgeePlugin) => void;

@@ -64,2 +62,3 @@ getLanguageStorage: () => import("../types").LanguageStorageMiddleware | undefined;

isInitialLoading: () => boolean;
isCacheDisabled: () => boolean;
setInitialLoading: (value: boolean) => void;

@@ -80,4 +79,5 @@ getLanguage: () => string | undefined;

fallbackNs?: import("../types").FallbackGeneral;
defaultNs: string;
staticData?: import("./State/initState").TolgeeStaticData | undefined;
defaultNs?: string | undefined;
availableNs?: string[] | undefined;
staticData?: import("./State/initState").TolgeeStaticDataProp | undefined;
observerType: "invisible" | "text";

@@ -90,2 +90,4 @@ observerOptions: import("./State/observerOptions").ObserverOptionsInternal;

filterTag?: string[] | undefined;
autoLoadRequiredData: boolean;
disableCache: boolean;
};

@@ -96,16 +98,45 @@ removeActiveNs: (ns: NsFallback) => void;

getFallbackNs: () => string[];
getNs: () => string[];
getDefaultNs: (ns?: string | undefined) => string;
getAvailableLanguages: () => string[] | undefined;
withDefaultNs: (descriptor: CacheDescriptor) => import("../types").CacheDescriptorInternal;
getAvailableNs: () => string[] | undefined;
withDefaultNs: (descriptor: CacheDescriptor) => CacheDescriptorInternal;
overrideCredentials: (credentials: import("../types").DevCredentials) => void;
onPendingLanguageChange: import("./Events/EventEmitter").EventEmitterInstance<string>;
onLanguageChange: import("./Events/EventEmitter").EventEmitterInstance<string>;
onLoadingChange: import("./Events/EventEmitter").EventEmitterInstance<boolean>;
onFetchingChange: import("./Events/EventEmitter").EventEmitterInstance<boolean>;
onInitialLoaded: import("./Events/EventEmitter").EventEmitterInstance<void>;
onRunningChange: import("./Events/EventEmitter").EventEmitterInstance<boolean>;
onCacheChange: import("./Events/EventEmitter").EventEmitterInstance<import("../types").CacheDescriptorWithKey>;
onUpdate: import("./Events/EventEmitterSelective").EventEmitterSelectiveInstance;
onPermanentChange: import("./Events/EventEmitter").EventEmitterInstance<import("../types").TranslationDescriptor>;
onError: import("./Events/EventEmitter").EventEmitterInstance<import("../types").TolgeeError>;
onPendingLanguageChange: {
readonly listen: (handler: import("../types").Handler<import("../types").ListenerEvent<"pendingLanguage", string>>) => import("../types").Subscription;
readonly emit: (data: string) => void;
};
onLanguageChange: {
readonly listen: (handler: import("../types").Handler<import("../types").ListenerEvent<"language", string>>) => import("../types").Subscription;
readonly emit: (data: string) => void;
};
onLoadingChange: {
readonly listen: (handler: import("../types").Handler<import("../types").ListenerEvent<"loading", boolean>>) => import("../types").Subscription;
readonly emit: (data: boolean) => void;
};
onFetchingChange: {
readonly listen: (handler: import("../types").Handler<import("../types").ListenerEvent<"fetching", boolean>>) => import("../types").Subscription;
readonly emit: (data: boolean) => void;
};
onInitialLoaded: {
readonly listen: (handler: import("../types").Handler<import("../types").ListenerEvent<"initialLoad", void>>) => import("../types").Subscription;
readonly emit: (data: void) => void;
};
onRunningChange: {
readonly listen: (handler: import("../types").Handler<import("../types").ListenerEvent<"running", boolean>>) => import("../types").Subscription;
readonly emit: (data: boolean) => void;
};
onCacheChange: {
readonly listen: (handler: import("../types").Handler<import("../types").ListenerEvent<"cache", import("../types").CacheDescriptorWithKey>>) => import("../types").Subscription;
readonly emit: (data: import("../types").CacheDescriptorWithKey) => void;
};
onPermanentChange: {
readonly listen: (handler: import("../types").Handler<import("../types").ListenerEvent<"permanentChange", import("../types").TranslationDescriptor>>) => import("../types").Subscription;
readonly emit: (data: import("../types").TranslationDescriptor) => void;
};
onError: {
readonly listen: (handler: import("../types").Handler<import("../types").ListenerEvent<"error", import("../types").TolgeeError>>) => import("../types").Subscription;
readonly emit: (data: import("../types").TolgeeError) => void;
};
onUpdate: import("./Events/EventEmitterCombined").EventEmitterCombinedInstance<import("../types").UpdateEvent>;
setEmitterActive: (active: boolean) => void;

@@ -112,0 +143,0 @@ on: import("../types").TolgeeOn<keyof import("../types").EventType>;

@@ -1,6 +0,6 @@

import { Subscription, Listener } from '../../types';
export declare function EventEmitter<T>(isActive: () => boolean): EventEmitterInstance<T>;
export type EventEmitterInstance<T> = {
readonly listen: (handler: Listener<T>) => Subscription;
import { Subscription, Handler, ListenerEvent } from '../../types';
export declare const EventEmitter: <Event_1 extends ListenerEvent<string, any>>(type: Event_1["type"], isActive: () => boolean) => EventEmitterInstance<Event_1>;
export type EventEmitterInstance<Event> = Event extends ListenerEvent<infer E, infer T> ? {
readonly listen: (handler: Handler<ListenerEvent<E, T>>) => Subscription;
readonly emit: (data: T) => void;
};
} : never;

@@ -1,13 +0,40 @@

import { CacheDescriptorWithKey, TolgeeError, TolgeeOn, TranslationDescriptor } from '../../types';
export declare function Events(getFallbackNs: () => string[], getDefaultNs: () => string): Readonly<{
onPendingLanguageChange: import("./EventEmitter").EventEmitterInstance<string>;
onLanguageChange: import("./EventEmitter").EventEmitterInstance<string>;
onLoadingChange: import("./EventEmitter").EventEmitterInstance<boolean>;
onFetchingChange: import("./EventEmitter").EventEmitterInstance<boolean>;
onInitialLoaded: import("./EventEmitter").EventEmitterInstance<void>;
onRunningChange: import("./EventEmitter").EventEmitterInstance<boolean>;
onCacheChange: import("./EventEmitter").EventEmitterInstance<CacheDescriptorWithKey>;
onUpdate: import("./EventEmitterSelective").EventEmitterSelectiveInstance;
onPermanentChange: import("./EventEmitter").EventEmitterInstance<TranslationDescriptor>;
onError: import("./EventEmitter").EventEmitterInstance<TolgeeError>;
import { TolgeeOn, UpdateEvent } from '../../types';
export declare function Events(): Readonly<{
onPendingLanguageChange: {
readonly listen: (handler: import("../../types").Handler<import("../../types").ListenerEvent<"pendingLanguage", string>>) => import("../../types").Subscription;
readonly emit: (data: string) => void;
};
onLanguageChange: {
readonly listen: (handler: import("../../types").Handler<import("../../types").ListenerEvent<"language", string>>) => import("../../types").Subscription;
readonly emit: (data: string) => void;
};
onLoadingChange: {
readonly listen: (handler: import("../../types").Handler<import("../../types").ListenerEvent<"loading", boolean>>) => import("../../types").Subscription;
readonly emit: (data: boolean) => void;
};
onFetchingChange: {
readonly listen: (handler: import("../../types").Handler<import("../../types").ListenerEvent<"fetching", boolean>>) => import("../../types").Subscription;
readonly emit: (data: boolean) => void;
};
onInitialLoaded: {
readonly listen: (handler: import("../../types").Handler<import("../../types").ListenerEvent<"initialLoad", void>>) => import("../../types").Subscription;
readonly emit: (data: void) => void;
};
onRunningChange: {
readonly listen: (handler: import("../../types").Handler<import("../../types").ListenerEvent<"running", boolean>>) => import("../../types").Subscription;
readonly emit: (data: boolean) => void;
};
onCacheChange: {
readonly listen: (handler: import("../../types").Handler<import("../../types").ListenerEvent<"cache", import("../../types").CacheDescriptorWithKey>>) => import("../../types").Subscription;
readonly emit: (data: import("../../types").CacheDescriptorWithKey) => void;
};
onPermanentChange: {
readonly listen: (handler: import("../../types").Handler<import("../../types").ListenerEvent<"permanentChange", import("../../types").TranslationDescriptor>>) => import("../../types").Subscription;
readonly emit: (data: import("../../types").TranslationDescriptor) => void;
};
onError: {
readonly listen: (handler: import("../../types").Handler<import("../../types").ListenerEvent<"error", import("../../types").TolgeeError>>) => import("../../types").Subscription;
readonly emit: (data: import("../../types").TolgeeError) => void;
};
onUpdate: import("./EventEmitterCombined").EventEmitterCombinedInstance<UpdateEvent>;
setEmitterActive(active: boolean): void;

@@ -14,0 +41,0 @@ on: TolgeeOn<keyof import("../../types").EventType>;

@@ -1,2 +0,2 @@

import { FallbackGeneral, FallbackLanguageOption, TreeTranslationsData, OnFormatError, FetchFn, MissingTranslationHandler } from '../../types';
import { FallbackGeneral, FallbackLanguageOption, TreeTranslationsData, OnFormatError, FetchFn, MissingTranslationHandler, CachePublicRecord } from '../../types';
import { ObserverOptions, ObserverOptionsInternal } from './observerOptions';

@@ -9,2 +9,3 @@ export declare const DEFAULT_FORMAT_ERROR = "invalid";

};
export type TolgeeStaticDataProp = TolgeeStaticData | CachePublicRecord[];
export type TolgeeOptionsInternal = {

@@ -32,4 +33,4 @@ /**

/**
* Languages which can be used for language detection
* and also limits which values can be stored
* Specify all available languages. Required for language detection or loading all languages at once (loadMatrix).
* It also limits which values can be stored. Is derrived from `staticData` keys if not provided.
*/

@@ -42,3 +43,3 @@ availableLanguages?: string[];

/**
* Namespaces which should be always fetched
* Namespaces which should be always fetched (default: [defaultNs] or [''])
*/

@@ -51,6 +52,10 @@ ns?: string[];

/**
* Default namespace when no namespace defined (default: '')
* Default namespace when no namespace defined (default: first from `ns`)
*/
defaultNs: string;
defaultNs?: string;
/**
* Specify all available namespaces. Required for loading all namespaces at once (loadMatrix).
*/
availableNs?: string[];
/**
* These data go directly to cache or you can specify async

@@ -65,4 +70,13 @@ * function which will be used to get the data. Use `:` to add namespace:

* ```
*
* You can also pass list of `CachePublicRecord`, which is in format:
*
* {
* 'language': <locale>,
* 'namespace': <namespace>
* 'data': <translations>
* }
*
*/
staticData?: TolgeeStaticData;
staticData?: TolgeeStaticDataProp;
/**

@@ -98,2 +112,10 @@ * Switches between invisible and text observer. (Default: invisible)

filterTag?: string[];
/**
* automatically load required records on `run` and `changeLanguage` (default: true)
*/
autoLoadRequiredData: boolean;
/**
* no internal cache is being held inside tolgee
*/
disableCache: boolean;
};

@@ -100,0 +122,0 @@ export type TolgeeOptions = Partial<Omit<TolgeeOptionsInternal, 'observerOptions'>> & {

@@ -1,5 +0,5 @@

import { CacheDescriptor, CacheDescriptorInternal, DevCredentials, NsFallback, NsType } from '../../types';
import { CacheDescriptor, CacheDescriptorInternal, DevCredentials, LanguageEvent, NsFallback, NsType, PendingLanguageEvent, RunningEvent } from '../../types';
import { EventEmitterInstance } from '../Events/EventEmitter';
import { TolgeeOptions } from './initState';
export declare function State(onLanguageChange: EventEmitterInstance<string>, onPendingLanguageChange: EventEmitterInstance<string>, onRunningChange: EventEmitterInstance<boolean>): Readonly<{
export declare function State(onLanguageChange: EventEmitterInstance<LanguageEvent>, onPendingLanguageChange: EventEmitterInstance<PendingLanguageEvent>, onRunningChange: EventEmitterInstance<RunningEvent>): Readonly<{
init(options?: Partial<TolgeeOptions>): void;

@@ -9,2 +9,3 @@ isRunning(): boolean;

isInitialLoading(): boolean;
isCacheDisabled(): boolean;
setInitialLoading(value: boolean): void;

@@ -25,4 +26,5 @@ getLanguage(): string | undefined;

fallbackNs?: import("../../types").FallbackGeneral;
defaultNs: string;
staticData?: import("./initState").TolgeeStaticData | undefined;
defaultNs?: string | undefined;
availableNs?: string[] | undefined;
staticData?: import("./initState").TolgeeStaticDataProp | undefined;
observerType: "invisible" | "text";

@@ -35,2 +37,4 @@ observerOptions: import("./observerOptions").ObserverOptionsInternal;

filterTag?: string[] | undefined;
autoLoadRequiredData: boolean;
disableCache: boolean;
};

@@ -42,4 +46,6 @@ addActiveNs(ns: NsFallback): void;

getFallbackNs(): string[];
getNs(): string[];
getDefaultNs(ns?: NsType): string;
getAvailableLanguages(): string[] | undefined;
getAvailableNs(): string[] | undefined;
withDefaultNs(descriptor: CacheDescriptor): CacheDescriptorInternal;

@@ -46,0 +52,0 @@ overrideCredentials(credentials: DevCredentials): void;

@@ -1,6 +0,6 @@

import { FallbackGeneral, FallbackLanguageOption, FetchFn, TolgeeError } from './types';
import { FallbackGeneral, FallbackLanguageOption, FetchFn, TolgeeError, ErrorEvent } from './types';
import { EventEmitterInstance } from './Controller/Events/EventEmitter';
export declare function isPromise(value: unknown): value is Promise<unknown>;
export declare function valueOrPromise<T, R>(value: T | Promise<T>, callback: (value: T) => R): R | Promise<R>;
export declare function handleRegularOrAsyncErr<T>(onError: EventEmitterInstance<TolgeeError>, createError: (e: any) => TolgeeError, callback: () => Promise<T> | T): Promise<T> | T;
export declare function handleRegularOrAsyncErr<T>(onError: EventEmitterInstance<ErrorEvent>, createError: (e: any) => TolgeeError, callback: () => Promise<T> | T): Promise<T> | T;
export declare function missingOptionError(option: string | string[]): string;

@@ -7,0 +7,0 @@ export declare function isObject(item: any): boolean;

@@ -8,16 +8,2 @@ import { TolgeeOptions, TolgeePlugin, DevCredentials, TFnType, DefaultParamType, TranslationKey } from './types';

/**
* Listen for specific namespaces changes.
*
* ```
* const sub = tolgee.onUpdate(handler)
*
* // subscribe to selected namespace
* sub.subscribeNs(['common'])
*
* // unsubscribe
* sub.unsubscribe()
* ```
*/
onNsUpdate: (handler: import("./types").Listener<undefined>) => import("./types").SubscriptionSelective;
/**
* Turn off/on events emitting. Is on by default.

@@ -48,3 +34,5 @@ */

changeTranslation: (descriptor: import("./types").CacheDescriptor, key: string, value: string) => {
revert(): void;
revert(): void; /**
* Manually load record from `Backend` (or `DevBackend` when in dev mode)
*/
};

@@ -63,2 +51,12 @@ /**

/**
* Load records which would be loaded by `run` function
*
* You can provide language if not previously set on tolgee instance
*/
loadRequired: (options?: import("./types").LoadRequiredOptions | undefined) => Promise<import("./types").CacheInternalRecord[]>;
/**
* Load records in matrix (languages x namespaces)
*/
loadMatrix: (options: import("./types").LoadMatrixOptions) => Promise<import("./types").CacheInternalRecord[]>;
/**
* Manually load multiple records from `Backend` (or `DevBackend` when in dev mode)

@@ -68,23 +66,19 @@ *

*/
loadRecords: (descriptors: import("./types").CacheDescriptor[]) => Promise<import("./types").TranslationsFlat[]>;
loadRecords: (descriptors: import("./types").CacheDescriptor[], options?: import("./types").LoadOptions | undefined) => Promise<import("./types").CacheInternalRecord[]>;
/**
* Manually load record from `Backend` (or `DevBackend` when in dev mode)
*/
loadRecord: (descriptor: import("./types").CacheDescriptor) => Promise<import("./types").TranslationsFlat>;
loadRecord: (descriptor: import("./types").CacheDescriptor, options?: import("./types").LoadOptions | undefined) => Promise<import("./types").TranslationsFlat>;
/**
* Prefill static data
*/
addStaticData: (data: import("./types").TolgeeStaticData | undefined) => void;
addStaticData: (data: import("./types").TolgeeStaticDataProp | undefined) => void;
/**
* Get record from cache.
*/
getRecord: (descriptor: import("./types").CacheDescriptor) => import("./types").TranslationsFlat | undefined;
getRecord: (descriptor: import("./types").CacheDescriptor) => import("./types").CacheInternalRecord | undefined;
/**
* Get all records from cache.
*/
getAllRecords: () => {
data: import("./types").TranslationsFlat;
language: string;
namespace: string;
}[];
getAllRecords: () => (import("./types").CacheInternalRecord | undefined)[];
/**

@@ -96,5 +90,5 @@ * @param ns optional list of namespaces that you are interested in

/**
* Returns records needed for instance to be `loaded`
* Returns descriptors of records needed for instance to be `loaded`
*/
getRequiredRecords: (lang?: string | undefined, ns?: import("./types").NsFallback) => import("./types").CacheDescriptor[];
getRequiredDescriptors: (lang?: string | undefined, ns?: import("./types").NsFallback) => import("./types").CacheDescriptorInternal[];
/**

@@ -153,4 +147,5 @@ * @return `true` if tolgee is loading initial data (triggered by `run`).

fallbackNs?: import("./types").FallbackGeneral;
defaultNs: string;
staticData?: import("./types").TolgeeStaticData | undefined;
defaultNs?: string | undefined;
availableNs?: string[] | undefined;
staticData?: import("./types").TolgeeStaticDataProp | undefined;
observerType: "invisible" | "text";

@@ -163,2 +158,4 @@ observerOptions: import("./types").ObserverOptionsInternal;

filterTag?: string[] | undefined;
autoLoadRequiredData: boolean;
disableCache: boolean;
};

@@ -165,0 +162,0 @@ /**

export type TranslationValue = string | undefined | null;
export type TranslationsFlat = Map<string, TranslationValue>;
export type TranslationsFlat = Record<string, TranslationValue>;
export type TreeTranslationsData = {

@@ -26,1 +26,19 @@ [key: string]: TranslationValue | TreeTranslationsData;

};
export type CacheInternalRecord = {
data: TranslationsFlat;
language: string;
namespace: string;
cacheKey: string;
};
export type LoadOptions = {
noDev?: boolean;
useCache?: boolean;
};
export type LoadRequiredOptions = LoadOptions & {
language?: string;
};
export type MatrixOptions = {
languages: string[] | 'all';
namespaces: string[] | 'all';
};
export type LoadMatrixOptions = LoadOptions & MatrixOptions;

@@ -1,2 +0,1 @@

import type { NsFallback } from './general';
import type { CacheDescriptorWithKey } from './cache';

@@ -7,16 +6,18 @@ import { TolgeeError } from './errors';

};
export type SubscriptionSelective = {
unsubscribe: () => void;
/**
* Subscribes to namespace(s)
* @param ns - namespace(s), if empty default namespace is used
*
* Can be used multiple times to subscribe for more.
*/
subscribeNs(ns?: NsFallback): SubscriptionSelective;
};
export type ListenerEvent<T> = {
export type ListenerEvent<E extends string, T> = {
type: E;
value: T;
};
export type Listener<T> = (e: ListenerEvent<T>) => void;
export type Handler<E extends ListenerEvent<string, any>> = (e: E) => void;
export type CombinedHandler<E extends ListenerEvent<string, any>> = (e: E[]) => void;
export type LanguageEvent = ListenerEvent<'language', string>;
export type PendingLanguageEvent = ListenerEvent<'pendingLanguage', string>;
export type LoadingEvent = ListenerEvent<'loading', boolean>;
export type FetchingEvent = ListenerEvent<'fetching', boolean>;
export type InitialLoadEvent = ListenerEvent<'initialLoad', void>;
export type RunningEvent = ListenerEvent<'running', boolean>;
export type CacheEvent = ListenerEvent<'cache', CacheDescriptorWithKey>;
export type ErrorEvent = ListenerEvent<'error', TolgeeError>;
export type PermanentChangeEvent = ListenerEvent<'permanentChange', TranslationDescriptor>;
export type UpdateEvent = LanguageEvent | CacheEvent | InitialLoadEvent;
export type TolgeeEvent = 'language' | 'pendingLanguage' | 'loading' | 'fetching' | 'initialLoad' | 'running' | 'cache' | 'update' | 'permanentChange';

@@ -40,39 +41,39 @@ export interface EventType {

*/
(event: 'update', handler: Listener<void>): Subscription;
(event: 'update', handler: CombinedHandler<UpdateEvent>): Subscription;
/**
* Emitted on language change.
*/
(event: 'language', handler: Listener<string>): Subscription;
(event: 'language', handler: Handler<LanguageEvent>): Subscription;
/**
* Emitted on pendingLanguage change.
*/
(event: 'pendingLanguage', handler: Listener<string>): Subscription;
(event: 'pendingLanguage', handler: Handler<PendingLanguageEvent>): Subscription;
/**
* Emitted on loading change. Changes when tolgee is loading some data for the first time.
*/
(event: 'loading', handler: Listener<boolean>): Subscription;
(event: 'loading', handler: Handler<LoadingEvent>): Subscription;
/**
* Emitted on fetching change. Changes when tolgee is fetching any data.
*/
(event: 'fetching', handler: Listener<boolean>): Subscription;
(event: 'fetching', handler: Handler<FetchingEvent>): Subscription;
/**
* Emitted when `tolgee.run` method finishes.
*/
(event: 'initialLoad', handler: Listener<void>): Subscription;
(event: 'initialLoad', handler: Handler<InitialLoadEvent>): Subscription;
/**
* Emitted when internal `running` state changes.
*/
(event: 'running', handler: Listener<boolean>): Subscription;
(event: 'running', handler: Handler<RunningEvent>): Subscription;
/**
* Emitted when cache changes.
*/
(event: 'cache', handler: Listener<CacheDescriptorWithKey>): Subscription;
(event: 'cache', handler: Handler<CacheEvent>): Subscription;
/**
* Emitted on errors
*/
(event: 'error', handler: Listener<TolgeeError>): Subscription;
(event: 'error', handler: Handler<ErrorEvent>): Subscription;
/**
* Translation was changed or created via dev tools
*/
(event: 'permanentChange', handler: Listener<CacheDescriptorWithKey>): Subscription;
(event: 'permanentChange', handler: Handler<PermanentChangeEvent>): Subscription;
(event: E, handler: unknown): Subscription;

@@ -79,0 +80,0 @@ };

@@ -6,4 +6,4 @@ export * from './general';

export * from './errors';
export type { State, TolgeeOptions, TolgeeOptionsInternal, TolgeeStaticData, } from '../Controller/State/initState';
export type { State, TolgeeOptions, TolgeeOptionsInternal, TolgeeStaticData, TolgeeStaticDataProp, } from '../Controller/State/initState';
export type { ObserverOptions, ObserverOptionsInternal, ModifierKey, } from '../Controller/State/observerOptions';
export type { TolgeeChainer, TolgeeInstance } from '../TolgeeCore';
{
"name": "@tolgee/core",
"version": "5.33.2",
"version": "5.33.3-prerelease.2efc0e6b.0",
"description": "Library providing ability to translate messages directly in context of developed application.",

@@ -76,3 +76,3 @@ "main": "./dist/tolgee.cjs.js",

"sideEffects": false,
"gitHead": "73467e06c29511cafab7e7162cef8ac4a36ff507"
"gitHead": "799332abc27675f19c1b722ac063cbe2451f500e"
}
/* eslint-disable no-console */
import { TolgeeCore, TolgeeInstance } from '../TolgeeCore';
import { TolgeePlugin, TreeTranslationsData } from '../types';
import { resolvablePromise } from './testTools';
import { TreeTranslationsData } from '../types';
import { DevToolsPlugin, DevToolsThrow, resolvablePromise } from './testTools';

@@ -15,26 +15,2 @@ function waitForInitialLoad(tolgee: TolgeeInstance) {

const DevToolsPlugin =
(postfix = ''): TolgeePlugin =>
(tolgee, tools) => {
tolgee.updateOptions({ apiKey: 'test', apiUrl: 'test' });
tools.setDevBackend({
getRecord({ language, namespace }) {
return Promise.resolve({
test: { sub: `${language}.${namespace || 'default'}${postfix}` },
});
},
});
return tolgee;
};
const DevToolsThrow = (): TolgeePlugin => (tolgee, tools) => {
tolgee.updateOptions({ apiKey: 'test', apiUrl: 'test' });
tools.setDevBackend({
getRecord() {
return Promise.reject();
},
});
return tolgee;
};
describe('cache', () => {

@@ -41,0 +17,0 @@ let tolgee: TolgeeInstance;

@@ -31,3 +31,3 @@ import { TolgeeCore } from '../index';

expect(promiseEnCommon).not.toBeCalled();
expect(enTest).toEqual(new Map([['test', 'Test']]));
expect(enTest).toEqual({ test: 'Test' });

@@ -39,3 +39,3 @@ const enCommon = await tolgee.loadRecord({

expect(promiseEnCommon).toBeCalledTimes(1);
expect(enCommon).toEqual(new Map([['cancel', 'Cancel']]));
expect(enCommon).toEqual({ cancel: 'Cancel' });

@@ -48,4 +48,4 @@ const esTest = await tolgee.loadRecord({

expect(promiseEsCommon).not.toBeCalled();
expect(esTest).toEqual(new Map([['test', 'Testa']]));
expect(esTest).toEqual({ test: 'Testa' });
});
});

@@ -9,25 +9,52 @@ import { TolgeeCore } from '../index';

await tolgee.changeLanguage('es');
expect(handler).toHaveBeenCalledWith({ value: 'es' });
expect(handler).toHaveBeenCalledWith({ type: 'language', value: 'es' });
});
it('correctly emits translation change listeners', async () => {
it('emits pendingLanguage event correctly', async () => {
const tolgee = TolgeeCore().init({
language: 'en',
staticData: {
en: { hello: 'World', language: 'English' },
es: { hello: 'Mundo', language: 'Spanish' },
en: () => Promise.resolve().then(() => ({ test: 'Test' })),
es: () => Promise.resolve().then(() => ({ test: 'El Test' })),
},
});
const helloHandler = jest.fn(() => {});
const languageHandler = jest.fn(() => {});
await tolgee.run();
const languageHandler = jest.fn();
const pendingLanguageHandler = jest.fn();
tolgee.on('language', languageHandler);
tolgee.on('pendingLanguage', pendingLanguageHandler);
const promise = tolgee.changeLanguage('es');
expect(pendingLanguageHandler).toHaveBeenCalledTimes(1);
expect(languageHandler).toHaveBeenCalledTimes(0);
await promise;
expect(pendingLanguageHandler).toHaveBeenCalledTimes(1);
expect(languageHandler).toHaveBeenCalledTimes(1);
});
tolgee.onNsUpdate(helloHandler);
tolgee.onNsUpdate(languageHandler);
tolgee.changeTranslation({ language: 'es' }, 'hello', 'Světe');
it('groups cache events with language change event', async () => {
const tolgee = TolgeeCore().init({ language: 'en' });
const handler = jest.fn((e) => {});
tolgee.on('update', handler);
tolgee.addStaticData({ en: { test: 'Test' } });
tolgee.changeLanguage('es');
expect(handler).toHaveBeenCalledWith([
{ type: 'cache', value: { language: 'en', namespace: '' } },
{ type: 'language', value: 'es' },
]);
});
await Promise.resolve();
expect(helloHandler).toHaveBeenCalledTimes(1);
expect(languageHandler).toHaveBeenCalledTimes(1);
it('groups cache events with initialLoad event', async () => {
const tolgee = TolgeeCore().init({
language: 'en',
staticData: {
en: () => Promise.resolve({ test: 'Test' }),
},
});
const handler = jest.fn((e) => {});
tolgee.on('update', handler);
await tolgee.run();
expect(handler).toHaveBeenCalledWith([
{ type: 'cache', value: { language: 'en', namespace: '' } },
{ type: 'initialLoad', value: undefined },
]);
});

@@ -34,0 +61,0 @@

@@ -44,3 +44,3 @@ import { TolgeeCore } from '../TolgeeCore';

expect(inputPrefix).toEqual('%-%tolgee:');
expect(defaultNs).toEqual('');
expect(defaultNs).toEqual(undefined);
});

@@ -47,0 +47,0 @@

@@ -0,1 +1,3 @@

import { TolgeePlugin } from '../types';
export const resolvablePromise = <T = any>() => {

@@ -8,1 +10,38 @@ let resolve: (value: T) => void;

};
export const DevToolsPlugin =
(postfix = ''): TolgeePlugin =>
(tolgee, tools) => {
tolgee.updateOptions({ apiKey: 'test', apiUrl: 'test' });
tools.setDevBackend({
getRecord({ language, namespace }) {
return Promise.resolve({
test: { sub: `${language}.${namespace || 'default'}${postfix}` },
});
},
});
return tolgee;
};
export const BackendPlugin =
(postfix = ''): TolgeePlugin =>
(tolgee, tools) => {
tools.addBackend({
getRecord({ language, namespace }) {
return Promise.resolve({
test: { sub: `${language}.${namespace || 'default'}${postfix}` },
});
},
});
return tolgee;
};
export const DevToolsThrow = (): TolgeePlugin => (tolgee, tools) => {
tolgee.updateOptions({ apiKey: 'test', apiUrl: 'test' });
tools.setDevBackend({
getRecord() {
return Promise.reject();
},
});
return tolgee;
};

@@ -5,3 +5,2 @@ import {

NsFallback,
TranslationsFlat,
TranslationValue,

@@ -11,5 +10,8 @@ TreeTranslationsData,

RecordFetchError,
LoadOptions,
CacheInternalRecord,
TranslationsFlat,
} from '../../types';
import { getFallbackArray, isPromise, unique } from '../../helpers';
import { TolgeeStaticData } from '../State/initState';
import { TolgeeStaticData, TolgeeStaticDataProp } from '../State/initState';
import { ValueObserverInstance } from '../ValueObserver';

@@ -38,2 +40,3 @@

isInitialLoading: () => boolean,
isCacheDisabled: () => boolean,
fetchingObserver: ValueObserverInstance<boolean>,

@@ -49,3 +52,3 @@ loadingObserver: ValueObserverInstance<boolean>

descriptor: CacheDescriptorInternal,
data: TreeTranslationsData,
data: TranslationsFlat,
recordVersion: number

@@ -58,3 +61,3 @@ ) {

});
events.onCacheChange.emit(descriptor);
events.onCacheChange.emit(decodeCacheKey(cacheKey));
}

@@ -116,4 +119,12 @@

const self = Object.freeze({
addStaticData(data: TolgeeStaticData | undefined) {
if (data) {
addStaticData(data: TolgeeStaticDataProp | undefined) {
if (Array.isArray(data)) {
for (const record of data) {
const key = encodeCacheKey(record);
const existing = cache.get(key);
if (!existing || existing.version === 0) {
addRecordInternal(record, flattenTranslations(record.data), 0);
}
}
} else if (data) {
staticData = { ...staticData, ...data };

@@ -125,3 +136,3 @@ Object.entries(data).forEach(([key, value]) => {

if (!existing || existing.version === 0) {
addRecordInternal(descriptor, value, 0);
addRecordInternal(descriptor, flattenTranslations(value), 0);
}

@@ -139,3 +150,3 @@ }

addRecord(descriptor: CacheDescriptorInternal, data: TreeTranslationsData) {
addRecordInternal(descriptor, data, version);
addRecordInternal(descriptor, flattenTranslations(data), version);
},

@@ -151,8 +162,23 @@

getRecord(descriptor: CacheDescriptor) {
return cache.get(encodeCacheKey(withDefaultNs(descriptor)))?.data;
getRecord(descriptor: CacheDescriptor): CacheInternalRecord | undefined {
const descriptorWithNs = withDefaultNs(descriptor);
const cacheKey = encodeCacheKey(descriptorWithNs);
const cacheRecord = cache.get(cacheKey);
if (!cacheRecord) {
return undefined;
}
return {
...descriptorWithNs,
cacheKey,
data: cacheRecord.data,
};
},
getAllRecords() {
const entries = Array.from(cache.entries());
return entries.map(([key]) => self.getRecord(decodeCacheKey(key)));
},
getTranslation(descriptor: CacheDescriptorInternal, key: string) {
return cache.get(encodeCacheKey(descriptor))?.data.get(key);
return cache.get(encodeCacheKey(descriptor))?.data[key];
},

@@ -163,5 +189,4 @@

for (const language of languages) {
const value = cache
.get(encodeCacheKey({ language, namespace }))
?.data.get(key);
const value = cache.get(encodeCacheKey({ language, namespace }))
?.data[key];
if (value !== undefined && value !== null) {

@@ -182,5 +207,4 @@ return [namespace];

for (const language of languages) {
const value = cache
.get(encodeCacheKey({ language, namespace }))
?.data.get(key);
const value = cache.get(encodeCacheKey({ language, namespace }))
?.data[key];
if (value !== undefined && value !== null) {

@@ -200,4 +224,6 @@ return value;

const record = cache.get(encodeCacheKey(descriptor))?.data;
record?.set(key, value);
events.onCacheChange.emit({ ...descriptor, key });
if (record?.[key]) {
record[key] = value;
events.onCacheChange.emit({ ...descriptor, key });
}
},

@@ -221,25 +247,54 @@

isLoading(language: string | undefined, ns?: NsFallback) {
isLoading(language: string, ns?: NsFallback) {
const namespaces = getFallbackArray(ns);
if (isInitialLoading()) {
return true;
}
const pendingCacheKeys = Array.from(asyncRequests.keys());
return Boolean(
isInitialLoading() ||
Array.from(asyncRequests.keys()).find((key) => {
const descriptor = decodeCacheKey(key);
return (
(!namespaces.length ||
namespaces.includes(descriptor.namespace)) &&
!self.exists({
namespace: descriptor.namespace,
language: language!,
})
);
})
pendingCacheKeys.find((key) => {
const descriptor = decodeCacheKey(key);
return (
(!namespaces.length || namespaces.includes(descriptor.namespace)) &&
!self.exists({
namespace: descriptor.namespace,
language: language,
})
);
})
);
},
async loadRecords(descriptors: CacheDescriptor[], isDev: boolean) {
const withPromises = descriptors.map((descriptor) => {
async loadRecords(
descriptors: CacheDescriptor[],
options?: LoadOptions
): Promise<CacheInternalRecord[]> {
type WithPromise = {
new: boolean;
language: string;
namespace: string;
cacheKey: string;
promise?: Promise<TreeTranslationsData | undefined>;
data?: TranslationsFlat;
};
const withPromises: WithPromise[] = descriptors.map((descriptor) => {
const keyObject = withDefaultNs(descriptor);
const cacheKey = encodeCacheKey(keyObject);
if (options?.useCache) {
const exists = self.exists(keyObject, true);
if (exists) {
return {
...keyObject,
new: false,
cacheKey,
data: self.getRecord(keyObject)!.data,
} as WithPromise;
}
}
const existingPromise = asyncRequests.get(cacheKey);

@@ -249,15 +304,18 @@

return {
...keyObject,
new: false,
promise: existingPromise,
keyObject,
cacheKey,
};
}
const dataPromise =
fetchData(keyObject, isDev) || Promise.resolve(undefined);
fetchData(keyObject, !options?.noDev) || Promise.resolve(undefined);
asyncRequests.set(cacheKey, dataPromise);
return {
...keyObject,
new: true,
promise: dataPromise,
keyObject,
cacheKey,

@@ -269,17 +327,25 @@ };

const results = await Promise.all(withPromises.map((val) => val.promise));
const promisesToWait = withPromises
.map((val) => val.promise)
.filter(Boolean);
withPromises.forEach((value, i) => {
const fetchedData = await Promise.all(promisesToWait);
withPromises.forEach((value) => {
if (value.promise) {
value.data = flattenTranslations(fetchedData[0] ?? {});
fetchedData.shift();
}
// if promise has changed in between, it means cache been invalidated or
// new data are being fetched
const promiseChanged =
asyncRequests.get(value.cacheKey) !== value.promise;
// if promise has changed in between, it means cache been invalidated or
// new data are being fetched
if (value.new && !promiseChanged) {
asyncRequests.delete(value.cacheKey);
const data = results[i];
if (data) {
self.addRecord(value.keyObject, data);
} else if (!self.getRecord(value.keyObject)) {
if (value.data) {
self.addRecord(value, value.data);
} else if (!self.getRecord(value)) {
// if no data exist, put empty object
self.addRecord(value.keyObject, {});
// so we know we don't have to fetch again
self.addRecord(value, {});
}

@@ -291,14 +357,9 @@ }

return withPromises.map((val) => self.getRecord(val.keyObject)!);
return withPromises.map((val) => ({
language: val.language,
namespace: val.namespace,
data: val.data ?? {},
cacheKey: val.cacheKey,
}));
},
getAllRecords() {
const entries = Array.from(cache.entries());
return entries.map(([key, entry]) => {
return {
...decodeCacheKey(key),
data: entry.data,
};
});
},
});

@@ -305,0 +366,0 @@

@@ -1,4 +0,8 @@

import { CacheDescriptorInternal, TreeTranslationsData } from '../../types';
import {
CacheDescriptorInternal,
TranslationsFlat,
TreeTranslationsData,
} from '../../types';
export const flattenTranslations = (
export const flattenTranslationsToMap = (
data: TreeTranslationsData

@@ -13,3 +17,3 @@ ): Map<string, string> => {

if (typeof value === 'object') {
flattenTranslations(value).forEach((flatValue, flatKey) => {
flattenTranslationsToMap(value).forEach((flatValue, flatKey) => {
result.set(key + '.' + flatKey, flatValue);

@@ -24,2 +28,7 @@ });

export const flattenTranslations = (
data: TreeTranslationsData
): TranslationsFlat => {
return Object.fromEntries(flattenTranslationsToMap(data).entries());
};
export const decodeCacheKey = (key: string): CacheDescriptorInternal => {

@@ -26,0 +35,0 @@ const [firstPart, ...rest] = key.split(':');

@@ -9,5 +9,10 @@ import { Events } from './Events/Events';

KeyAndNamespacesInternal,
CacheDescriptorInternal,
LoadOptions,
LoadRequiredOptions,
LoadMatrixOptions,
MatrixOptions,
} from '../types';
import { Cache } from './Cache/Cache';
import { getFallbackArray } from '../helpers';
import { getFallbackArray, unique } from '../helpers';
import { Plugins } from './Plugins/Plugins';

@@ -24,3 +29,3 @@ import { ValueObserver } from './ValueObserver';

export function Controller({ options }: StateServiceProps) {
const events = Events(getFallbackNs, getDefaultNs);
const events = Events();
const fetchingObserver = ValueObserver<boolean>(

@@ -60,2 +65,3 @@ false,

state.isInitialLoading,
state.isCacheDisabled,
fetchingObserver,

@@ -88,3 +94,3 @@ loadingObserver

function getDefaultAndFallbackNs(ns?: NsType) {
return [...getFallbackArray(getDefaultNs(ns)), ...getFallbackNs()];
return unique([...getFallbackArray(getDefaultNs(ns)), ...getFallbackNs()]);
}

@@ -94,7 +100,7 @@

// takes (ns|default, initial ns, fallback ns, active ns)
function getRequiredNamespaces(ns: NsFallback) {
return [
function getRequiredNamespaces(ns?: NsFallback) {
return unique([
...getFallbackArray(ns ?? getDefaultNs()),
...state.getRequiredNamespaces(),
];
]);
}

@@ -122,11 +128,9 @@

function getRequiredRecords(lang?: string, ns?: NsFallback) {
function getRequiredDescriptors(lang?: string, ns?: NsFallback) {
const languages = state.getFallbackLangs(lang);
const namespaces = getRequiredNamespaces(ns);
const result: CacheDescriptor[] = [];
const result: CacheDescriptorInternal[] = [];
languages.forEach((language) => {
namespaces.forEach((namespace) => {
if (!cache.exists({ language, namespace }, true)) {
result.push({ language, namespace });
}
result.push({ language, namespace });
});

@@ -137,7 +141,39 @@ });

function loadRequiredRecords(lang?: string, ns?: NsFallback) {
const descriptors = getRequiredRecords(lang, ns);
if (descriptors.length) {
return valueOrPromise(self.loadRecords(descriptors), () => {});
function getMissingDescriptors(lang?: string, ns?: NsFallback) {
return getRequiredDescriptors(lang, ns).filter(
(descriptor) => !cache.exists(descriptor, true)
);
}
function getMatrixRecords(options: MatrixOptions) {
let languages: string[] = [];
let namespaces: string[] = [];
if (Array.isArray(options.languages)) {
languages = options.languages;
} else if (options.languages === 'all') {
const availableLanguages = self.getAvailableLanguages();
if (!availableLanguages) {
throw new Error(missingOptionError('availableLanguages'));
}
languages = availableLanguages;
}
if (Array.isArray(options.namespaces)) {
namespaces = options.namespaces;
} else if (options.namespaces === 'all') {
const availableNs = self.getAvailableNs();
if (!availableNs) {
throw new Error(missingOptionError('availableNs'));
}
namespaces = availableNs;
}
const records: CacheDescriptorInternal[] = [];
languages.forEach((language) => {
namespaces.forEach((namespace) => {
records.push({ language, namespace });
});
});
return records;
}

@@ -159,4 +195,9 @@

const data = valueOrPromise(initializeLanguage(), () => {
// fail if there is no language
return loadRequiredRecords();
const missingDescriptors = getMissingDescriptors();
if (
missingDescriptors.length &&
state.getInitialOptions().autoLoadRequiredData
) {
return cache.loadRecords(missingDescriptors, { useCache: true });
}
});

@@ -219,3 +260,3 @@

findPositions: pluginService.findPositions,
getRequiredRecords: getRequiredRecords,
getRequiredDescriptors: getRequiredDescriptors,
async changeLanguage(language: string) {

@@ -230,4 +271,6 @@ if (

if (state.isRunning()) {
await loadRequiredRecords(language);
if (state.isRunning() && state.getInitialOptions().autoLoadRequiredData) {
await cache.loadRecords(getRequiredDescriptors(language), {
useCache: true,
});
}

@@ -248,14 +291,12 @@

if (state.isRunning()) {
await loadRequiredRecords(undefined, ns);
await cache.loadRecords(getRequiredDescriptors(undefined, ns), {
useCache: true,
});
}
},
loadRecords(descriptors: CacheDescriptor[]) {
return cache.loadRecords(descriptors, self.isDev());
async loadRecord(descriptor: CacheDescriptor, options?: LoadOptions) {
return (await self.loadRecords([descriptor], options))[0]?.data;
},
async loadRecord(descriptor: CacheDescriptor) {
return (await self.loadRecords([descriptor]))[0];
},
isLoading(ns?: NsFallback) {

@@ -296,2 +337,15 @@ return cache.isLoading(state.getLanguage()!, ns);

async loadRequired(options?: LoadRequiredOptions) {
if (!options?.language) {
await initializeLanguage();
}
const requiredRecords = getRequiredDescriptors(options?.language);
return self.loadRecords(requiredRecords, options);
},
async loadMatrix(options: LoadMatrixOptions) {
const records = getMatrixRecords(options);
return self.loadRecords(records, options);
},
run() {

@@ -298,0 +352,0 @@ checkCorrectConfiguration();

@@ -1,11 +0,12 @@

import { Subscription, Listener } from '../../types';
import { Subscription, Handler, ListenerEvent } from '../../types';
export function EventEmitter<T>(
export const EventEmitter = <Event extends ListenerEvent<string, any>>(
type: Event['type'],
isActive: () => boolean
): EventEmitterInstance<T> {
let handlers: Listener<T>[] = [];
): EventEmitterInstance<Event> => {
let handlers: Handler<Event>[] = [];
return Object.freeze({
listen(handler: Listener<T>): Subscription {
const handlerWrapper: Listener<T> = (e) => {
return {
listen(handler: (e: Event) => void): Subscription {
const handlerWrapper: Handler<Event> = (e) => {
handler(e);

@@ -22,13 +23,20 @@ };

},
emit(data: T) {
emit(data: Event['value']) {
if (isActive()) {
handlers.forEach((handler) => handler({ value: data }));
handlers.forEach((handler) =>
handler({ type: type, value: data } as Event)
);
}
},
});
}
} as unknown as EventEmitterInstance<Event>;
};
export type EventEmitterInstance<T> = {
readonly listen: (handler: Listener<T>) => Subscription;
readonly emit: (data: T) => void;
};
export type EventEmitterInstance<Event> =
Event extends ListenerEvent<infer E, infer T>
? {
readonly listen: (
handler: Handler<ListenerEvent<E, T>>
) => Subscription;
readonly emit: (data: T) => void;
}
: never;
import { EventEmitter } from './EventEmitter';
import { EventEmitterSelective } from './EventEmitterSelective';
import {
CacheDescriptorWithKey,
TolgeeError,
CacheEvent,
FetchingEvent,
InitialLoadEvent,
LanguageEvent,
LoadingEvent,
PendingLanguageEvent,
PermanentChangeEvent,
RunningEvent,
TolgeeOn,
TranslationDescriptor,
UpdateEvent,
ErrorEvent,
} from '../../types';
import { EventEmitterCombined } from './EventEmitterCombined';
export function Events(
getFallbackNs: () => string[],
getDefaultNs: () => string
) {
export function Events() {
let emitterActive = true;

@@ -21,12 +25,18 @@

const self = Object.freeze({
onPendingLanguageChange: EventEmitter<string>(isActive),
onLanguageChange: EventEmitter<string>(isActive),
onLoadingChange: EventEmitter<boolean>(isActive),
onFetchingChange: EventEmitter<boolean>(isActive),
onInitialLoaded: EventEmitter<void>(isActive),
onRunningChange: EventEmitter<boolean>(isActive),
onCacheChange: EventEmitter<CacheDescriptorWithKey>(isActive),
onUpdate: EventEmitterSelective(isActive, getFallbackNs, getDefaultNs),
onPermanentChange: EventEmitter<TranslationDescriptor>(isActive),
onError: EventEmitter<TolgeeError>(isActive),
onPendingLanguageChange: EventEmitter<PendingLanguageEvent>(
'pendingLanguage',
isActive
),
onLanguageChange: EventEmitter<LanguageEvent>('language', isActive),
onLoadingChange: EventEmitter<LoadingEvent>('loading', isActive),
onFetchingChange: EventEmitter<FetchingEvent>('fetching', isActive),
onInitialLoaded: EventEmitter<InitialLoadEvent>('initialLoad', isActive),
onRunningChange: EventEmitter<RunningEvent>('running', isActive),
onCacheChange: EventEmitter<CacheEvent>('cache', isActive),
onPermanentChange: EventEmitter<PermanentChangeEvent>(
'permanentChange',
isActive
),
onError: EventEmitter<ErrorEvent>('error', isActive),
onUpdate: EventEmitterCombined<UpdateEvent>(isActive),
setEmitterActive(active: boolean) {

@@ -61,7 +71,5 @@ emitterActive = active;

self.onInitialLoaded.listen(() => self.onUpdate.emit());
self.onLanguageChange.listen(() => self.onUpdate.emit());
self.onCacheChange.listen(({ value }) =>
self.onUpdate.emit([value.namespace], true)
);
self.onInitialLoaded.listen((e) => self.onUpdate.emit(e, false));
self.onLanguageChange.listen((e) => self.onUpdate.emit(e, false));
self.onCacheChange.listen((e) => self.onUpdate.emit(e, true));

@@ -68,0 +76,0 @@ return self;

@@ -270,2 +270,7 @@ import {

const { apiKey, apiUrl, projectId, filterTag } = getInitialOptions();
if (!apiKey || !apiUrl || !self.hasDevBackend()) {
return undefined;
}
return instances.devBackend?.getRecord({

@@ -272,0 +277,0 @@ apiKey,

@@ -8,2 +8,3 @@ import {

MissingTranslationHandler,
CachePublicRecord,
} from '../../types';

@@ -27,2 +28,4 @@ import { createFetchFunction, sanitizeUrl } from '../../helpers';

export type TolgeeStaticDataProp = TolgeeStaticData | CachePublicRecord[];
export type TolgeeOptionsInternal = {

@@ -55,4 +58,4 @@ /**

/**
* Languages which can be used for language detection
* and also limits which values can be stored
* Specify all available languages. Required for language detection or loading all languages at once (loadMatrix).
* It also limits which values can be stored. Is derrived from `staticData` keys if not provided.
*/

@@ -67,3 +70,3 @@ availableLanguages?: string[];

/**
* Namespaces which should be always fetched
* Namespaces which should be always fetched (default: [defaultNs] or [''])
*/

@@ -78,7 +81,12 @@ ns?: string[];

/**
* Default namespace when no namespace defined (default: '')
* Default namespace when no namespace defined (default: first from `ns`)
*/
defaultNs: string;
defaultNs?: string;
/**
* Specify all available namespaces. Required for loading all namespaces at once (loadMatrix).
*/
availableNs?: string[];
/**
* These data go directly to cache or you can specify async

@@ -93,4 +101,13 @@ * function which will be used to get the data. Use `:` to add namespace:

* ```
*
* You can also pass list of `CachePublicRecord`, which is in format:
*
* {
* 'language': <locale>,
* 'namespace': <namespace>
* 'data': <translations>
* }
*
*/
staticData?: TolgeeStaticData;
staticData?: TolgeeStaticDataProp;

@@ -133,2 +150,12 @@ /**

filterTag?: string[];
/**
* automatically load required records on `run` and `changeLanguage` (default: true)
*/
autoLoadRequiredData: boolean;
/**
* no internal cache is being held inside tolgee
*/
disableCache: boolean;
};

@@ -152,3 +179,2 @@

const defaultValues: TolgeeOptionsInternal = {
defaultNs: '',
observerOptions: defaultObserverOptions,

@@ -158,4 +184,6 @@ observerType: 'invisible',

apiUrl: DEFAULT_API_URL,
autoLoadRequiredData: true,
fetch: createFetchFunction(),
onTranslationMissing: DEFAULT_MISSING_TRANSLATION,
disableCache: false,
};

@@ -162,0 +190,0 @@

@@ -5,4 +5,7 @@ import {

DevCredentials,
LanguageEvent,
NsFallback,
NsType,
PendingLanguageEvent,
RunningEvent,
} from '../../types';

@@ -21,5 +24,5 @@

export function State(
onLanguageChange: EventEmitterInstance<string>,
onPendingLanguageChange: EventEmitterInstance<string>,
onRunningChange: EventEmitterInstance<boolean>
onLanguageChange: EventEmitterInstance<LanguageEvent>,
onPendingLanguageChange: EventEmitterInstance<PendingLanguageEvent>,
onRunningChange: EventEmitterInstance<RunningEvent>
) {

@@ -49,2 +52,6 @@ let state = initState();

isCacheDisabled() {
return state.initialOptions.disableCache;
},
setInitialLoading(value: boolean) {

@@ -105,3 +112,4 @@ state.isInitialLoading = value;

return unique([
...(state.initialOptions.ns || [state.initialOptions.defaultNs]),
self.getDefaultNs(),
...(state.initialOptions.ns || []),
...getFallbackArray(state.initialOptions.fallbackNs),

@@ -130,4 +138,12 @@ ...state.activeNamespaces.keys(),

getNs() {
return state.initialOptions.ns?.length
? state.initialOptions.ns
: [state.initialOptions.defaultNs ?? ''];
},
getDefaultNs(ns?: NsType) {
return ns === undefined ? state.initialOptions.defaultNs : ns;
return ns === undefined
? state.initialOptions.defaultNs ?? state.initialOptions.ns?.[0] ?? ''
: ns;
},

@@ -146,2 +162,6 @@

getAvailableNs() {
return state.initialOptions.availableNs;
},
withDefaultNs(descriptor: CacheDescriptor): CacheDescriptorInternal {

@@ -151,3 +171,3 @@ return {

descriptor.namespace === undefined
? self.getInitialOptions().defaultNs
? self.getDefaultNs()
: descriptor.namespace,

@@ -154,0 +174,0 @@ language: descriptor.language,

@@ -7,2 +7,3 @@ import {

TolgeeError,
ErrorEvent,
} from './types';

@@ -29,3 +30,3 @@ import { EventEmitterInstance } from './Controller/Events/EventEmitter';

export function handleRegularOrAsyncErr<T>(
onError: EventEmitterInstance<TolgeeError>,
onError: EventEmitterInstance<ErrorEvent>,
createError: (e: any) => TolgeeError,

@@ -32,0 +33,0 @@ callback: () => Promise<T> | T

@@ -39,17 +39,2 @@ import { Controller } from './Controller/Controller';

/**
* Listen for specific namespaces changes.
*
* ```
* const sub = tolgee.onUpdate(handler)
*
* // subscribe to selected namespace
* sub.subscribeNs(['common'])
*
* // unsubscribe
* sub.unsubscribe()
* ```
*/
onNsUpdate: controller.onUpdate.listenSome,
/**
* Turn off/on events emitting. Is on by default.

@@ -99,2 +84,14 @@ */

/**
* Load records which would be loaded by `run` function
*
* You can provide language if not previously set on tolgee instance
*/
loadRequired: controller.loadRequired,
/**
* Load records in matrix (languages x namespaces)
*/
loadMatrix: controller.loadMatrix,
/**
* Manually load multiple records from `Backend` (or `DevBackend` when in dev mode)

@@ -133,5 +130,5 @@ *

/**
* Returns records needed for instance to be `loaded`
* Returns descriptors of records needed for instance to be `loaded`
*/
getRequiredRecords: controller.getRequiredRecords,
getRequiredDescriptors: controller.getRequiredDescriptors,

@@ -138,0 +135,0 @@ /**

export type TranslationValue = string | undefined | null;
export type TranslationsFlat = Map<string, TranslationValue>;
export type TranslationsFlat = Record<string, TranslationValue>;

@@ -38,1 +38,24 @@ export type TreeTranslationsData = {

};
export type CacheInternalRecord = {
data: TranslationsFlat;
language: string;
namespace: string;
cacheKey: string;
};
export type LoadOptions = {
noDev?: boolean;
useCache?: boolean;
};
export type LoadRequiredOptions = LoadOptions & {
language?: string;
};
export type MatrixOptions = {
languages: string[] | 'all';
namespaces: string[] | 'all';
};
export type LoadMatrixOptions = LoadOptions & MatrixOptions;

@@ -1,2 +0,1 @@

import type { NsFallback } from './general';
import type { CacheDescriptorWithKey } from './cache';

@@ -9,16 +8,23 @@ import { TolgeeError } from './errors';

export type SubscriptionSelective = {
unsubscribe: () => void;
/**
* Subscribes to namespace(s)
* @param ns - namespace(s), if empty default namespace is used
*
* Can be used multiple times to subscribe for more.
*/
subscribeNs(ns?: NsFallback): SubscriptionSelective;
};
export type ListenerEvent<E extends string, T> = { type: E; value: T };
export type Handler<E extends ListenerEvent<string, any>> = (e: E) => void;
export type CombinedHandler<E extends ListenerEvent<string, any>> = (
e: E[]
) => void;
export type ListenerEvent<T> = { value: T };
export type Listener<T> = (e: ListenerEvent<T>) => void;
export type LanguageEvent = ListenerEvent<'language', string>;
export type PendingLanguageEvent = ListenerEvent<'pendingLanguage', string>;
export type LoadingEvent = ListenerEvent<'loading', boolean>;
export type FetchingEvent = ListenerEvent<'fetching', boolean>;
export type InitialLoadEvent = ListenerEvent<'initialLoad', void>;
export type RunningEvent = ListenerEvent<'running', boolean>;
export type CacheEvent = ListenerEvent<'cache', CacheDescriptorWithKey>;
export type ErrorEvent = ListenerEvent<'error', TolgeeError>;
export type PermanentChangeEvent = ListenerEvent<
'permanentChange',
TranslationDescriptor
>;
export type UpdateEvent = LanguageEvent | CacheEvent | InitialLoadEvent;
export type TolgeeEvent =

@@ -53,3 +59,3 @@ | 'language'

*/
(event: 'update', handler: Listener<void>): Subscription;
(event: 'update', handler: CombinedHandler<UpdateEvent>): Subscription;

@@ -59,3 +65,3 @@ /**

*/
(event: 'language', handler: Listener<string>): Subscription;
(event: 'language', handler: Handler<LanguageEvent>): Subscription;

@@ -65,3 +71,6 @@ /**

*/
(event: 'pendingLanguage', handler: Listener<string>): Subscription;
(
event: 'pendingLanguage',
handler: Handler<PendingLanguageEvent>
): Subscription;

@@ -71,3 +80,3 @@ /**

*/
(event: 'loading', handler: Listener<boolean>): Subscription;
(event: 'loading', handler: Handler<LoadingEvent>): Subscription;

@@ -77,3 +86,3 @@ /**

*/
(event: 'fetching', handler: Listener<boolean>): Subscription;
(event: 'fetching', handler: Handler<FetchingEvent>): Subscription;

@@ -83,3 +92,3 @@ /**

*/
(event: 'initialLoad', handler: Listener<void>): Subscription;
(event: 'initialLoad', handler: Handler<InitialLoadEvent>): Subscription;

@@ -89,3 +98,3 @@ /**

*/
(event: 'running', handler: Listener<boolean>): Subscription;
(event: 'running', handler: Handler<RunningEvent>): Subscription;

@@ -95,3 +104,3 @@ /**

*/
(event: 'cache', handler: Listener<CacheDescriptorWithKey>): Subscription;
(event: 'cache', handler: Handler<CacheEvent>): Subscription;

@@ -101,3 +110,3 @@ /**

*/
(event: 'error', handler: Listener<TolgeeError>): Subscription;
(event: 'error', handler: Handler<ErrorEvent>): Subscription;

@@ -109,3 +118,3 @@ /**

event: 'permanentChange',
handler: Listener<CacheDescriptorWithKey>
handler: Handler<PermanentChangeEvent>
): Subscription;

@@ -112,0 +121,0 @@

@@ -12,2 +12,3 @@ export * from './general';

TolgeeStaticData,
TolgeeStaticDataProp,
} from '../Controller/State/initState';

@@ -14,0 +15,0 @@

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is too big to display

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc