import { Injectable } from '@angular/core';
import { Mapper } from './mapper';
import { Product } from '../models/product';
import { ItemDTO } from '@swagger/cat';

@Injectable()
export class ItemDtoToProductMapping implements Mapper<ItemDTO, Product> {
  get sourceName(): string {
    return 'ItemDTO';
  }

  get targetName(): string {
    return 'Product';
  }

  map(source: ItemDTO): Product {
    return {
      // tslint:disable-next-line: no-string-literal
      id: (source.ids && source.ids[0]) || source['id'],
      imageId: source.product.ean,
      contributors: source.product.contributors,
      name: source.product.name,
      format: source.product.format,
      formatDetail: source.product.formatDetail,
      ean: source.product.ean,
      productGroup: source.product.productGroup,
      productGroupName: '',
      features:
        source.features &&
        source.features.map((feature) => ({
          name: feature.value,
          key: feature.key[feature.key.length - 1],
        })),
      // tslint:disable-next-line: no-string-literal TODO update ItemDTO interface
      sourceProduct: { ...source.product, catalogProductNumber: source['id'] },
      sourcePrice:
        (source.storeAvailabilities && source.storeAvailabilities[0] && source.storeAvailabilities[0].price) ||
        (source.catalogAvailability && source.catalogAvailability.price),
    } as any;
  }
}
