import type { Language } from '../language/Language'; import type { DiscountType } from './DiscountType'; import { languageFromJson, languageToJson } from '../language/Language'; import { discountTypeFromJson, discountTypeToJson } from './DiscountType'; export class DiscountItem { title?: Map; type?: DiscountType; value?: string; constructor( title?: Map, type?: DiscountType, value?: string ) { this.title = title; this.type = type; this.value = value; } } export function discountItemFromJson(json: { [key: string]: any; }): DiscountItem { const titleMap = json.title ? new Map( Object.entries(json.title).map(([key, value]) => [ languageFromJson(key), value as string, ]) ) : undefined; return new DiscountItem( titleMap, json.type ? discountTypeFromJson(json.type) : undefined, json.value ); } export function discountItemToJson(discountItem: DiscountItem): { [key: string]: any; } { const titleMap: { [key: string]: any } = {}; discountItem.title?.forEach((value, key) => { titleMap[languageToJson(key)] = value; }); return { title: titleMap, type: discountItem.type ? discountTypeToJson(discountItem.type) : undefined, value: discountItem.value, }; }