import {CouponsPlus} from "../../globals";
import getSymbolFromCurrency from "currency-symbol-map";

export const getWithCurrency = (amount?: number | string) => {
    const currencySymbol = getSymbolFromCurrency(CouponsPlus.woocommerce.currency);
    const currencyPosition = CouponsPlus.woocommerce.currencyPosition;

    let formattedAmount = '';

    if (amount !== undefined) {
        // Check if it's a string representing a float (contains a dot)
        const isFloat = typeof amount === 'string'
            ? amount.includes('.')
            : Number(amount) % 1 !== 0;

        if (isFloat) {
            // Format with two decimal places
            formattedAmount = typeof amount === 'string'
                ? parseFloat(amount).toLocaleString(undefined, { minimumFractionDigits: 2, maximumFractionDigits: 2 })
                : amount.toLocaleString(undefined, { minimumFractionDigits: 2, maximumFractionDigits: 2 });
        } else {
            // Regular formatting for integers
            formattedAmount = typeof amount === 'string'
                ? parseFloat(amount).toLocaleString()
                : amount.toLocaleString();
        }
    }

    if (currencyPosition.startsWith('left')) {
        return `${currencySymbol}${formattedAmount}`.trim();
    } else if (currencyPosition.startsWith('right')) {
        return `${formattedAmount}${currencySymbol}`.trim();
    }
}