import Toast from "./Toast"; export default class AppState { /** Json data as exported from zip and compliant with schemas */ private jsonData: TypeApp; /** Current App locale */ private locale: string; /** Fallback App locale */ private fallbackLocale: string; constructor(jsonData: TypeApp, locale: string, fallbackLocale: string) { this.jsonData = jsonData; this.locale = locale; this.fallbackLocale = fallbackLocale; } public settings(): TypeSettings { return this.jsonData.settings; } public switchLocale(locale: string): void { this.locale = locale; } public geti18N() { return { locale: this.locale, fallbackLocale: this.fallbackLocale, }; } public getFallbackLocale(): string { return this.fallbackLocale; } // * --- BRANDS public brands(): Array { return this.jsonData.items; } public brand(slug:string): TypeBrand|undefined { return this.jsonData.items.find(brand => brand.slug[this.locale] === slug); } public brandFromRange(range:TypeRange): TypeBrand { const brand = this.jsonData.items.find(brand => brand.id === range.brand_id); if (!brand) { Toast.toast("Une erreur est survenue"); throw new Error(`Could not find brand with id ${range.brand_id}`); } return brand; } // * --- END BRANDS // * --- RANGES public ranges(brand: TypeBrand | null = null): Array { const ranges: Array = []; this.jsonData.items.map((brandItem) => { if (brand === null || brand?.id === brandItem.id) { ranges.push(...brandItem.ranges); return brandItem; } return brandItem; }); return ranges.sort((rangeA, rangeB) => rangeA.order - rangeB.order); } public range(slug:string): TypeRange|undefined { return this.ranges().find(range => range.slug[this.locale] === slug); } // * --- END RANGES public font(fontId: number): TypeFont|undefined { return this.jsonData.fonts.find(font=> font.id === fontId); } }