"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;
};
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const bignumber_js_1 = __importDefault(require("bignumber.js"));
const sinon_1 = __importDefault(require("sinon"));
const internal_1 = require("../../../../internal");
const mocks_1 = require("../../../../testUtils/mocks");
const types_1 = require("../../../../types");
const constants_1 = require("../../../../utils/constants");
const utilsConversionModule = __importStar(require("../../../../utils/conversion"));
const Settlements_1 = require("../Settlements");
describe('Settlements class', () => {
    let mockContext;
    let mockAsset;
    let settlements;
    let stringToAccountIdStub;
    let stringToTickerStub;
    let bigNumberToBalanceStub;
    let portfolioIdToMeshPortfolioIdStub;
    let portfolioLikeToPortfolioIdStub;
    let portfolioIdToPortfolioStub;
    let stringToIdentityIdStub;
    let rawAccountId;
    let rawTicker;
    let rawToDid;
    let rawAmount;
    let statusCode;
    let amount;
    let toDid;
    let ticker;
    let accountId;
    beforeAll(() => {
        mocks_1.entityMockUtils.initMocks();
        mocks_1.dsMockUtils.initMocks();
        toDid = 'toDid';
        statusCode = new bignumber_js_1.default(81);
        amount = new bignumber_js_1.default(100);
        stringToAccountIdStub = sinon_1.default.stub(utilsConversionModule, 'stringToAccountId');
        stringToTickerStub = sinon_1.default.stub(utilsConversionModule, 'stringToTicker');
        bigNumberToBalanceStub = sinon_1.default.stub(utilsConversionModule, 'bigNumberToBalance');
        portfolioIdToMeshPortfolioIdStub = sinon_1.default.stub(utilsConversionModule, 'portfolioIdToMeshPortfolioId');
        portfolioLikeToPortfolioIdStub = sinon_1.default.stub(utilsConversionModule, 'portfolioLikeToPortfolioId');
        portfolioIdToPortfolioStub = sinon_1.default.stub(utilsConversionModule, 'portfolioIdToPortfolio');
        stringToIdentityIdStub = sinon_1.default.stub(utilsConversionModule, 'stringToIdentityId');
        rawAmount = mocks_1.dsMockUtils.createMockBalance(amount);
    });
    beforeEach(() => {
        mockContext = mocks_1.dsMockUtils.getContextInstance();
        mockAsset = mocks_1.entityMockUtils.getAssetInstance();
        bigNumberToBalanceStub.withArgs(amount, mockContext, false).returns(rawAmount);
        settlements = new Settlements_1.Settlements(mockAsset, mockContext);
        ticker = mockAsset.ticker;
        // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
        accountId = mockContext.currentPair.address;
        rawAccountId = mocks_1.dsMockUtils.createMockAccountId(accountId);
        rawTicker = mocks_1.dsMockUtils.createMockTicker(ticker);
        rawToDid = mocks_1.dsMockUtils.createMockIdentityId(toDid);
        stringToAccountIdStub.withArgs(accountId, mockContext).returns(rawAccountId);
        stringToTickerStub.withArgs(ticker, mockContext).returns(rawTicker);
        stringToIdentityIdStub.withArgs(toDid, mockContext).returns(rawToDid);
    });
    afterEach(() => {
        mocks_1.dsMockUtils.reset();
        mocks_1.entityMockUtils.reset();
    });
    afterAll(() => {
        mocks_1.dsMockUtils.cleanup();
    });
    test('should extend namespace', () => {
        expect(Settlements_1.Settlements.prototype instanceof internal_1.Namespace).toBe(true);
    });
    describe('method: canSettle', () => {
        let fromDid;
        let fromPortfolioId;
        let toPortfolioId;
        let rawFromPortfolio;
        let rawToPortfolio;
        let rawFromDid;
        let fromPortfolio;
        let toPortfolio;
        beforeAll(() => {
            fromDid = 'fromDid';
            fromPortfolioId = { did: fromDid };
            toPortfolioId = { did: toDid };
            rawFromDid = mocks_1.dsMockUtils.createMockIdentityId(fromDid);
            rawFromPortfolio = mocks_1.dsMockUtils.createMockPortfolioId({ did: fromDid, kind: 'Default' });
            rawToPortfolio = mocks_1.dsMockUtils.createMockPortfolioId({ did: toDid, kind: 'Default' });
        });
        beforeEach(() => {
            fromPortfolio = mocks_1.entityMockUtils.getDefaultPortfolioInstance(fromPortfolioId);
            toPortfolio = mocks_1.entityMockUtils.getDefaultPortfolioInstance(toPortfolioId);
            portfolioLikeToPortfolioIdStub.withArgs(fromDid).returns(fromPortfolioId);
            portfolioLikeToPortfolioIdStub.withArgs(toDid).returns(toPortfolioId);
            portfolioLikeToPortfolioIdStub.withArgs(fromDid).returns(fromPortfolioId);
            portfolioIdToMeshPortfolioIdStub.withArgs(toPortfolioId, mockContext).returns(rawToPortfolio);
            portfolioIdToPortfolioStub.withArgs(fromPortfolioId, mockContext).returns(fromPortfolio);
            portfolioIdToPortfolioStub.withArgs(toPortfolioId, mockContext).returns(toPortfolio);
            stringToIdentityIdStub.withArgs(fromDid, mockContext).returns(rawFromDid);
        });
        test('should return a status value representing whether the transaction can be made from the current Identity', () => __awaiter(void 0, void 0, void 0, function* () {
            const currentIdentity = yield mockContext.getCurrentIdentity();
            const { did: currentDid } = currentIdentity;
            const rawCurrentDid = mocks_1.dsMockUtils.createMockIdentityId(currentDid);
            const rawDummyAccountId = mocks_1.dsMockUtils.createMockAccountId(constants_1.DUMMY_ACCOUNT_ID);
            const currentDefaultPortfolioId = { did: currentDid };
            fromPortfolio.getCustodian.resolves(mocks_1.entityMockUtils.getIdentityInstance({ did: currentDid }));
            toPortfolio.getCustodian.resolves(mocks_1.entityMockUtils.getIdentityInstance({ did: toDid }));
            portfolioLikeToPortfolioIdStub.withArgs(currentIdentity).returns(currentDefaultPortfolioId);
            portfolioIdToMeshPortfolioIdStub
                .withArgs(currentDefaultPortfolioId, mockContext)
                .returns(rawFromPortfolio);
            portfolioIdToPortfolioStub.withArgs(currentDefaultPortfolioId, mockContext).returns(mocks_1.entityMockUtils.getDefaultPortfolioInstance({
                did: currentDid,
            }));
            stringToIdentityIdStub.withArgs(currentDid, mockContext).returns(rawCurrentDid);
            // also test the case where the SDK was instanced without an account
            mockContext.currentPair = undefined;
            stringToAccountIdStub.withArgs(constants_1.DUMMY_ACCOUNT_ID, mockContext).returns(rawDummyAccountId);
            const rawResponse = mocks_1.dsMockUtils.createMockCanTransferResult({
                Ok: mocks_1.dsMockUtils.createMockU8(statusCode),
            });
            mocks_1.dsMockUtils
                .createRpcStub('asset', 'canTransfer')
                .withArgs(rawDummyAccountId, rawCurrentDid, rawFromPortfolio, rawToDid, rawToPortfolio, rawTicker, rawAmount)
                .returns(rawResponse);
            const result = yield settlements.canSettle({ to: toDid, amount });
            expect(result).toBe(types_1.TransferStatus.Success);
        }));
        test('should return a status value representing whether the transaction can be made from another Identity', () => __awaiter(void 0, void 0, void 0, function* () {
            const rawResponse = mocks_1.dsMockUtils.createMockCanTransferResult({
                Ok: mocks_1.dsMockUtils.createMockU8(statusCode),
            });
            fromPortfolio.getCustodian.resolves(mocks_1.entityMockUtils.getIdentityInstance({ did: fromDid }));
            toPortfolio.getCustodian.resolves(mocks_1.entityMockUtils.getIdentityInstance({ did: toDid }));
            portfolioIdToMeshPortfolioIdStub
                .withArgs({ did: fromDid }, mockContext)
                .returns(rawFromPortfolio);
            mocks_1.dsMockUtils
                .createRpcStub('asset', 'canTransfer')
                .withArgs(rawAccountId, rawFromDid, rawFromPortfolio, rawToDid, rawToPortfolio, rawTicker, rawAmount)
                .returns(rawResponse);
            const result = yield settlements.canSettle({ from: fromDid, to: toDid, amount });
            expect(result).toBe(types_1.TransferStatus.Success);
        }));
    });
    describe('method: canTransfer', () => {
        let fromDid;
        let fromPortfolioId;
        let toPortfolioId;
        let rawFromPortfolio;
        let rawToPortfolio;
        let rawFromDid;
        let fromPortfolio;
        let toPortfolio;
        let granularCanTransferResultToTransferBreakdownStub;
        beforeAll(() => {
            fromDid = 'fromDid';
            fromPortfolioId = { did: fromDid };
            toPortfolioId = { did: toDid };
            rawFromDid = mocks_1.dsMockUtils.createMockIdentityId(fromDid);
            rawFromPortfolio = mocks_1.dsMockUtils.createMockPortfolioId({ did: fromDid, kind: 'Default' });
            rawToPortfolio = mocks_1.dsMockUtils.createMockPortfolioId({ did: toDid, kind: 'Default' });
            granularCanTransferResultToTransferBreakdownStub = sinon_1.default.stub(utilsConversionModule, 'granularCanTransferResultToTransferBreakdown');
        });
        beforeEach(() => {
            fromPortfolio = mocks_1.entityMockUtils.getDefaultPortfolioInstance({
                did: fromDid,
            });
            toPortfolio = mocks_1.entityMockUtils.getDefaultPortfolioInstance({
                did: toDid,
            });
            portfolioLikeToPortfolioIdStub.withArgs(fromDid).returns(fromPortfolioId);
            portfolioLikeToPortfolioIdStub.withArgs(toDid).returns(toPortfolioId);
            portfolioIdToMeshPortfolioIdStub.withArgs(toPortfolioId, mockContext).returns(rawToPortfolio);
            portfolioIdToPortfolioStub.withArgs(fromPortfolioId, mockContext).returns(fromPortfolio);
            portfolioIdToPortfolioStub.withArgs(toPortfolioId, mockContext).returns(toPortfolio);
            stringToIdentityIdStub.withArgs(fromDid, mockContext).returns(rawFromDid);
        });
        test('should return a transfer breakdown representing whether the transaction can be made from the current Identity', () => __awaiter(void 0, void 0, void 0, function* () {
            const currentIdentity = yield mockContext.getCurrentIdentity();
            const { did: currentDid } = currentIdentity;
            const rawCurrentDid = mocks_1.dsMockUtils.createMockIdentityId(currentDid);
            const currentDefaultPortfolioId = { did: currentDid };
            portfolioLikeToPortfolioIdStub.withArgs(currentIdentity).returns(currentDefaultPortfolioId);
            portfolioIdToMeshPortfolioIdStub
                .withArgs(currentDefaultPortfolioId, mockContext)
                .returns(rawFromPortfolio);
            portfolioIdToPortfolioStub.withArgs(currentDefaultPortfolioId, mockContext).returns(mocks_1.entityMockUtils.getDefaultPortfolioInstance({
                did: currentDid,
                getCustodian: mocks_1.entityMockUtils.getIdentityInstance({ did: currentDid }),
            }));
            stringToIdentityIdStub.withArgs(currentDid, mockContext).returns(rawCurrentDid);
            toPortfolio.getCustodian.resolves(mocks_1.entityMockUtils.getIdentityInstance({ did: toDid }));
            const response = 'rpcResponse';
            mocks_1.dsMockUtils
                .createRpcStub('asset', 'canTransferGranular')
                .withArgs(rawCurrentDid, rawFromPortfolio, rawToDid, rawToPortfolio, rawTicker, rawAmount)
                .returns(response);
            const expected = 'breakdown';
            granularCanTransferResultToTransferBreakdownStub
                .withArgs(response, mockContext)
                .returns(expected);
            const result = yield settlements.canTransfer({ to: toDid, amount });
            expect(result).toEqual(expected);
        }));
        test('should return a transfer breakdown representing whether the transaction can be made from another Identity', () => __awaiter(void 0, void 0, void 0, function* () {
            const response = 'rpcResponse';
            portfolioIdToMeshPortfolioIdStub
                .withArgs({ did: fromDid }, mockContext)
                .returns(rawFromPortfolio);
            fromPortfolio.getCustodian.resolves(mocks_1.entityMockUtils.getIdentityInstance({ did: fromDid }));
            toPortfolio.getCustodian.resolves(mocks_1.entityMockUtils.getIdentityInstance({ did: toDid }));
            mocks_1.dsMockUtils
                .createRpcStub('asset', 'canTransferGranular')
                .withArgs(rawFromDid, rawFromPortfolio, rawToDid, rawToPortfolio, rawTicker, rawAmount)
                .returns(response);
            const expected = 'breakdown';
            granularCanTransferResultToTransferBreakdownStub
                .withArgs(response, mockContext)
                .returns(expected);
            const result = yield settlements.canTransfer({ from: fromDid, to: toDid, amount });
            expect(result).toEqual(expected);
        }));
    });
});
//# sourceMappingURL=Settlements.js.map