import { IDeliveryClientConfig } from '../config' ; import { Contracts } from '../contracts' ; import { IContentItem , IContentItemsContainer , IContentItemWithRawDataContainer , IContentItemWithRawElements } from '../models' ; import { ElementMapper } from './element.mapper' ; export interface IMapItemResult < TContentItem extends IContentItem = IContentItem > { item : TContentItem ; processedItems : IContentItemsContainer ; preparedItems : IContentItemWithRawDataContainer ; processingStartedForCodenames : string [ ] ; } export interface IMultipleItemsMapResult < TContentItem extends IContentItem = IContentItem > { items : TContentItem [ ] ; linkedItems : IContentItemsContainer ; } export interface ISingleItemMapResult < TContentItem extends IContentItem = IContentItem > { item : TContentItem ; linkedItems : IContentItemsContainer ; } export class ItemMapper { private readonly elementMapper : ElementMapper ; constructor ( readonly config ) { this . elementMapper = new ElementMapper ( config ) ; } param response param queryConfig mapSingleItemFromResponse < TContentItem extends IContentItem = IContentItem > ( response ) { const mapResult = this . mapItems < TContentItem > ( { mainItems : [ response . item ] , linkedItems : Object . values ( response . modular_content ) , } ) ; return { item : mapResult . items [ 0 ] , linkedItems : mapResult . linkedItems } ; } param response param queryConfig mapMultipleItemsFromResponse < TContentItem extends IContentItem = IContentItem > ( response ) { const mapResult = this . mapItems < TContentItem > ( { mainItems : response . items , linkedItems : Object . values ( response . modular_content ) } ) ; return mapResult ; } /**
     * Maps item contracts to full models
     */ mapItems < TContentItem extends IContentItem = IContentItem > ( data ) { const that = this ; const processedItems = { } ; const preparedItems = { } ; const processingStartedForCodenames = [ ] ; const mappedMainItems = [ ] ; const mappedLinkedItems = { } ; const itemsToResolve = [ ... data . mainItems , ... data . linkedItems ] ; for ( const item of itemsToResolve ) { preparedItems [ item . system . codename ] = { item : this . createContentItem ( item ) , rawItem : item } ; } for ( const item of data . mainItems ) { const itemResult = that . mapItem < TContentItem > ( { item : preparedItems [ item . system . codename ] , processedItems : processedItems , preparedItems : preparedItems , processingStartedForCodenames : processingStartedForCodenames } ) ; mappedMainItems . push ( itemResult . item ) ; } for ( const item of data . linkedItems ) { const itemResult = that . mapItem < TContentItem > ( { item : preparedItems [ item . system . codename ] , processedItems : processedItems , preparedItems : preparedItems , processingStartedForCodenames : processingStartedForCodenames } ) ; mappedLinkedItems [ item . system . codename ] = itemResult . item ; } return { items : mappedMainItems , linkedItems : mappedLinkedItems } ; } /**
     * Maps item contract to full model
     */ private mapItem < TContentItem extends IContentItem = IContentItem > ( data ) { if ( ! data . item ) { throw Error ( `Could not map item because its undefined` ) ; } const result = this . elementMapper . mapElements < TContentItem > ( { dataToMap : data . item , preparedItems : data . preparedItems , processingStartedForCodenames : [ ] , processedItems : data . processedItems , } ) ; if ( ! result ) { throw Error ( `Mapping of content item '${ data . item . item . system . codename }' failed` ) ; } return { item : result . item , processedItems : result . processedItems , preparedItems : result . preparedItems , processingStartedForCodenames : result . processingStartedForCodenames } ; } private createContentItem ( item ) { const contentItem = { elements : { } , system : { codename : item . system . codename , collection : item . system . collection , id : item . system . id , language : item . system . language , lastModified : item . system . last_modified , name : item . system . name , sitemapLocations : item . system . sitemap_locations , type : item . system . type , workflowStep : item . system . workflow_step ?? null } } ; return contentItem ; } }