const currencyNames = {
    'INR': 'Indian Rupees',
    'USD': 'Dollars',
    'EUR': 'Euros',
    'GBP': 'Pounds',
    'AUD': 'Australian Dollars',
    'CAD': 'Canadian Dollars',
    'CHF': 'Swiss Francs',
    'CNY': 'Yuan',
    'JPY': 'Yen',
    'NZD': 'New Zealand Dollars',
    'SEK': 'Kronor',
    'RUB': 'Rubles'
};

function convertToWords(amount, currency) {
    const belowTwenty = [
        "Zero", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine",
        "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"
    ];
    const tens = ["", "", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"];
/*     const scalesINR = ["", "Thousand", "Lakh", "Crore"];
    const scalesOthers = ["", "Thousand", "Million", "Billion"];
 */
    function convertThreeDigitNumber(num) {
        let result = "";
        if (num >= 100) {
            result += belowTwenty[Math.floor(num / 100)] + " Hundred ";
            num %= 100;
        }
        if (num >= 20) {
            result += tens[Math.floor(num / 10)] + " ";
            num %= 10;
        }
        if (num > 0) {
            result += belowTwenty[num] + " ";
        }
        return result.trim();
    }

    function determineScale(amount, currency) {
        if (currency === "INR") {
            if (amount > 999 && amount < 100000) {
                return "Thousand";
            } else if (amount > 99999 && amount < 10000000) {
                return "Lakh";
            } else if (amount > 9999999) {
                return "Crore";
            }
        } else {
            if (amount >= 1000 && amount < 1000000) {
                return "Thousand";
            } else if (amount >= 1000000 && amount < 1000000000) {
                return "Million";
            } else if (amount >= 1000000000) {
                return "Billion";
            }
        }
        return "";
    }

    function convertToScale(amount, scale) {
        let divisor = 1;
        switch (scale) {
            case "Thousand":
                divisor = 1000;
                break;
            case "Lakh":
                divisor = 100000;
                break;
            case "Crore":
                divisor = 10000000;
                break;
            case "Million":
                divisor = 1000000;
                break;
            case "Billion":
                divisor = 1000000000;
                break;
        }
        return { numberInScale: Math.floor(amount / divisor), remainder: amount % divisor };
    }

    function convertFullNumber(amount, currency) {
        const scale = determineScale(amount, currency);
        if (scale) {
            const { numberInScale, remainder } = convertToScale(amount, scale);
            let result = convertThreeDigitNumber(numberInScale) + " " + scale;
            if (remainder > 0) {
                const nextScale = determineScale(remainder, currency);
                if (nextScale) {
                    result += " " + convertFullNumber(remainder, currency); // Recursively process larger remainders
                } else {
                    result += " " + convertThreeDigitNumber(remainder); // Handle smaller remainders directly
                }
            }
            return result.trim();
        } else {
            return convertThreeDigitNumber(amount); // For amounts less than or equal to 999
        }
    }

    return currencyNames[currency]+' '+convertFullNumber(amount, currency);
}
/* 
// Example usage:
console.log(convertToWords(39689300, "INR")); // "Three Crore Ninety-Six Lakh Eighty-Nine Thousand Three Hundred"
console.log(convertToWords(9500, "INR")); // "Nine Thousand Five Hundred"
console.log(convertToWords(150000, "INR")); // "One Lakh Fifty Thousand"
console.log(convertToWords(20000000, "INR")); // "Two Crore"
console.log(convertToWords(7500, "USD")); // "Seven Thousand Five Hundred"
console.log(convertToWords(1500000, "USD")); // "One Million Five Hundred Thousand"
console.log(convertToWords(2000000000, "EUR")); // "Two Billion" */

export { convertToWords, currencyNames };