import { currency } from "../helpers/money";

export default {
    methods: {
        currency(money, overrideCurrency = null) {
            // console.log(this?.$page?.props.currency);
            if (!money) money = 0;

            const currency = {
                code_en:
                    overrideCurrency ||
                    this?.$page?.props.currency ||
                    this?.activeCurrency() ||
                    "USD",
            };

            money = this?.exchangeCurrency(money, overrideCurrency);

            return (
                new Intl.NumberFormat("en-US", {
                    style: "currency",
                    currency: currency.code_en,
                    currencyDisplay: "narrowSymbol",
                    maximumFractionDigits: 0,
                    minimumFractionDigits: 0,
                }).format(money) + (currency.code_en == "USD" ? " USD" : "")
            );
        },
        activeCurrency() {
            // get active currency from local storage
            return localStorage.getItem("currency") || "USD";
        },
        // exchange currency from usd to active currency
        exchangeCurrency(money, overrideCurrency = null) {
            const currency =
                overrideCurrency ||
                this?.$page?.props.currency ||
                this?.activeCurrency() ||
                "USD";
            if (
                !this?.$page?.props.exchange_rates ||
                !this?.$page?.props.exchange_rates[currency]
            )
                return money;
            return money * this?.$page?.props.exchange_rates[currency];
        },
    },
    // watch for changes in the currency
};
