"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.bytesToRippleAddress = exports.rippleAddressToBytes = exports.processFlags = exports.unixEpochToRippleTime = exports.rippleTimeToUnixEpoch = exports.xrp_ensure_data = void 0;
const types_1 = require("../types");
const constants_1 = require("./constants");
const errors_1 = require("./errors");
const utils_1 = require("./utils");
// eslint-disable-next-line @typescript-eslint/no-var-requires
const XrpAddress = require("ripple-address-codec");
////////////////////////////
//// MCC Error handling ////
////////////////////////////
// eslint-disable-next-line @typescript-eslint/no-explicit-any
function xrp_ensure_data(data) {
    if (data.result.status === "error") {
        if (data.result.error === "txnNotFound") {
            throw new errors_1.mccError(errors_1.mccErrorCode.InvalidTransaction);
        }
        if (data.result.error === "lgrNotFound") {
            throw new errors_1.mccError(errors_1.mccErrorCode.InvalidBlock);
        }
        throw (0, utils_1.MccError)(data);
    }
}
exports.xrp_ensure_data = xrp_ensure_data;
function rippleTimeToUnixEpoch(timestamp) {
    return timestamp + constants_1.XRP_UTD;
}
exports.rippleTimeToUnixEpoch = rippleTimeToUnixEpoch;
function unixEpochToRippleTime(timestamp) {
    return timestamp - constants_1.XRP_UTD;
}
exports.unixEpochToRippleTime = unixEpochToRippleTime;
function processFlags(flag) {
    const altFlags = [];
    const flagPos = [16, 17, 18, 19, 20, 21, 22, 23, 24];
    for (const posFlag of flagPos) {
        if ((flag >> posFlag) % 2 === 1) {
            // eslint-disable-next-line @typescript-eslint/ban-ts-comment
            // @ts-ignore
            altFlags.push(types_1.PosToFlag[posFlag]);
        }
    }
    return altFlags;
}
exports.processFlags = processFlags;
///////////////////////////
// Xrp address <-> Bytes //
///////////////////////////
function rippleAddressToBytes(address) {
    if (address.length > 0) {
        if (address[0] === "r") {
            return XrpAddress.decodeAccountID(address);
        }
        else {
            // it is a ripple x address
            const classic = XrpAddress.xAddressToClassicAddress(address);
            return XrpAddress.decodeAccountID(classic.classicAddress);
        }
    }
    return Buffer.from([0x00]);
}
exports.rippleAddressToBytes = rippleAddressToBytes;
function bytesToRippleAddress(byts) {
    if (byts.length === 20) {
        // it is a valid address
        return XrpAddress.encodeAccountID(byts);
    }
    throw new Error("Not a valid ripple address");
}
exports.bytesToRippleAddress = bytesToRippleAddress;
//# sourceMappingURL=xrpUtils.js.map