/*******************************************************************************
***  This file is part of Pecunia.                                           ***
***                                                                          ***
***  Copyright (C) 2017, 2018, 2019, 2020, 2021, 2023                        ***
***  John Schneiderman <Licencing _AT_ Schneiderman _DOT_ me>                ***
***                                                                          ***
***  This program is free software: you can redistribute it and/or modify it ***
***  under the terms of the GNU Lesser General Public License as published   ***
***  by the Free Software Foundation, either version 3 of the License, or    ***
***  (at your option) any later version.                                     ***
***                                                                          ***
***  This program is distributed in the hope that it will be useful, but     ***
***  WITHOUT ANY WARRANTY; without even the implied warranty of              ***
***  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.                    ***
***  See the GNU Lesser General Public License for more details.             ***
***                                                                          ***
***  You should have received a copy of the GNU Lesser General Public License***
***  along with this program. If not, see <http://www.gnu.org/licenses/>.    ***
*******************************************************************************/
#include "Conversion.h"
using pecunia::MinorUnit;

#include <cmath>
using std::pow;
#include <string>
using std::string;
using std::to_string;
#include <stdexcept>
using std::overflow_error;

#include "Codes.h"
using pecunia::currency::Iso4217Codes;
using pecunia::currency::minorUnitDigits;
using pecunia::currency::toStdString;


// cppcheck-suppress unusedFunction
MinorUnit pecunia::toMinorUnit(const MinorUnit& minor, const Iso4217Codes& code)
{
	const auto digits{minorUnitDigits(code)};
	const auto conversionFactor{pow(10, digits)};

	if (minor >= conversionFactor)
		throw overflow_error{
			string{"The minor unit value "} + to_string(minor) + " is too large for the currency '"
				+ toStdString(code) + "'."
		};
	return static_cast<MinorUnit>(minor * conversionFactor);
}

