import { COUNTRIES_PHONE_CODES } from '../../../constans/countriesPhoneCodes';
import { countryForPhone } from '../../../core/utils/countryForPhone';

export function phoneMask(phone: string) {
  const maxPhoneSize = 15;
  const digitRegex = /\d/;
  const decodedPhone = decodeURIComponent(phone).replace(/\s+/, '');
  const phoneCountry = countryForPhone(decodedPhone);
  if (phoneCountry) {
    const phoneCode = COUNTRIES_PHONE_CODES[phoneCountry];
    const countryCode = [...phoneCode.split(''), ' '];
    return countryCode.concat(...Array(maxPhoneSize - countryCode.length).fill(digitRegex));
  }
  return ['+'].concat(...Array(maxPhoneSize - 1).fill(digitRegex));
}
