"use strict";
var __extends = (this && this.__extends) || (function () {
    var extendStatics = function (d, b) {
        extendStatics = Object.setPrototypeOf ||
            ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
            function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
        return extendStatics(d, b);
    };
    return function (d, b) {
        if (typeof b !== "function" && b !== null)
            throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.Currency = void 0;
var hash_160_1 = require("./hash-160");
var buffer_1 = require("buffer/");
var XRP_HEX_REGEX = /^0{40}$/;
var ISO_REGEX = /^[A-Z0-9]{3}$/;
var HEX_REGEX = /^[A-F0-9]{40}$/;
// eslint-disable-next-line no-control-regex
var STANDARD_FORMAT_HEX_REGEX = /^0{24}[\x00-\x7F]{6}0{10}$/;
/**
 * Convert an ISO code to a currency bytes representation
 */
function isoToBytes(iso) {
    var bytes = buffer_1.Buffer.alloc(20);
    if (iso !== 'XRP') {
        var isoBytes = iso.split('').map(function (c) { return c.charCodeAt(0); });
        bytes.set(isoBytes, 12);
    }
    return bytes;
}
/**
 * Tests if ISO is a valid iso code
 */
function isIsoCode(iso) {
    return ISO_REGEX.test(iso);
}
function isoCodeFromHex(code) {
    var iso = code.toString();
    if (iso === 'XRP') {
        return null;
    }
    if (isIsoCode(iso)) {
        return iso;
    }
    return null;
}
/**
 * Tests if hex is a valid hex-string
 */
function isHex(hex) {
    return HEX_REGEX.test(hex);
}
/**
 * Tests if a string is a valid representation of a currency
 */
function isStringRepresentation(input) {
    return input.length === 3 || isHex(input);
}
/**
 * Tests if a Buffer is a valid representation of a currency
 */
function isBytesArray(bytes) {
    return bytes.byteLength === 20;
}
/**
 * Ensures that a value is a valid representation of a currency
 */
function isValidRepresentation(input) {
    return input instanceof buffer_1.Buffer
        ? isBytesArray(input)
        : isStringRepresentation(input);
}
/**
 * Generate bytes from a string or buffer representation of a currency
 */
function bytesFromRepresentation(input) {
    if (!isValidRepresentation(input)) {
        throw new Error("Unsupported Currency representation: ".concat(input));
    }
    return input.length === 3 ? isoToBytes(input) : buffer_1.Buffer.from(input, 'hex');
}
/**
 * Class defining how to encode and decode Currencies
 */
var Currency = /** @class */ (function (_super) {
    __extends(Currency, _super);
    function Currency(byteBuf) {
        var _this = _super.call(this, byteBuf !== null && byteBuf !== void 0 ? byteBuf : Currency.XRP.bytes) || this;
        var hex = _this.bytes.toString('hex');
        if (XRP_HEX_REGEX.test(hex)) {
            _this._iso = 'XRP';
        }
        else if (STANDARD_FORMAT_HEX_REGEX.test(hex)) {
            _this._iso = isoCodeFromHex(_this.bytes.slice(12, 15));
        }
        else {
            _this._iso = null;
        }
        return _this;
    }
    /**
     * Return the ISO code of this currency
     *
     * @returns ISO code if it exists, else null
     */
    Currency.prototype.iso = function () {
        return this._iso;
    };
    /**
     * Constructs a Currency object
     *
     * @param val Currency object or a string representation of a currency
     */
    Currency.from = function (value) {
        if (value instanceof Currency) {
            return value;
        }
        if (typeof value === 'string') {
            return new Currency(bytesFromRepresentation(value));
        }
        throw new Error('Cannot construct Currency from value given');
    };
    /**
     * Gets the JSON representation of a currency
     *
     * @returns JSON representation
     */
    Currency.prototype.toJSON = function () {
        var iso = this.iso();
        if (iso !== null) {
            return iso;
        }
        return this.bytes.toString('hex').toUpperCase();
    };
    Currency.XRP = new Currency(buffer_1.Buffer.alloc(20));
    return Currency;
}(hash_160_1.Hash160));
exports.Currency = Currency;
//# sourceMappingURL=currency.js.map