import { Injectable } from '@angular/core';
import { Mapper } from './mapper';
import { ReceiptItemDtoToRemissionProductMapping } from './receipt-item-dto-to-remission-product.mapping';
import { MappingService } from './mapping.service';
import { ShippingDocument } from '../models/shipping-document';
import { ReceiptDTO } from '@swagger/remi';

@Injectable()
export class ReceiptDTOToShippingDocumentMapping implements Mapper<ReceiptDTO, ShippingDocument> {
  constructor(private mappingService: MappingService) {}

  get sourceName() {
    return 'ReceiptDTO';
  }

  get targetName() {
    return 'ShippingDocument';
  }

  map(source: ReceiptDTO): ShippingDocument {
    const productMapper = this.mappingService.get<ReceiptItemDtoToRemissionProductMapping>('ReceiptItemDTO', 'RemissionProduct');
    return {
      id: source.id,
      shippingDocumentNumber: source.receiptNumber,
      packageNumber: source.packages && source.packages[0] && source.packages[0].data && source.packages[0].data.packageNumber,
      products: source.items.map((item) => productMapper.map(item.data || item)),
      isCompleted: !!source.completed || source.status === 4 ? true : false,
      isDeleted: source.status === 4,
    };
  }
}
