import { mapStringToSsn } from '@msfa-utils/map-string-to-ssn.util';
import { Address, mapResponseToAddress } from './address.model';
import { ContactInformationResponse } from './api/contact-information.response.model';
import { mapResponseToPhoneNumber, PhoneNumber } from './phonenumber.model';

export interface ContactInformation {
  firstName: string;
  lastName: string;
  fullName: string;
  ssn: string;
  email: string;
  phoneNumbers: PhoneNumber[];
  addresses: Address[];
}

export function mapResponseToContactInformation(data: ContactInformationResponse): ContactInformation {
  const { fornamn, efternamn, personnummer, epost, telekomadresser, adresser } = data;
  return {
    firstName: fornamn || '',
    lastName: efternamn || '',
    fullName: fornamn && efternamn ? `${fornamn} ${efternamn}` : '',
    ssn: personnummer ? mapStringToSsn(personnummer) : '',
    email: epost || '',
    phoneNumbers: telekomadresser ? telekomadresser.map(phoneNumber => mapResponseToPhoneNumber(phoneNumber)) : [],
    addresses: adresser ? adresser.map(address => mapResponseToAddress(address)) : null,
  };
}
