
/**
 * Converts numbers to words (Indian currency format).
 * Note: This is a simplified version. For production use with all edge cases, consider a robust library or a more complete implementation.
 */
export const amountToWords = (amount: number): string => {
    if (amount === 0) return "Zero Only";

    // Round to 2 decimal places
    amount = Math.round(Math.abs(amount) * 100) / 100;

    const parts = amount.toString().split('.');
    const integerPart = parseInt(parts[0]);
    const decimalPart = parts.length > 1 ? parseInt(parts[1]) : 0;

    const units = ['', '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'];

    function convertGroup(n: number): string {
        if (n < 20) return units[n];
        const digit = n % 10;
        if (n < 100) return tens[Math.floor(n / 10)] + (digit ? " " + units[digit] : "");
        if (n < 1000) return units[Math.floor(n / 100)] + " Hundred" + (n % 100 == 0 ? "" : " " + convertGroup(n % 100));
        return convertGroup(n);
    }

    let words = "";
    let residual = integerPart;

    if (residual >= 10000000) {
        words += convertGroup(Math.floor(residual / 10000000)) + " Crore ";
        residual %= 10000000;
    }

    if (residual >= 100000) {
        words += convertGroup(Math.floor(residual / 100000)) + " Lakh ";
        residual %= 100000;
    }

    if (residual >= 1000) {
        words += convertGroup(Math.floor(residual / 1000)) + " Thousand ";
        residual %= 1000;
    }

    if (residual > 0) {
        words += convertGroup(residual);
    }

    if (decimalPart > 0) {
        words += " and " + convertGroup(decimalPart) + " Paise";
    }

    return (words.trim() + " Only").replace(/\s+/g, ' ');
};
