<?php

namespace App\Modules\IpStack\Repositories;

use Stevebauman\Location\Facades\Location;

class IpStackRepository
{
    /**
     * Get ip stack
     */
    public static function isLocalIp(string $ip): bool
    {
        return ! filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE);
    }

    /**
     * Get country code from ip
     */
    public static function getCountryCodeFromIp(?string $ip): string
    {
        if (is_null($ip)) {
            return 'TM';
        }

        return self::isLocalIp($ip) ? 'TM' : Location::get($ip)->countryCode ?? 'TM';
    }
}
