<?php

if (!function_exists('formatCnpjCpf')) {

    function formatCnpjCpf($value) {
        $value = preg_replace('/\D/', '', $value);
        if (strlen($value) === 11) {
            return preg_replace("/(\d{3})(\d{3})(\d{3})(\d{2})/", "$1.$2.$3-$4", $value);
        }
        if (strlen($value) === 14) {
            return preg_replace("/(\d{2})(\d{3})(\d{3})(\d{4})(\d{2})/", "$1.$2.$3/$4-$5", $value);
        }
        return $value;
    }

}

if (!function_exists('isExternalLink')) {

    /**
     * Verifica se uma URL é um link externo
     *
     * @param string $url
     * @return bool
     */
    function isExternalLink($url) {
        if (empty($url) || !is_string($url)) {
            return false;
        }
        if (strpos($url, 'http') !== 0) {
            return false; // rotas internas não começam com http
        }
        $parsed = parse_url($url);
        $host = $parsed['host'] ?? '';
        return !empty($host) && !str_ends_with($host, '.angra.rj.gov.br') && $host !== 'angra.rj.gov.br';
    }

}
