import { generateHashMapData } from '.';
import { ICountry } from '../interface/country.interface';
import { IData } from '../interface/data.interface';
import { IPhoneInformation } from '../interface/phone-information';

export function getInformation(number: string, data: Map<ICountry, Array<IData>>): IPhoneInformation {
  let phoneInformation: IPhoneInformation = {
    name: '',
    dialCode: '',
    isoCode: '',
    flag: '',
    type: '',
  };

  let prefixLength = 2;

  data.forEach((value, key) => {
    if (number.startsWith(key.dialCode)) {
      const splittedInfromation = value[0].Destination.split(' - ');

      phoneInformation = { ...key, type: splittedInfromation[1], carrier: splittedInfromation[2] };

      for (let index = 0; index < value.length; index++) {
        if (number.startsWith('+' + value[index].Prefix.toString())) {
          if (value[index].Prefix.toString().length > prefixLength) {
            prefixLength = value[index].Prefix.toString().length;
            const splittedInfromation = value[index].Destination.split(' - ');

            phoneInformation.type = splittedInfromation[1];
            phoneInformation.carrier = splittedInfromation[2];
          }
        }
      }
    }
  });

  return phoneInformation;
}
