import { Products, Recommendation, TrendingProduct } from './Contracts'; class Trending { /** * The recommendation response from Dynamic Yield. * * @type {Recommendation} */ private recommendation: Recommendation; /** * The player Products collection. * * @type {Products} */ private ___products___: Products; /** * Create a new Trending instance. * * @param {Recommendation} recommendation * @param {Products} products * @return {Trending} */ constructor(recommendation: Recommendation, products: Products) { this.recommendation = recommendation; this.___products___ = products; } /** * Returns the trending headline text. * * @return {string} */ public get header(): string { return this.recommendation.textMap.header; } /** * Returns the trending sub-headline text. * * @return {string} */ public get subHeader(): string { return this.recommendation.textMap.subHeader; } /** * Returns the trending products disclaimers. * * @return {string} */ public get disclaimers(): string { const disclaimers = this.recommendation.products.reduce((disclaimers, trending) => { const product = this.___products___[trending.code]; return { ...disclaimers, ...product.meta.disclaimer }; }, {}); return Object.values(disclaimers).join(' '); } /** * Returns the trending products models. * * @return {Array} */ public get products(): Array { return this.recommendation.products.reduce((products, { code }) => { const { title, assets, meta } = this.___products___[code]; return [...products, { asset: assets[0], title, type: meta.type }]; }, >[]); } /** * Serialize the Trending data model. * * @param {string} [key] * @return {object} */ public toJSON(key?: string) { const { header, subHeader, disclaimers, products } = this; return { header, subHeader, disclaimers, products }; } } export default Trending;