"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getIncompatibleCurrencyKeys = exports.countries = exports.USStates = exports.initSwap = exports.postSwapCancelled = exports.maybeKeepTronAccountAlive = exports.maybeTronEmptyAccount = exports.maybeTezosAccountUnrevealedAccount = exports.getExchangeRates = exports.postSwapAccepted = exports.getCompleteSwapHistory = exports.getSwapAPIVersion = exports.getSwapUserIP = exports.getSwapAPIBaseURL = exports.getSwapAPIError = exports.isSwapOperationPending = exports.operationStatusList = exports.getAvailableProviders = void 0;
const live_env_1 = require("@ledgerhq/live-env");
const errors_1 = require("../../errors");
const getCompleteSwapHistory_1 = __importDefault(require("./getCompleteSwapHistory"));
exports.getCompleteSwapHistory = getCompleteSwapHistory_1.default;
const initSwap_1 = __importDefault(require("./initSwap"));
exports.initSwap = initSwap_1.default;
const postSwapState_1 = require("./postSwapState");
Object.defineProperty(exports, "postSwapAccepted", { enumerable: true, get: function () { return postSwapState_1.postSwapAccepted; } });
Object.defineProperty(exports, "postSwapCancelled", { enumerable: true, get: function () { return postSwapState_1.postSwapCancelled; } });
const getExchangeRates_1 = __importDefault(require("./getExchangeRates"));
exports.getExchangeRates = getExchangeRates_1.default;
const maybeTezosAccountUnrevealedAccount_1 = require("./maybeTezosAccountUnrevealedAccount");
Object.defineProperty(exports, "maybeTezosAccountUnrevealedAccount", { enumerable: true, get: function () { return maybeTezosAccountUnrevealedAccount_1.maybeTezosAccountUnrevealedAccount; } });
const maybeTronEmptyAccount_1 = require("./maybeTronEmptyAccount");
Object.defineProperty(exports, "maybeTronEmptyAccount", { enumerable: true, get: function () { return maybeTronEmptyAccount_1.maybeTronEmptyAccount; } });
const maybeKeepTronAccountAlive_1 = require("./maybeKeepTronAccountAlive");
Object.defineProperty(exports, "maybeKeepTronAccountAlive", { enumerable: true, get: function () { return maybeKeepTronAccountAlive_1.maybeKeepTronAccountAlive; } });
const getIncompatibleCurrencyKeys_1 = require("./getIncompatibleCurrencyKeys");
Object.defineProperty(exports, "getIncompatibleCurrencyKeys", { enumerable: true, get: function () { return getIncompatibleCurrencyKeys_1.getIncompatibleCurrencyKeys; } });
var providers_1 = require("../providers");
Object.defineProperty(exports, "getAvailableProviders", { enumerable: true, get: function () { return providers_1.getAvailableProviders; } });
exports.operationStatusList = {
    finishedOK: ["finished"],
    finishedKO: ["refunded"],
};
// A swap operation is considered pending if it is not in a finishedOK or finishedKO state
const isSwapOperationPending = status => !exports.operationStatusList.finishedOK.includes(status) &&
    !exports.operationStatusList.finishedKO.includes(status);
exports.isSwapOperationPending = isSwapOperationPending;
const getSwapAPIBaseURL = () => (0, live_env_1.getEnv)("SWAP_API_BASE");
exports.getSwapAPIBaseURL = getSwapAPIBaseURL;
const getSwapUserIP = () => {
    const SWAP_USER_IP = (0, live_env_1.getEnv)("SWAP_USER_IP");
    if (SWAP_USER_IP) {
        return { "X-Forwarded-For": SWAP_USER_IP };
    }
    return undefined;
};
exports.getSwapUserIP = getSwapUserIP;
const SWAP_API_BASE_PATTERN = /.*\/v(?<version>\d+)\/*$/;
const getSwapAPIVersion = () => {
    const version = Number(getSwapAPIBaseURL().match(SWAP_API_BASE_PATTERN)?.groups?.version);
    if (version == null || isNaN(version)) {
        throw new errors_1.SwapGenericAPIError("Configured swap API base URL is invalid, should end with /v<number>");
    }
    return version;
};
exports.getSwapAPIVersion = getSwapAPIVersion;
const USStates = {
    AL: "Alabama",
    AK: "Alaska",
    AZ: "Arizona",
    AR: "Arkansas",
    CA: "California",
    CO: "Colorado",
    CT: "Connecticut",
    DE: "Delaware",
    DC: "District Of Columbia",
    FL: "Florida",
    GA: "Georgia",
    HI: "Hawaii",
    ID: "Idaho",
    IL: "Illinois",
    IN: "Indiana",
    IA: "Iowa",
    KS: "Kansas",
    KY: "Kentucky",
    LA: "Louisiana",
    ME: "Maine",
    MD: "Maryland",
    MA: "Massachusetts",
    MI: "Michigan",
    MN: "Minnesota",
    MS: "Mississippi",
    MO: "Missouri",
    MT: "Montana",
    NE: "Nebraska",
    NV: "Nevada",
    NH: "New Hampshire",
    NJ: "New Jersey",
    NM: "New Mexico",
    NY: "New York",
    NC: "North Carolina",
    ND: "North Dakota",
    OH: "Ohio",
    OK: "Oklahoma",
    OR: "Oregon",
    PA: "Pennsylvania",
    RI: "Rhode Island",
    SC: "South Carolina",
    SD: "South Dakota",
    TN: "Tennessee",
    TX: "Texas",
    UT: "Utah",
    VT: "Vermont",
    VA: "Virginia",
    WA: "Washington",
    WV: "West Virginia",
    WI: "Wisconsin",
    WY: "Wyoming",
};
exports.USStates = USStates;
const countries = {
    US: "United States",
};
exports.countries = countries;
const swapBackendErrorCodes = {
    "100": errors_1.JSONRPCResponseError,
    "101": errors_1.JSONDecodeError,
    "200": errors_1.NoIPHeaderError,
    "300": errors_1.CurrencyNotSupportedError,
    "301": errors_1.CurrencyDisabledError,
    "302": errors_1.CurrencyDisabledAsInputError,
    "303": errors_1.CurrencyDisabledAsOutputError,
    "304": errors_1.CurrencyNotSupportedByProviderError,
    "400": errors_1.TradeMethodNotSupportedError,
    "500": errors_1.UnexpectedError,
    "600": errors_1.NotImplementedError,
    "700": errors_1.ValidationError,
    "701": errors_1.AccessDeniedError,
};
const getSwapAPIError = (errorCode, errorMessage) => {
    if (errorCode in swapBackendErrorCodes)
        return new swapBackendErrorCodes[errorCode](errorMessage);
    return new Error(errorMessage);
};
exports.getSwapAPIError = getSwapAPIError;
//# sourceMappingURL=index.js.map