<?php

class Currency_Utils {
    private static array $zeroDecimalISOCurrencies = [
        "bif", "clp", "djf", "gnf", "jpy", "kmf", "krw",
        "mga", "pyg", "rwf", "ugx", "vnd", "vuv", "xaf", "xof", "xpf"
    ];

    private static array $threeDecimalISOCurrencies = [
        "bhd", "jod", "kwd", "omr", "tnd"
    ];

    private static array $currencySymbols = [
        "afn" => "؋",
        "usd" => "$",
        "xcd" => "EC$",
        "amd" => "Դ",
        "awg" => "ƒ",
        "aud" => "AU$",
        "azn" => "₼",
        "bsd" => "B$",
        "bdt" => "৳",
        "bbd" => "Bds$",
        "byn" => "Br",
        "bzd" => "BZ$",
        "xof" => "Franc ",
        "bmd" => "BD$",
        "btn" => "Nu",
        "bob" => "Bs. ",
        "bov" => "Mvdol ",
        "bam" => "КМ",
        "bwp" => "P",
        "nok" => "kr",
        "brl" => "R$",
        "bgn" => "лв",
        "bif" => "₣",
        "khr" => "៛",
        "xaf" => "FCFA ",
        "cad" => "CA$",
        "kyd" => "CI$",
        "cny" => "¥",
        "cdf" => "₣",
        "nzd" => "NZ$",
        "crc" => "₡",
        "hrk" => "Kn",
        "cup" => "₱",
        "ang" => "ƒ",
        "eur" => "€",
        "czk" => "Kč",
        "djf" => "₣",
        "dop" => "RD$",
        "ern" => "Nfk",
        "etb" => "Br",
        "fkp" => "FK£",
        "fjd" => "FJ$",
        "xpf" => "₣",
        "gmd" => "D",
        "gel" => "ლ",
        "ghs" => "₵",
        "dkk" => "kr.",
        "gtq" => "Q",
        "gnf" => "₣",
        "gyd" => "G$",
        "htg" => "G",
        "hnl" => "L",
        "hkd" => "HK$",
        "huf" => "Ft",
        "isk" => "Kr",
        "inr" => "₹",
        "idr" => "Rp",
        "irr" => "﷼",
        "ils" => "₪",
        "jmd" => "JA$",
        "jpy" => "¥",
        "kzt" => "〒",
        "kes" => "Sh",
        "kgs" => "лв",
        "lak" => "₭",
        "mop" => "P",
        "mga" => "MK",
        "mwk" => "MK",
        "myr" => "RM",
        "mvr" => "ރ",
        "mru" => "UM",
        "mur" => "₨",
        "mxn" => "MX$",
        "mxv" => "-",
        "mdl" => "L",
        "mnt" => "₮",
        "mzn" => "MTn",
        "nad" => "N$",
        "zar" => "R",
        "npr" => "₨",
        "nio" => "C$",
        "ngn" => "₦",
        "omr" => "﷼",
        "pkr" => "₨",
        "pab" => "B/.",
        "pgk" => "K",
        "pyg" => "₲",
        "pen" => "S/.",
        "php" => "₱",
        "pln" => "zł",
        "mkd" => "ден",
        "ron" => "L",
        "rub" => "₽",
        "rwf" => "₣",
        "wst" => "WS$",
        "stn" => "Db",
        "rsd" => "din",
        "scr" => "Rs",
        "sll" => "Le",
        "sgd" => "S$",
        "sbd" => "SI$",
        "sos" => "Sh",
        "ssp" => "SS£",
        "lkr" => "Rs",
        "srd" => "Sr$",
        "szl" => "L",
        "sek" => "kr",
        "chf" => "₣",
        "twd" => "NT$",
        "tjs" => "ЅМ",
        "tzs" => "Sh",
        "thb" => "฿",
        "top" => "T$",
        "ttd" => "TT$",
        "try" => "TL",
        "tmt" => "m",
        "ugx" => "Sh",
        "uah" => "₴",
        "gbp" => "£",
        "uzs" => "лв",
        "vuv" => "Vt",
        "vef" => "Bs.",
        "vnd" => "₫",
        "zmw" => "ZK",
        "zwl" => "Z$"
    ];

    public static function getShortestFormFromLong(?int $amount, ?string $isoCurrency): ?float {
        if ($amount === null) {
            return null;
        }

        $isoCurrency = strtolower($isoCurrency ?? 'usd');

        if (in_array($isoCurrency, self::$zeroDecimalISOCurrencies)) {
            return (float)$amount;
        } elseif (in_array($isoCurrency, self::$threeDecimalISOCurrencies)) {
            return $amount / 1000.0;
        }

        return $amount / 100.0;
    }

    public static function getShortestFormFromDouble(?float $amount, ?string $isoCurrency): ?float {
        if ($amount === null) {
            return null;
        }

        $isoCurrency = strtolower($isoCurrency ?? 'usd');

        if (in_array($isoCurrency, self::$zeroDecimalISOCurrencies)) {
            return $amount;
        } elseif (in_array($isoCurrency, self::$threeDecimalISOCurrencies)) {
            return $amount / 1000.0;
        }

        return $amount / 100.0;
    }

    public static function getSymbol(string $isoCurrency): string {
        $isoCurrency = strtolower($isoCurrency);
        return self::$currencySymbols[$isoCurrency] ?? ($isoCurrency . " ");
    }
}