<?php

namespace common\modules\orderPayment\WOP\OnlinePayments\Core\BusinessLogic\Domain\Checkout;

use common\modules\orderPayment\WOP\OnlinePayments\Core\BusinessLogic\Domain\Checkout\Exceptions\InvalidCurrencyCode;
use common\modules\orderPayment\WOP\OnlinePayments\Core\BusinessLogic\Domain\Translations\Model\TranslatableLabel;
/**
 * Class Currency
 *
 * @package OnlinePayments\Core\BusinessLogic\Domain\Checkout\PaymentRequest\Models\Amount
 */
class Currency
{
    private const DEFAULT_MINOR_UNITS = 2;
    private const DEFAULT_CURRENCY = 'EUR';
    /**
     * Mapping between all the currencies and their respective amount minor units.
     *
     * @var array<string, int>
     */
    /* <auto-generated> */
    private static array $map = ['BHD' => 3, 'XOF' => 0, 'BIF' => 0, 'XAF' => 0, 'CLP' => 0, 'CLF' => 4, 'KMF' => 0, 'DJF' => 0, 'XPF' => 0, 'GNF' => 0, 'ISK' => 0, 'IQD' => 3, 'JPY' => 0, 'JOD' => 3, 'KRW' => 0, 'KWD' => 3, 'LYD' => 3, 'OMR' => 3, 'PYG' => 0, 'RWF' => 0, 'TND' => 3, 'UGX' => 0, 'UYI' => 0, 'UYW' => 4, 'VUV' => 0, 'VND' => 0];
    /* </auto-generated> */
    /**
     * @var string
     */
    private string $isoCode;
    /**
     * @param string $isoCode
     */
    private function __construct(string $isoCode)
    {
        $this->isoCode = strtoupper($isoCode);
    }
    /**
     * @return string
     */
    public function getIsoCode(): string
    {
        return $this->isoCode;
    }
    /**
     * @param string $code
     * @return Currency
     * @throws InvalidCurrencyCode
     */
    public static function fromIsoCode(string $code): Currency
    {
        if (strlen($code) !== 3) {
            throw new InvalidCurrencyCode(new TranslatableLabel("Invalid currency code %s provided. The three-letter ISO currency is expected (e.g. 'EUR').", 'checkout.invalidCurrencyCode', [$code]));
        }
        return new self($code);
    }
    /**
     * Return default currency
     */
    public static function getDefault(): Currency
    {
        return new self(self::DEFAULT_CURRENCY);
    }
    /**
     * Returns the number of minor units for the provided currency.
     *
     * @return int
     */
    public function getMinorUnits(): int
    {
        if (!array_key_exists($this->getIsoCode(), self::$map)) {
            return self::DEFAULT_MINOR_UNITS;
        }
        return self::$map[$this->getIsoCode()];
    }
    public function __toString(): string
    {
        return $this->isoCode;
    }
    /**
     * Is two currencies same
     *
     * @param Currency $currency
     * @return bool
     */
    public function equal(Currency $currency): bool
    {
        return $this->isoCode === $currency->getIsoCode();
    }
}
