"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getCurrencyShortSymbol = exports.getCurrencySymbol = exports.getCurrencyItems = exports.getRatesForCurrencyCode = exports.loadExchangeRates = exports.convertPrice = void 0;
const BookingAPI_1 = __importDefault(require("../api/BookingAPI"));
const FullPageBookingEngineElementEvents_1 = require("../engines/FullPageBookingEngine/FullPageBookingEngineElementEvents");
const Currency_1 = require("../models/Currency");
const currencies = {
    CAD: new Currency_1.Currency('CA$', 'Canadian Dollars', '$'),
    HKD: new Currency_1.Currency('HK$', 'Hong Kong Dollars', '$', {
        CAD: 0.176960404,
        HKD: 1,
        ISK: 17.4644168298,
        // PHP: 6.5122123525, // — placeholder value; FE should fetch live rates from ExchangeRateAPI
        DKK: 0.8632841905,
        HUF: 40.3870429778,
        CZK: 3.1177690018,
        GBP: 0.1043325188,
        RON: 0.5616060778,
        SEK: 1.2145181651,
        IDR: 1884.222960844,
        INR: 9.7400604538,
        BRL: 0.6908636085,
        RUB: 9.0844615331,
        HRK: 0.8786639953,
        JPY: 13.8151875572,
        THB: 4.10251659,
        CHF: 0.124150231,
        EUR: 0.1158117827,
        MYR: 0.5606911647,
        BGN: 0.2265046846,
        TRY: 0.8813392475,
        CNY: 0.9201941005,
        NOK: 1.2493775117,
        NZD: 0.2068745874,
        ZAR: 2.2495164858,
        USD: 0.1289680012,
        MXN: 2.8454955007,
        SGD: 0.181963473,
        AUD: 0.1931856347,
        ILS: 0.4524187291,
        KRW: 159.3813334569,
        PLN: 0.5153045271,
    }),
    // ISK: new Currency("Kr", "Iceland Kronur"),
    PHP: new Currency_1.Currency('₱', 'Philippine Pesos'),
    // DKK: new Currency("kr", "Danish Kroner"),
    // HUF: new Currency("Ft", "Hungarian Forint"),
    // CZK: new Currency("Kč", "Czech Koruna"),
    GBP: new Currency_1.Currency('£', 'British Pounds'),
    // RON: new Currency("L", "Romania New Lei"),
    // SEK: new Currency("kr", "Swedish Kronor"),
    IDR: new Currency_1.Currency('Rp', 'Indonesian Rupiahs'),
    INR: new Currency_1.Currency('₹', 'Indian Rupees'),
    // BRL: new Currency("R$", "Brazilian Reals"),
    // RUB: new Currency("р.", "Russian Rubles"),
    // HRK: new Currency("Kn", "Croatian Kuna"),
    JPY: new Currency_1.Currency('¥', 'Japanese Yen'),
    // THB: new Currency("฿", "Thai Baht"),
    // CHF: new Currency("₣", "Swiss Francs"),
    EUR: new Currency_1.Currency('€', 'Euros'),
    // BGN: new Currency("Лв", "Bulgarian Leva"),
    // TRY: new Currency("₺", "Turkish Lira"),
    CNY: new Currency_1.Currency('¥', 'Chinese Yuan'),
    // NOK: new Currency("kr", "Norwegian Kroner"),
    NZD: new Currency_1.Currency('NZ$', 'New Zealand Dollars', '$'),
    // ZAR: new Currency("R", "South African Rand"),
    USD: new Currency_1.Currency('$', 'US Dollars'),
    // MXN: new Currency("MX$", "Mexican Pesos", "$"),
    SGD: new Currency_1.Currency('S$', 'Singapore Dollars', '$'),
    AUD: new Currency_1.Currency('A$', 'Australian Dollars', '$', {
        CAD: 0.923580786,
        HKD: 5.2259218826,
        ISK: 91.5817564289,
        // PHP: 33.9319505095,
        DKK: 4.5214095099,
        HUF: 209.5160116448,
        CZK: 16.2900291121,
        GBP: 0.5438682678,
        RON: 2.9377122756,
        SEK: 6.3461305192,
        IDR: 9849.8786996604,
        INR: 50.9085395439,
        BRL: 3.5950994663,
        RUB: 46.9661572052,
        HRK: 4.6030446385,
        JPY: 72.62857836,
        THB: 21.3512857836,
        CHF: 0.6481077147,
        EUR: 0.6065016982,
        MYR: 2.9114507521,
        BGN: 1.1861960213,
        TRY: 4.5938258127,
        CNY: 4.8111960213,
        NOK: 6.5435468219,
        NZD: 1.0810286269,
        ZAR: 11.765708394,
        USD: 0.6741872877,
        MXN: 14.8301795245,
        SGD: 0.9503275109,
        AUD: 1,
        ILS: 2.362142164,
        KRW: 827.5351770985,
        PLN: 2.6854682193,
    }),
    // ILS: new Currency("₪", "New Israeli Shekels"),
    // KRW: new Currency("₩", "South Korean Won"),
    // PLN: new Currency("zł", "Polish Złoty"),
    FJD: new Currency_1.Currency('FJ$', 'Fiji Dollar', '$'),
    /*
   * The following currencies are currently excluded as the exchange rate API doesn't return them
  AED: new Currency("د.إ", "UAE Dirham"),
  AFN: new Currency("Af", "Afghani"),
  ALL: new Currency("L", "Lek"),
  AMD: new Currency("Դ", "Armenian Dram"),
  AOA: new Currency("Kz", "Kwanza"),
  ARS: new Currency("$", "Argentine Peso"),
  AWG: new Currency("ƒ", "Aruban Guilder/Florin"),
  AZN: new Currency("ман", "Azerbaijanian Manat"),
  BAM: new Currency("КМ", "Konvertibilna Marka"),
  BBD: new Currency("$", "Barbados Dollar"),
  BDT: new Currency("৳", "Taka"),
  BHD: new Currency("ب.د", "Bahraini Dinar"),
  BIF: new Currency("₣", "Burundi Franc"),
  BMD: new Currency("$", "Bermudian Dollar"),
  BND: new Currency("$", "Brunei Dollar"),
  BOB: new Currency("Bs.", "Boliviano"),
  BSD: new Currency("$", "Bahamian Dollar"),
  BTN: new Currency("", "Ngultrum"),
  BWP: new Currency("P", "Pula"),
  BYN: new Currency("Br", "Belarusian Ruble"),
  BZD: new Currency("$", "Belize Dollar"),
  CDF: new Currency("₣", "Congolese Franc"),
  CLP: new Currency("$", "Chilean Peso"),
  COP: new Currency("$", "Colombian Peso"),
  CRC: new Currency("₡", "Costa Rican Colon"),
  CUP: new Currency("$", "Cuban Peso"),
  CVE: new Currency("$", "Cape Verde Escudo"),
  DJF: new Currency("₣", "Djibouti Franc"),
  DOP: new Currency("$", "Dominican Peso"),
  DZD: new Currency("د.ج", "Algerian Dinar"),
  EGP: new Currency("£", "Egyptian Pound"),
  ERN: new Currency("Nfk", "Nakfa"),
  ETB: new Currency("", "Ethiopian Birr"),
  FKP: new Currency("£", "Falkland Islands Pound"),
  GEL: new Currency("ლ", "Lari"),
  GHS: new Currency("₵", "Cedi"),
  GIP: new Currency("£", "Gibraltar Pound"),
  GMD: new Currency("D", "Dalasi"),
  GNF: new Currency("₣", "Guinea Franc"),
  GTQ: new Currency("Q", "Quetzal"),
  GYD: new Currency("$", "Guyana Dollar"),
  HNL: new Currency("L", "Lempira"),
  HTG: new Currency("G", "Gourde"),
  IQD: new Currency("ع.د", "Iraqi Dinar"),
  IRR: new Currency("﷼", "Iranian Rial"),
  JMD: new Currency("$", "Jamaican Dollar"),
  JOD: new Currency("د.ا", "Jordanian Dinar"),
  KES: new Currency("Sh", "Kenyan Shilling"),
  KGS: new Currency("", "Som"),
  KHR: new Currency("៛", "Riel"),
  KPW: new Currency("₩", "North Korean Won"),
  KWD: new Currency("د.ك", "Kuwaiti Dinar"),
  KYD: new Currency("$", "Cayman Islands Dollar"),
  KZT: new Currency("〒", "Tenge"),
  LAK: new Currency("₭", "Kip"),
  LBP: new Currency("ل.ل", "Lebanese Pound"),
  LKR: new Currency("Rs", "Sri Lanka Rupee"),
  LRD: new Currency("$", "Liberian Dollar"),
  LSL: new Currency("L", "Loti"),
  LYD: new Currency("ل.د", "Libyan Dinar"),
  MAD: new Currency("د.م.", "Moroccan Dirham"),
  MDL: new Currency("L", "Moldovan Leu"),
  MGA: new Currency("", "Malagasy Ariary"),
  MKD: new Currency("ден", "Denar"),
  MMK: new Currency("K", "Kyat"),
  MNT: new Currency("₮", "Tugrik"),
  MOP: new Currency("P", "Pataca"),
  MRU: new Currency("UM", "Ouguiya"),
  MUR: new Currency("₨", "Mauritius Rupee"),
  MVR: new Currency("ރ.", "Rufiyaa"),
  MWK: new Currency("MK", "Kwacha"),
  MYR: new Currency("RM", "Malaysian Ringgit"),
  MZN: new Currency("MTn", "Metical"),
  NAD: new Currency("$", "Namibia Dollar"),
  NGN: new Currency("₦", "Naira"),
  NIO: new Currency("C$", "Cordoba Oro"),
  NPR: new Currency("₨", "Nepalese Rupee"),
  OMR: new Currency("ر.ع.", "Rial Omani"),
  PAB: new Currency("B/.", "Balboa"),
  PEN: new Currency("S/.", "Nuevo Sol"),
  PGK: new Currency("K", "Kina"),
  PKR: new Currency("₨", "Pakistan Rupee"),
  PYG: new Currency("₲", "Guarani"),
  QAR: new Currency("ر.ق", "Qatari Rial"),
  RSD: new Currency("din", "Serbian Dinar"),
  RWF: new Currency("₣", "Rwanda Franc"),
  SAR: new Currency("ر.س", "Saudi Riyal"),
  SBD: new Currency("$", "Solomon Islands Dollar"),
  SCR: new Currency("₨", "Seychelles Rupee"),
  SDG: new Currency("£", "Sudanese Pound"),
  SHP: new Currency("£", "Saint Helena Pound"),
  SLL: new Currency("Le", "Leone"),
  SOS: new Currency("Sh", "Somali Shilling"),
  SRD: new Currency("$", "Suriname Dollar"),
  STN: new Currency("Db", "Dobra"),
  SYP: new Currency("ل.س", "Syrian Pound"),
  SZL: new Currency("L", "Lilangeni"),
  TJS: new Currency("ЅМ", "Somoni"),
  TMT: new Currency("m", "Manat"),
  TND: new Currency("د.ت", "Tunisian Dinar"),
  TOP: new Currency("T$", "Pa’anga"),
  TTD: new Currency("$", "Trinidad and Tobago Dollar"),
  TWD: new Currency("$", "Taiwan Dollar"),
  TZS: new Currency("Sh", "Tanzanian Shilling"),
  UAH: new Currency("₴", "Hryvnia"),
  UGX: new Currency("Sh", "Uganda Shilling"),
  UYU: new Currency("$", "Peso Uruguayo"),
  UZS: new Currency("", "Uzbekistan Sum"),
  VEF: new Currency("Bs F", "Bolivar Fuerte"),
  VND: new Currency("₫", "Dong"),
  VUV: new Currency("Vt", "Vatu"),
  WST: new Currency("T", "Tala"),
  XAF: new Currency("₣", "CFA Franc BCEAO"),
  XCD: new Currency("$", "East Caribbean Dollar"),
  XPF: new Currency("₣", "CFP Franc"),
  YER: new Currency("﷼", "Yemeni Rial"),
  ZMW: new Currency("ZK", "Zambian Kwacha"),
  ZWL: new Currency("$", "Zimbabwe Dollar"),
   */
};
const labelOverrides = {};
const convertPrice = (price, includeDecimalPlaces, to, from) => {
    const amount = _convertPrice(price, to, from);
    return includeDecimalPlaces ? preciseRound(amount, 2) : Math.round(amount);
};
exports.convertPrice = convertPrice;
const preciseRound = (num, decimals) => {
    const factor = Math.pow(10, decimals);
    const sign = num < 0 ? -1 : num > 0 ? 1 : 0;
    return Math.round(num * factor + (decimals > 0 ? 1 : 0) * (sign * (10 / Math.pow(100, decimals)))) / factor;
};
const _convertPrice = (price, to, from) => {
    var _a, _b, _c, _d;
    if (typeof price !== 'number') {
        price = parseFloat(price);
    }
    if (to === from) {
        return price;
    }
    if (!((_a = currencies[from]) === null || _a === void 0 ? void 0 : _a.rates)) {
        (0, exports.loadExchangeRates)(from);
    }
    return price * ((_d = (_c = (_b = currencies[from]) === null || _b === void 0 ? void 0 : _b.rates) === null || _c === void 0 ? void 0 : _c[to]) !== null && _d !== void 0 ? _d : 1);
};
const loadExchangeRates = (currencyCode) => __awaiter(void 0, void 0, void 0, function* () {
    const currency = currencies[currencyCode];
    if (!currency) {
        throw new Error(currencyCode + ' is not a supported currency code');
    }
    if (currency.rates) {
        return;
    }
    yield BookingAPI_1.default.ExchangeRate.fetchData(currencyCode).then((rates) => {
        currency.rates = rates;
    });
    FullPageBookingEngineElementEvents_1.FullPageBookingEngineElementEvents.reload(); // HMM
});
exports.loadExchangeRates = loadExchangeRates;
const getRatesForCurrencyCode = (code) => {
    var _a;
    return (_a = currencies[code]) === null || _a === void 0 ? void 0 : _a.rates;
};
exports.getRatesForCurrencyCode = getRatesForCurrencyCode;
const getCurrencyItems = () => {
    const currencyItems = Object.keys(currencies).map((code) => ({
        text: code + ' - ' + currencies[code].name,
        value: code,
    }));
    return currencyItems.sort((a, b) => a.value.localeCompare(b.value));
};
exports.getCurrencyItems = getCurrencyItems;
const getCurrencySymbol = (currencyCode) => {
    var _a, _b, _c, _d;
    if ((_a = labelOverrides[currencyCode]) === null || _a === void 0 ? void 0 : _a.long) {
        return (_b = labelOverrides[currencyCode]) === null || _b === void 0 ? void 0 : _b.long;
    }
    return (_d = (_c = currencies[currencyCode]) === null || _c === void 0 ? void 0 : _c.symbol) !== null && _d !== void 0 ? _d : '';
};
exports.getCurrencySymbol = getCurrencySymbol;
const getCurrencyShortSymbol = (currencyCode) => {
    var _a, _b, _c, _d;
    if ((_a = labelOverrides[currencyCode]) === null || _a === void 0 ? void 0 : _a.short) {
        return (_b = labelOverrides[currencyCode]) === null || _b === void 0 ? void 0 : _b.short;
    }
    return (_d = (_c = currencies[currencyCode]) === null || _c === void 0 ? void 0 : _c.shortSymbol) !== null && _d !== void 0 ? _d : '';
};
exports.getCurrencyShortSymbol = getCurrencyShortSymbol;
//# sourceMappingURL=CurrencyHelper.js.map