<?php

if (!function_exists('whatsapp_link')) {
    function whatsapp_link(string $phone, string $message = ''): string
    {
        $phone = preg_replace('/[\s\-\(\)]/', '', $phone);
        if (str_starts_with($phone, '0')) {
            $phone = '20' . substr($phone, 1);
        } elseif (str_starts_with($phone, '+')) {
            $phone = substr($phone, 1);
        }

        $url = "https://wa.me/{$phone}";
        if ($message) {
            $url .= '?text=' . urlencode($message);
        }

        return $url;
    }
}
