"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 transferPolyx_1 = require("../transferPolyx");
const mocks_1 = require("../../../testUtils/mocks");
const types_1 = require("../../../types");
const utilsConversionModule = __importStar(require("../../../utils/conversion"));
jest.mock('~/api/entities/Identity', require("../../../testUtils/mocks/entities").mockIdentityModule('~/api/entities/Identity'));
jest.mock('~/api/entities/Account', require("../../../testUtils/mocks/entities").mockAccountModule('~/api/entities/Account'));
describe('transferPolyx procedure', () => {
    let mockContext;
    beforeAll(() => {
        mocks_1.entityMockUtils.initMocks();
        mocks_1.dsMockUtils.initMocks();
        mocks_1.procedureMockUtils.initMocks();
    });
    beforeEach(() => {
        mockContext = mocks_1.dsMockUtils.getContextInstance();
    });
    afterEach(() => {
        mocks_1.entityMockUtils.reset();
        mocks_1.procedureMockUtils.reset();
        mocks_1.dsMockUtils.reset();
    });
    afterAll(() => {
        mocks_1.procedureMockUtils.cleanup();
        mocks_1.dsMockUtils.cleanup();
    });
    test('should throw an error if the user has insufficient balance to transfer', () => {
        mocks_1.dsMockUtils.createQueryStub('identity', 'keyToIdentityIds', { returnValue: {} });
        const proc = mocks_1.procedureMockUtils.getInstance(mockContext);
        return expect(transferPolyx_1.prepareTransferPolyx.call(proc, {
            to: 'someAccount',
            amount: new bignumber_js_1.default(101),
        })).rejects.toThrow('Insufficient free balance');
    });
    test("should throw an error if destination Account doesn't have an associated Identity", () => {
        mocks_1.entityMockUtils.configureMocks({
            accountOptions: {
                getIdentity: null,
            },
        });
        const proc = mocks_1.procedureMockUtils.getInstance(mockContext);
        return expect(transferPolyx_1.prepareTransferPolyx.call(proc, { to: 'someAccount', amount: new bignumber_js_1.default(99) })).rejects.toThrow("The destination Account doesn't have an associated Identity");
    });
    test("should throw an error if sender Identity doesn't have valid CDD", () => {
        mocks_1.dsMockUtils
            .createQueryStub('identity', 'keyToIdentityIds')
            .returns(mocks_1.dsMockUtils.createMockIdentityId('currentIdentityId'));
        mockContext = mocks_1.dsMockUtils.getContextInstance({
            validCdd: false,
        });
        const proc = mocks_1.procedureMockUtils.getInstance(mockContext);
        return expect(transferPolyx_1.prepareTransferPolyx.call(proc, { to: 'someAccount', amount: new bignumber_js_1.default(99) })).rejects.toThrow('The sender Identity has an invalid CDD claim');
    });
    test("should throw an error if destination Account doesn't have valid CDD", () => {
        mocks_1.dsMockUtils
            .createQueryStub('identity', 'keyToIdentityIds')
            .returns(mocks_1.dsMockUtils.createMockIdentityId('currentIdentityId'));
        mocks_1.entityMockUtils.configureMocks({
            accountOptions: {
                getIdentity: mocks_1.entityMockUtils.getIdentityInstance({
                    hasValidCdd: false,
                }),
            },
        });
        const proc = mocks_1.procedureMockUtils.getInstance(mockContext);
        return expect(transferPolyx_1.prepareTransferPolyx.call(proc, { to: 'someAccount', amount: new bignumber_js_1.default(99) })).rejects.toThrow('The receiver Identity has an invalid CDD claim');
    });
    test('should add a balance transfer transaction to the queue', () => __awaiter(void 0, void 0, void 0, function* () {
        const to = mocks_1.entityMockUtils.getAccountInstance({ address: 'someAccount' });
        const amount = new bignumber_js_1.default(99);
        const memo = 'someMessage';
        const rawAccount = mocks_1.dsMockUtils.createMockAccountId(to.address);
        const rawAmount = mocks_1.dsMockUtils.createMockBalance(amount);
        const rawMemo = 'memo';
        mocks_1.dsMockUtils
            .createQueryStub('identity', 'keyToIdentityIds')
            .returns(mocks_1.dsMockUtils.createMockIdentityId('currentIdentityId'));
        sinon_1.default.stub(utilsConversionModule, 'stringToAccountId').returns(rawAccount);
        sinon_1.default.stub(utilsConversionModule, 'bigNumberToBalance').returns(rawAmount);
        sinon_1.default.stub(utilsConversionModule, 'stringToMemo').returns(rawMemo);
        let tx = mocks_1.dsMockUtils.createTxStub('balances', 'transfer');
        const proc = mocks_1.procedureMockUtils.getInstance(mockContext);
        yield transferPolyx_1.prepareTransferPolyx.call(proc, {
            to,
            amount,
        });
        sinon_1.default.assert.calledWith(mocks_1.procedureMockUtils.getAddTransactionStub(), {
            transaction: tx,
            args: [rawAccount, rawAmount],
        });
        tx = mocks_1.dsMockUtils.createTxStub('balances', 'transferWithMemo');
        yield transferPolyx_1.prepareTransferPolyx.call(proc, {
            to,
            amount,
            memo,
        });
        sinon_1.default.assert.calledWith(mocks_1.procedureMockUtils.getAddTransactionStub(), {
            transaction: tx,
            args: [rawAccount, rawAmount, rawMemo],
        });
    }));
    describe('getAuthorization', () => {
        test('should return the appropriate roles and permissions', () => {
            const memo = 'something';
            const args = {
                memo,
            };
            expect(transferPolyx_1.getAuthorization(args)).toEqual({
                permissions: {
                    transactions: [types_1.TxTags.balances.TransferWithMemo],
                    assets: [],
                    portfolios: [],
                },
            });
            args.memo = undefined;
            expect(transferPolyx_1.getAuthorization(args)).toEqual({
                permissions: {
                    transactions: [types_1.TxTags.balances.Transfer],
                    assets: [],
                    portfolios: [],
                },
            });
        });
    });
});
//# sourceMappingURL=transferPolyx.js.map