<?php

if (!function_exists('icylab_country_options')) {
    function icylab_country_options()
    {
        return array(
            array('name' => 'Chile', 'code' => 'cl', 'phone_prefix' => '+56', 'flag' => '🇨🇱'),
            array('name' => 'Argentina', 'code' => 'ar', 'phone_prefix' => '+54', 'flag' => '🇦🇷'),
            array('name' => 'Perú', 'code' => 'pe', 'phone_prefix' => '+51', 'flag' => '🇵🇪'),
            array('name' => 'Uruguay', 'code' => 'uy', 'phone_prefix' => '+598', 'flag' => '🇺🇾'),
            array('name' => 'Brasil', 'code' => 'br', 'phone_prefix' => '+55', 'flag' => '🇧🇷'),
            array('name' => 'Colombia', 'code' => 'co', 'phone_prefix' => '+57', 'flag' => '🇨🇴'),
            array('name' => 'Ecuador', 'code' => 'ec', 'phone_prefix' => '+593', 'flag' => '🇪🇨'),
            array('name' => 'México', 'code' => 'mx', 'phone_prefix' => '+52', 'flag' => '🇲🇽'),
            array('name' => 'Paraguay', 'code' => 'py', 'phone_prefix' => '+595', 'flag' => '🇵🇾'),
            array('name' => 'Bolivia', 'code' => 'bo', 'phone_prefix' => '+591', 'flag' => '🇧🇴'),
            array('name' => 'Estados Unidos', 'code' => 'us', 'phone_prefix' => '+1', 'flag' => '🇺🇸'),
            array('name' => 'España', 'code' => 'es', 'phone_prefix' => '+34', 'flag' => '🇪🇸'),
            array('name' => 'Otro', 'code' => '', 'phone_prefix' => '', 'flag' => '🌐'),
        );
    }
}

if (!function_exists('icylab_country_code_from_value')) {
    function icylab_country_code_from_value($value)
    {
        $value = trim((string)$value);
        if ($value === '') return 'cl';

        $normalized = mb_strtolower($value, 'UTF-8');
        $normalized = strtr($normalized, array(
            'á' => 'a', 'é' => 'e', 'í' => 'i', 'ó' => 'o', 'ú' => 'u', 'ñ' => 'n',
            'Á' => 'a', 'É' => 'e', 'Í' => 'i', 'Ó' => 'o', 'Ú' => 'u', 'Ñ' => 'n'
        ));

        if (preg_match('/^[a-z]{2}$/', $normalized)) {
            return $normalized;
        }

        foreach (icylab_country_options() as $country) {
            $name = mb_strtolower($country['name'], 'UTF-8');
            $name = strtr($name, array('á'=>'a','é'=>'e','í'=>'i','ó'=>'o','ú'=>'u','ñ'=>'n'));
            if ($normalized === $name) {
                return (string)$country['code'];
            }
        }

        $aliases = array(
            'peru' => 'pe',
            'mexico' => 'mx',
            'usa' => 'us',
            'eeuu' => 'us',
            'estados unidos de america' => 'us',
            'united states' => 'us',
            'united states of america' => 'us',
            'espana' => 'es',
            'global' => '',
            'otro' => '',
            'other' => '',
        );

        return array_key_exists($normalized, $aliases) ? $aliases[$normalized] : 'cl';
    }
}

if (!function_exists('icylab_country_name_from_code')) {
    function icylab_country_name_from_code($code)
    {
        $code = strtolower(trim((string)$code));
        foreach (icylab_country_options() as $country) {
            if ($country['code'] === $code) return $country['name'];
        }
        return $code === '' ? 'Otro' : 'Chile';
    }
}
