<?php

function cleanWhatsappNumber($phone){

    $phone = preg_replace('/[^0-9]/', '', $phone);

    /*
    ============================================
    SI NO TIENE PREFIJO
    ============================================
    */

    if(strlen($phone) == 9){

        $phone = '34' . $phone;

    }

    return $phone;
}

function buildWhatsappMessage(
    $name,
    $campaign = '',
    $advisor = ''
){

    $message = "Hola";

    if($name != ''){

        $message .= " " . $name;

    }

    $message .= ", soy";

    if($advisor != ''){

        $message .= " " . $advisor;

    } else {

        $message .= " del equipo Wolfxell";

    }

    if($campaign != ''){

        $message .= " de " . $campaign;

    }

    $message .= ". ";

    $message .= "Te contacto para ayudarte con tu solicitud energética.";

    return urlencode($message);
}

function whatsappLink(
    $phone,
    $message = ''
){

    $clean =
    cleanWhatsappNumber($phone);

    return
    "https://wa.me/" .
    $clean .
    "?text=" .
    $message;
}