@adyen/adyen-platform-experience-web
Version:

158 lines (157 loc) • 5.67 kB
JavaScript
var b = Object.defineProperty;
var _ = (r) => {
throw TypeError(r);
};
var j = (r, t, e) => t in r ? b(r, t, { enumerable: !0, configurable: !0, writable: !0, value: e }) : r[t] = e;
var R = (r, t, e) => j(r, typeof t != "symbol" ? t + "" : t, e), z = (r, t, e) => t.has(r) || _("Cannot " + e);
var s = (r, t, e) => (z(r, t, "read from private field"), e ? e.call(r) : t.get(r)), o = (r, t, e) => t.has(r) ? _("Cannot add the same private member more than once") : t instanceof WeakSet ? t.add(r) : t.set(r, e), a = (r, t, e, i) => (z(r, t, "write to private field"), i ? i.call(r, e) : t.set(r, e), e), C = (r, t, e) => (z(r, t, "access private method"), e);
import { FALLBACK_LOCALE as E, DEFAULT_TRANSLATIONS as x, getLocalesFromTranslationSourcesRecord as I, DEFAULT_DATETIME_FORMAT as M, SUPPORTED_LOCALES as W } from "./constants/localization.js";
import { en_US as w } from "../../translations/index.js";
import { getLocalisedAmount as k } from "./amount/amount-util.js";
import { createTranslationsLoader as q, getLocalizationProxyDescriptors as v } from "./localization-utils.js";
import { toTwoLetterCode as F, formatCustomTranslations as B, getTranslation as N } from "./utils.js";
import { createWatchlist as K } from "../../primitives/reactive/watchlist/main.js";
import V from "./datetime/restamper/restamper.js";
import { isNullish as P, isUndefined as Y, isNull as Z } from "../../utils/value/is.js";
import { ALREADY_RESOLVED_PROMISE as $ } from "../../utils/async/constants.js";
import { struct as G } from "../../utils/struct/main.js";
import { noop as H } from "../../utils/common.js";
var h, g, u, T, f, p, c, A, l, m, d, L, D, S;
class ct {
constructor(t = E, e) {
o(this, D);
o(this, h, E);
o(this, g, F(s(this, h)));
o(this, u, [E]);
o(this, T, s(this, u));
o(this, f);
o(this, p, x);
o(this, c, q.call(this));
o(this, A, $);
o(this, l);
o(this, m);
o(this, d, K({ timestamp: () => performance.now() }));
o(this, L, V());
R(this, "watch", s(this, d).subscribe.bind(void 0));
R(this, "i18n", G(v.call(this)));
R(this, "preferredTranslations");
this.watch(H), this.preferredTranslations = Object.freeze(
(e == null ? void 0 : e.reduce((i, n) => ({ ...i, ...n }), w)) ?? { ...w }
), a(this, u, I(this.preferredTranslations)), this.locale = t;
}
get customTranslations() {
return s(this, f) ?? {};
}
set customTranslations(t) {
let e, i = [...s(this, u)];
if (!P(t)) {
e = B(t, W);
const n = Object.keys(e);
i = [...i, ...n].sort().filter((y, O, U) => U.indexOf(y) === O);
}
s(this, c).supportedLocales = i, C(this, D, S).call(this, e);
}
get languageCode() {
return s(this, g);
}
get lastRefreshTimestamp() {
return s(this, d).snapshot.timestamp;
}
get locale() {
return s(this, h);
}
set locale(t) {
if (P(t))
this.locale = E;
else {
if (s(this, c).locale = t, s(this, h) === s(this, c).locale) return;
C(this, D, S).call(this, s(this, f));
}
}
get ready() {
return s(this, A);
}
get supportedLocales() {
return s(this, T);
}
get timezone() {
return s(this, L).tz.current;
}
set timezone(t) {
s(this, L).tz = t;
}
/**
* Returns a translated string from a key in the current {@link Localization.locale}
* @param key - Translation key
* @param options - Translation options
* @returns Translated string
*/
get(t, e) {
const i = N(s(this, p), t, e);
return Z(i) ? t : i;
}
/**
* Returns a boolean that checks if the translation key exists in the current {@link Localization.locale}
* @param key - Translation key
* @param options - Translation options
* @returns boolean
*/
has(t, e) {
return !!N(s(this, p), t, e);
}
/**
* Returns a localized string for an amount
* @param amount - Amount to be converted
* @param currencyCode - Currency code of the amount
* @param options - Options for String.prototype.toLocaleString
*/
amount(t, e, i) {
const { hideCurrency: n, ...y } = i || {}, O = k(t, s(this, h), e, n, {
...y,
currencyDisplay: "symbol",
signDisplay: "never"
});
return t < 0 ? `- ${O}` : O;
}
/**
* Returns a localized string for a date
* @param date - Date to be localized
* @param options - Options for {@link Date.toLocaleDateString}
*/
date(t, e = {}) {
const i = { ...M, timeZone: s(this, L).tz.current, ...e };
return new Date(t).toLocaleDateString(s(this, h), i);
}
/**
* Returns a localized string for a full date
* @param date - Date to be localized
*/
fullDate(t) {
return this.date(t, {
month: "short",
hour: "2-digit",
minute: "2-digit",
second: "2-digit",
hour12: !1
});
}
}
h = new WeakMap(), g = new WeakMap(), u = new WeakMap(), T = new WeakMap(), f = new WeakMap(), p = new WeakMap(), c = new WeakMap(), A = new WeakMap(), l = new WeakMap(), m = new WeakMap(), d = new WeakMap(), L = new WeakMap(), D = new WeakSet(), S = function(t) {
Y(s(this, m)) && a(this, A, new Promise((n) => {
a(this, m, () => {
n(s(this, l)), a(this, l, a(this, m, void 0));
});
}));
const e = () => {
var n;
s(this, l) === i && ((n = s(this, m)) == null || n.call(this));
}, i = a(this, l, (async () => {
a(this, p, await s(this, c).load(t)), a(this, h, s(this, c).locale), a(this, T, Object.freeze(s(this, c).supportedLocales)), a(this, f, t), a(this, g, F(s(this, h))), s(this, d).requestNotification();
})());
i.then(e).catch((n) => {
e(), console.error(n);
});
};
export {
ct as default
};