"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.GETTERS_BN = exports.GETTERS_BASIC = exports.GETTERS_LISTS = exports.GETTERS_AMOUNTS = exports.getRandomNumber = exports.getTestFile = exports.throwOrReturnSameGetterAmounts = exports.throwOrReturnSameGetterBN = exports.throwOrReturnSameGetterList = exports.throwOrReturnSameGetter = exports.AddressAmountEqual = exports.singleAddressAmountEqual = exports.sendMinimalUTXOTransaction = exports.round_for_ltc = exports.expectThrow = void 0;
const chai_1 = require("chai");
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const expectThrow = async (method, errorMessage) => {
    let error = null;
    try {
        await method;
        // eslint-disable-next-line @typescript-eslint/no-explicit-any
    }
    catch (err) {
        error = err;
    }
    if (typeof errorMessage === "object") {
        // eslint-disable-next-line guard-for-in
        for (const prop in errorMessage) {
            (0, chai_1.expect)(error).to.haveOwnProperty(prop);
            (0, chai_1.expect)(error[prop]).to.eq(errorMessage[prop]);
        }
    }
    else {
        (0, chai_1.expect)(error).to.equal(errorMessage);
    }
};
exports.expectThrow = expectThrow;
function round_for_ltc(input) {
    return parseFloat(input.toFixed(8));
}
exports.round_for_ltc = round_for_ltc;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
async function sendMinimalUTXOTransaction(RPC, fromWalletLabel, from, to) {
    const unspent = await RPC.listUnspentTransactions(fromWalletLabel);
    const vin = [];
    const vinAddresses = [];
    let unspentAmount = 0;
    for (const utx of unspent) {
        console.log("ALL utxo", utx);
        if (utx.address == from) {
            console.log(utx);
            vin.push({
                txid: utx.txid,
                vout: utx.vout,
            });
            vinAddresses.push(utx.address);
            unspentAmount += utx.amount;
        }
    }
    const gas = 1e-5;
    const test_amount = 1e-5;
    if (unspentAmount < gas + test_amount) {
        console.error("Not enough founds on sender wallet");
        throw Error("Insufficient founds on sender wallet");
    }
    const vout = [
        { address: to, amount: test_amount },
        { address: from, amount: round_for_ltc(unspentAmount - test_amount - gas) },
    ];
    const a = await RPC.createRawTransaction(fromWalletLabel, vin, vout);
    const signKeys = [];
    for (const add of vinAddresses) {
        signKeys.push(await RPC.getPrivateKey(fromWalletLabel, add));
    }
    const signedTx = await RPC.signRawTransaction(fromWalletLabel, a, signKeys);
    const txId = await RPC.sendRawTransactionInBlock(fromWalletLabel, signedTx.hex);
    return txId;
}
exports.sendMinimalUTXOTransaction = sendMinimalUTXOTransaction;
function singleAddressAmountEqual(a, b) {
    return a.address == b.address && a.amount.toString() == b.amount.toString() && a.elementaryUnits == b.elementaryUnits && a.utxo == b.utxo;
}
exports.singleAddressAmountEqual = singleAddressAmountEqual;
function AddressAmountEqual(a, b) {
    if (a.length != b.length) {
        return false;
    }
    const sortParam = (x, y) => {
        if (!x.address || !y.address)
            return 1;
        if (x.address == y.address)
            return 0;
        if (x.address > y.address)
            return 1;
        else
            return -1;
    };
    a.sort(sortParam);
    b.sort(sortParam);
    for (let i = 0; i < a.length; i++) {
        if (singleAddressAmountEqual(a[i], b[i]) == false) {
            return false;
        }
    }
    return true;
}
exports.AddressAmountEqual = AddressAmountEqual;
// eslint-disable-next-line @typescript-eslint/ban-types
function throwOrReturnSameGetter(c1, c2, getter) {
    let result1, result2;
    try {
        // eslint-disable-next-line @typescript-eslint/ban-ts-comment
        // @ts-ignore
        result1 = c1[getter];
    }
    catch (error1) {
        try {
            // eslint-disable-next-line @typescript-eslint/ban-ts-comment
            // @ts-ignore
            c2[getter];
        }
        catch (error2) {
            // Test that both functions threw the same error
            (0, chai_1.expect)(error1.message).to.equal(error2.message);
            return;
        }
        // If function2 didn't throw an error, fail the test
        chai_1.expect.fail("Function 1 threw an error but function 2 did not");
    }
    try {
        // eslint-disable-next-line @typescript-eslint/ban-ts-comment
        // @ts-ignore
        result2 = c2[getter];
    }
    catch (error2) {
        // If function1 returned a value, fail the test
        chai_1.expect.fail("Function 1 returned a value but function 2 threw an error");
    }
    // Test that both functions returned the same value
    (0, chai_1.expect)(result1).to.deep.equal(result2);
}
exports.throwOrReturnSameGetter = throwOrReturnSameGetter;
// eslint-disable-next-line @typescript-eslint/ban-types
function throwOrReturnSameGetterList(c1, c2, getter) {
    let result1, result2;
    try {
        // eslint-disable-next-line @typescript-eslint/ban-ts-comment
        // @ts-ignore
        result1 = c1[getter];
    }
    catch (error1) {
        try {
            // eslint-disable-next-line @typescript-eslint/ban-ts-comment
            // @ts-ignore
            c2[getter];
        }
        catch (error2) {
            // Test that both functions threw the same error
            (0, chai_1.expect)(error1.message).to.equal(error2.message);
            return;
        }
        // If function2 didn't throw an error, fail the test
        chai_1.expect.fail("Function 1 threw an error but function 2 did not");
    }
    try {
        // eslint-disable-next-line @typescript-eslint/ban-ts-comment
        // @ts-ignore
        result2 = c2[getter];
    }
    catch (error2) {
        // If function1 returned a value, fail the test
        chai_1.expect.fail("Function 1 returned a value but function 2 threw an error");
    }
    // Test that both functions returned the same value
    (0, chai_1.expect)(result1.sort()).to.deep.equal(result2.sort());
}
exports.throwOrReturnSameGetterList = throwOrReturnSameGetterList;
// eslint-disable-next-line @typescript-eslint/ban-types
function throwOrReturnSameGetterBN(c1, c2, getter) {
    let result1, result2;
    try {
        // eslint-disable-next-line @typescript-eslint/ban-ts-comment
        // @ts-ignore
        result1 = c1[getter];
    }
    catch (error1) {
        try {
            // eslint-disable-next-line @typescript-eslint/ban-ts-comment
            // @ts-ignore
            c2[getter];
        }
        catch (error2) {
            // Test that both functions threw the same error
            (0, chai_1.expect)(error1.message).to.equal(error2.message);
            return;
        }
        // If function2 didn't throw an error, fail the test
        chai_1.expect.fail("Function 1 threw an error but function 2 did not");
    }
    try {
        // eslint-disable-next-line @typescript-eslint/ban-ts-comment
        // @ts-ignore
        result2 = c2[getter];
    }
    catch (error2) {
        // If function1 returned a value, fail the test
        chai_1.expect.fail("Function 1 returned a value but function 2 threw an error");
    }
    // Test that both functions returned the same value
    (0, chai_1.expect)(result1.toString()).to.deep.equal(result2.toString());
}
exports.throwOrReturnSameGetterBN = throwOrReturnSameGetterBN;
// eslint-disable-next-line @typescript-eslint/ban-types
function throwOrReturnSameGetterAmounts(c1, c2, getter) {
    let result1, result2;
    try {
        // eslint-disable-next-line @typescript-eslint/ban-ts-comment
        // @ts-ignore
        result1 = c1[getter];
    }
    catch (error1) {
        try {
            // eslint-disable-next-line @typescript-eslint/ban-ts-comment
            // @ts-ignore
            c2[getter];
        }
        catch (error2) {
            // Test that both functions threw the same error
            (0, chai_1.expect)(error1.message).to.equal(error2.message);
            return;
        }
        // If function2 didn't throw an error, fail the test
        chai_1.expect.fail("Function 1 threw an error but function 2 did not");
    }
    try {
        // eslint-disable-next-line @typescript-eslint/ban-ts-comment
        // @ts-ignore
        result2 = c2[getter];
    }
    catch (error2) {
        // If function1 returned a value, fail the test
        chai_1.expect.fail("Function 1 returned a value but function 2 threw an error");
    }
    // Test that both functions returned the same value
    (0, chai_1.assert)(AddressAmountEqual(result1, result2));
}
exports.throwOrReturnSameGetterAmounts = throwOrReturnSameGetterAmounts;
/**
 * Returns truncated file path.
 * @param file module filename
 * @returns file path from `test/` on, separated by `'/'`
 */
function getTestFile(myFile) {
    return myFile.slice(myFile.replace(/\\/g, "/").indexOf("test/"));
}
exports.getTestFile = getTestFile;
function getRandomNumber(min, max) {
    return Math.floor(Math.random() * (max - min + 1) + min);
}
exports.getRandomNumber = getRandomNumber;
//Getters in transaction class
exports.GETTERS_AMOUNTS = ["spentAmounts", "intendedSpentAmounts", "receivedAmounts", "intendedReceivedAmounts"]; //getters with return type AddressAmount[]
exports.GETTERS_LISTS = ["reference", "sourceAddresses", "receivingAddresses"]; // getters with return type string[]
exports.GETTERS_BASIC = [
    "txid",
    "stdTxid",
    "hash",
    "stdPaymentReference",
    "unixTimestamp",
    "successStatus",
    "type",
    "isNativePayment",
    "currencyName",
    "elementaryUnits",
    "feeSignerTotalAmount",
]; // getters with return type string
exports.GETTERS_BN = ["fee"];
//# sourceMappingURL=testUtils.js.map