import { AddressType, getAddressType } from '@msfa-enums/address-type.enum';
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[];
  officialAddress: Address;
  postalAddress: Address;
}

export function mapResponseToContactInformation(data: ContactInformationResponse): ContactInformation {
  const { fornamn, efternamn, personnummer, epost, telekomadresser, adresser } = data;
  const officialAddress = adresser?.find(({ adresstyp }) => getAddressType(adresstyp) === AddressType.OFFICIAL);
  const postalAddress = adresser?.find(({ adresstyp }) => getAddressType(adresstyp) === AddressType.POSTAL);

  return {
    firstName: fornamn || '',
    lastName: efternamn || '',
    fullName: fornamn && efternamn ? `${fornamn} ${efternamn}` : '',
    ssn: mapStringToSsn(personnummer),
    email: epost || '',
    phoneNumbers: telekomadresser
      ? telekomadresser
          .filter(phoneNumber => phoneNumber.landskod && phoneNumber.nummer_utan_inledande_nolla)
          .map(phoneNumber => mapResponseToPhoneNumber(phoneNumber))
      : [],
    officialAddress: officialAddress ? mapResponseToAddress(officialAddress) : null,
    postalAddress: postalAddress ? mapResponseToAddress(postalAddress) : null,
    // addresses: adresser ? adresser.map(address => mapResponseToAddress(address)) : null,
  };
}
