"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.mockVaultsApiGetMethod = exports.createMockVaultWithBacking = exports.mockComputeCollateralInStakingPoolMethod = exports.createMockAccountId = exports.prepareBackingCollateralProportionMocks = exports.prepareRegisterNewCollateralVaultMocks = exports.MOCKED_SEND_LOGGED_ERR_MSG = void 0;
const src_1 = require("../../../src");
const allThingsEncoding = __importStar(require("../../../src/utils/encoding"));
exports.MOCKED_SEND_LOGGED_ERR_MSG = "mocked sendLogged rejection";
/**
 *
 * @returns Two mock account IDs, one for the nominator and one for the vault
 * @param sinon The sinon sandbox to use for mocking
 * @param vaultsApi The vaults API used to call the method under test
 * @param stubbedTransactionApi The stubbed transaction API (called internally)
 * @param isAccountIdUndefined if true, mocks that transactionApi.getAccountId() returns an undefined account ID
 * @param doesSendLoggedReject if true, mocks that transactionApi.sendLogged() rejects
 * @returns the mocked extrinsic to return when transactionApi.sendLogged() is finally called
 */
const prepareRegisterNewCollateralVaultMocks = (sinon, vaultsApi, stubbedTransactionApi, isAccountIdUndefined, doesSendLoggedReject) => {
    if (isAccountIdUndefined) {
        stubbedTransactionApi.getAccount.returns(undefined);
        return null;
    }
    // mock getting a valid (ie. has been set) account id
    const vaultAccountId = exports.createMockAccountId("0x0123456789012345678901234567890123456789012345678901234567890123");
    stubbedTransactionApi.getAccount.returns(vaultAccountId);
    // mock api returns to be able to call sendLogged
    const mockSubmittableExtrinsic = {};
    sinon.stub(vaultsApi, "buildRegisterVaultExtrinsic").returns(mockSubmittableExtrinsic);
    const fakeEvent = {};
    sinon.stub(vaultsApi, "getRegisterVaultEvent").returns(fakeEvent);
    if (doesSendLoggedReject) {
        stubbedTransactionApi.sendLogged.rejects(new Error(exports.MOCKED_SEND_LOGGED_ERR_MSG));
        return null;
    }
    stubbedTransactionApi.sendLogged.resolves();
    return mockSubmittableExtrinsic;
};
exports.prepareRegisterNewCollateralVaultMocks = prepareRegisterNewCollateralVaultMocks;
/**
 * Helper function to mock calls outside of the function backingCollateralProportion
 * @param sinon The sinon sandbox to use for mocking
 * @param vaultsApi The vaults API used to call the method under test
 * @param stubbedRewardsApi The stubbed rewards API (called internally)
 * @param nominatorCollateralStakedAmount The mocked nominator's collateral staked amount
 * @param vaultBackingCollateralAmount The mocked vault's backing collateral amount
 * @param collateralCurrency The collateral currency for the amounts mocked
 * @returns Two mock account IDs, one for the nominator and one for the vault
 */
const prepareBackingCollateralProportionMocks = (sinon, vaultsApi, stubbedRewardsApi, nominatorCollateralStakedAmount, vaultBackingCollateralAmount, collateralCurrency) => {
    const nominatorId = exports.createMockAccountId("mock nominator account id");
    const vaultId = exports.createMockAccountId("mock vault account id");
    // prepare mocks
    const mockVault = exports.createMockVaultWithBacking(vaultBackingCollateralAmount, collateralCurrency);
    exports.mockVaultsApiGetMethod(sinon, vaultsApi, mockVault);
    exports.mockComputeCollateralInStakingPoolMethod(sinon, stubbedRewardsApi, nominatorCollateralStakedAmount, collateralCurrency);
    return {
        nominatorId,
        vaultId,
    };
};
exports.prepareBackingCollateralProportionMocks = prepareBackingCollateralProportionMocks;
/**
 * Construct a mocked AccountId
 * @param someString An identifier to use for the mock account
 * @returns A simplified variation of an AccountId
 */
const createMockAccountId = (someString) => {
    return {
        toString: () => someString,
        toHuman: () => toString(),
        toRawType: () => "MockAccountId",
        eq: (other) => !!other && toString() === other.toString(),
    };
};
exports.createMockAccountId = createMockAccountId;
/**
 * Mock RewardsAPI.computeCollateralInStakingPool to return a specific collateral amount
 * @param sinon The sinon sandbox to use for mocking
 * @param stubbedRewardsApi A stubbed rewards API to add the mocked bahvior to
 * @param amount The mocked return amount
 * @param currency The currency of the mocked return amount
 */
const mockComputeCollateralInStakingPoolMethod = (sinon, stubbedRewardsApi, amount, currency) => {
    // don't care what the inner method returns as we mock the outer one
    const tempId = {};
    sinon.stub(allThingsEncoding, "newVaultId").returns(tempId);
    // the actual mock that matters
    stubbedRewardsApi.computeCollateralInStakingPool.resolves(src_1.newMonetaryAmount(amount, currency));
};
exports.mockComputeCollateralInStakingPoolMethod = mockComputeCollateralInStakingPoolMethod;
/**
 * Create a mock VaultExt instance with only backing collateral amount set
 * @param amount The mocked backing collateral amount
 * @param collateralCurrency The collateral currency for the mocked backing collateral amount
 * @returns A mocked VaultExt instance with only backingCollateral property set
 */
const createMockVaultWithBacking = (amount, collateralCurrency) => {
    // VaultExt-ish; only need .backingCollateral to be available for this test
    return {
        backingCollateral: src_1.newMonetaryAmount(amount, collateralCurrency),
    };
};
exports.createMockVaultWithBacking = createMockVaultWithBacking;
/**
 * Mock the return value of VaultsAPI.get
 * @param sinon The sinon sandbox to use for mocking
 * @param vaultsApi The vaultsAPI instance for which to mock .get() methed
 * @param vault The vault to return
 */
const mockVaultsApiGetMethod = (sinon, vaultsApi, vault) => {
    // make VaultAPI.get() return a mocked vault
    sinon.stub(vaultsApi, "get").returns(Promise.resolve(vault));
};
exports.mockVaultsApiGetMethod = mockVaultsApiGetMethod;
//# sourceMappingURL=vaultsTestMocks.js.map