function pf(phone) {
  let phoneWa = phone.replace(/\D/g, '');

  /* put all phone numbers in here if you don't want to be formatted! */
  // const exceptions = ['5542920015920', '5542920015921'];
  // if (exceptions.includes(phoneWa)) return phoneWa;

  if (phoneWa === '') {
    // DO NOTHING
    //
    /* Indonesia formatting */
  } else if (phoneWa.substr(0, 2) == '08') {
    phoneWa = phoneWa.replace(/08/, '628');
  } else if (phoneWa.substr(0, 4) == '6208') {
    phoneWa = phoneWa.replace(/6208/, '628');
    /* Indonesia formatting */

    /* Italy formatting */
  } else if (phoneWa.substr(0, 1) == '3' && (phoneWa.length === 9 || phoneWa.length === 10)) {
    phoneWa = '39' + phoneWa;
    /* Italy formatting */

    /* Nigeria formatting */
  } else if (phoneWa.substr(0, 4) == '2340') {
    phoneWa = phoneWa.replace(/2340/, '234');
    /* Nigeria formatting */

    /* Mexico formatting */
  } else if (phoneWa.substr(0, 2) == '52' && phoneWa.substr(2, 1) != '1') {
    phoneWa = phoneWa.replace(/52/, '521');
    /* Mexico formatting */

    /* Argentina formatting */
  } else if (phoneWa.substr(0, 2) == '54' && phoneWa.substr(2, 1) != '9') {
    phoneWa = phoneWa.replace(/54/, '549');
    /* Argentina formatting */

    /* Brazil formatting */
  } else if (phoneWa.substr(0, 2) == '55' && phoneWa.length == 13) {
    let isCellularPost = phoneWa.substr(-9).startsWith('92') || phoneWa.substr(-9).startsWith('933');
    if (isCellularPost) return phoneWa;

    let ddd = parseInt(phoneWa.substr(2, 2));
    if (ddd > 30) {
      phoneWa = '55' + ddd + phoneWa.substr(-8);
    }
    /* Brazil formatting */
  }
  return phoneWa;
}

function s_wa(phone) {
  return `${pf(phone)}@s.whatsapp.net`;
}

function g_us(phone) {
  return `${pf(phone)}@g.us`;
}

function c_us(phone) {
  return `${pf(phone)}@c.us`;
}

module.exports = { pf, s_wa, g_us, c_us };
