<?php


namespace Easymailing\App\Utils;


class Api
{
	public static function extractUuid($identifier): ?string
	{
		if (!$identifier) {
			return null;
		}
		$data = explode("/", $identifier);
		return end($data);
	}

	public static function convertEurToCents($eur): int
	{
		if ( ! is_numeric($eur)) {
			return 0;
		}
		return (int) round((float) $eur * 100);
	}

	public static function getClientIP()
	{
		$ipAddress = '';

		if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
			// IP desde Internet compartido
			$ipAddress = $_SERVER['HTTP_CLIENT_IP'];
		} elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
			// IP pasada desde el proxy
			$ipAddress = $_SERVER['HTTP_X_FORWARDED_FOR'];
		} elseif (!empty($_SERVER['HTTP_X_FORWARDED'])) {
			$ipAddress = $_SERVER['HTTP_X_FORWARDED'];
		} elseif (!empty($_SERVER['HTTP_FORWARDED_FOR'])) {
			$ipAddress = $_SERVER['HTTP_FORWARDED_FOR'];
		} elseif (!empty($_SERVER['HTTP_FORWARDED'])) {
			$ipAddress = $_SERVER['HTTP_FORWARDED'];
		} elseif (!empty($_SERVER['REMOTE_ADDR'])) {
			// IP desde la conexión remota
			$ipAddress = $_SERVER['REMOTE_ADDR'];
		}

		// Si hay múltiples IPs en HTTP_X_FORWARDED_FOR, selecciona la primera
		if (strpos($ipAddress, ',') !== false) {
			$ipList = explode(',', $ipAddress);
			$ipAddress = trim($ipList[0]);
		}

		return $ipAddress;
	}


}
