<?php
namespace EasyChat\Library\Messenger;

final class SmsPhoneNumber {
    public static function normalize($value) {
        $raw = trim((string)$value);
        $has_explicit_country = strpos($raw, '+') === 0;
        $digits = preg_replace('/\D+/', '', $raw);

        if (strlen($digits) === 11 && $digits[0] === '8') {
            $digits = '7' . substr($digits, 1);
        } elseif (!$has_explicit_country && strlen($digits) === 10 && $digits[0] === '9') {
             
             
            $digits = '7' . $digits;
        }

        if (strlen($digits) < 10 || strlen($digits) > 15) {
            return '';
        }

        return '+' . $digits;
    }

    public static function lookupVariants($normalized) {
        $normalized = self::normalize($normalized);
        if ($normalized === '') {
            return [];
        }

        $digits = substr($normalized, 1);
        $variants = [$normalized, $digits];

        if (strlen($digits) === 11 && $digits[0] === '7') {
            $national = substr($digits, 1);
            $variants[] = '8' . $national;
            $variants[] = '+8' . $national;
            $variants[] = $national;
            $variants[] = '+' . $national;
        }

        return array_values(array_unique(array_filter($variants)));
    }
}
