<?php

namespace App\Support;

class EmailAddress
{
    public static function normalize(string $email): string
    {
        return strtolower(trim($email));
    }

    public static function isValid(string $email): bool
    {
        return $email !== '' && filter_var($email, FILTER_VALIDATE_EMAIL) !== false;
    }

    /**
     * Mask a local-part for OTP copy, e.g. vi***@example.com
     */
    public static function mask(string $email): string
    {
        $email = self::normalize($email);
        $at    = strrpos($email, '@');
        if ($at === false) {
            return $email;
        }

        $local  = substr($email, 0, $at);
        $domain = substr($email, $at);
        $keep   = min(2, strlen($local));

        return substr($local, 0, $keep) . '***' . $domain;
    }
}
