"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 types_1 = require("../../../polkadot/types");
const sinon_1 = __importDefault(require("sinon"));
const controllerTransfer_1 = require("../controllerTransfer");
const mocks_1 = require("../../../testUtils/mocks");
const types_2 = require("../../../types");
const utilsConversionModule = __importStar(require("../../../utils/conversion"));
jest.mock('~/api/entities/SecurityToken', require("../../../testUtils/mocks/entities").mockSecurityTokenModule('~/api/entities/SecurityToken'));
jest.mock('~/api/entities/DefaultPortfolio', require("../../../testUtils/mocks/entities").mockDefaultPortfolioModule('~/api/entities/DefaultPortfolio'));
describe('controllerTransfer procedure', () => {
    let mockContext;
    let portfolioIdToPortfolioStub;
    let numberToBalanceStub;
    let portfolioIdToMeshPortfolioIdStub;
    let stringToTickerStub;
    let ticker;
    let rawTicker;
    let did;
    let rawPortfolioId;
    let originPortfolio;
    let rawAmount;
    let amount;
    beforeAll(() => {
        mocks_1.dsMockUtils.initMocks();
        mocks_1.procedureMockUtils.initMocks();
        mocks_1.entityMockUtils.initMocks();
        portfolioIdToPortfolioStub = sinon_1.default.stub(utilsConversionModule, 'portfolioIdToPortfolio');
        numberToBalanceStub = sinon_1.default.stub(utilsConversionModule, 'numberToBalance');
        portfolioIdToMeshPortfolioIdStub = sinon_1.default.stub(utilsConversionModule, 'portfolioIdToMeshPortfolioId');
        stringToTickerStub = sinon_1.default.stub(utilsConversionModule, 'stringToTicker');
        ticker = 'SOME_TICKER';
        rawTicker = mocks_1.dsMockUtils.createMockTicker(ticker);
        did = 'someDid';
        rawPortfolioId = mocks_1.dsMockUtils.createMockPortfolioId({
            did: mocks_1.dsMockUtils.createMockIdentityId(did),
            kind: mocks_1.dsMockUtils.createMockPortfolioKind('Default'),
        });
        originPortfolio = mocks_1.entityMockUtils.getDefaultPortfolioInstance({
            did,
            tokenBalances: [{ free: new bignumber_js_1.default(90) }],
        });
        amount = new bignumber_js_1.default(50);
        rawAmount = mocks_1.dsMockUtils.createMockBalance(amount.toNumber());
    });
    let addTransactionStub;
    beforeEach(() => {
        addTransactionStub = mocks_1.procedureMockUtils.getAddTransactionStub();
        mockContext = mocks_1.dsMockUtils.getContextInstance();
        stringToTickerStub.returns(rawTicker);
        portfolioIdToPortfolioStub.returns(originPortfolio);
        numberToBalanceStub.returns(rawAmount);
        portfolioIdToMeshPortfolioIdStub.returns(rawPortfolioId);
    });
    afterEach(() => {
        mocks_1.entityMockUtils.reset();
        mocks_1.procedureMockUtils.reset();
        mocks_1.dsMockUtils.reset();
    });
    afterAll(() => {
        mocks_1.entityMockUtils.cleanup();
        mocks_1.procedureMockUtils.cleanup();
        mocks_1.dsMockUtils.cleanup();
    });
    test('should throw an error if the Portfolio does not have enough balance to transfer', () => {
        const proc = mocks_1.procedureMockUtils.getInstance(mockContext);
        return expect(controllerTransfer_1.prepareControllerTransfer.call(proc, {
            ticker,
            originPortfolio,
            amount: new bignumber_js_1.default(1000),
        })).rejects.toThrow('The origin Portfolio does not have enough free balance for this transfer');
    });
    test('should add a controller transfer transaction to the queue', () => __awaiter(void 0, void 0, void 0, function* () {
        const proc = mocks_1.procedureMockUtils.getInstance(mockContext);
        const transaction = mocks_1.dsMockUtils.createTxStub('asset', 'controllerTransfer');
        yield controllerTransfer_1.prepareControllerTransfer.call(proc, {
            ticker,
            originPortfolio,
            amount,
        });
        sinon_1.default.assert.calledWith(addTransactionStub, transaction, {}, rawTicker, rawAmount, rawPortfolioId);
    }));
    describe('getAuthorization', () => {
        test('should return the appropriate roles and permissions', () => __awaiter(void 0, void 0, void 0, function* () {
            const portfolioId = {
                did: 'oneDid',
            };
            mocks_1.dsMockUtils.getContextInstance({ did: portfolioId.did });
            const proc = mocks_1.procedureMockUtils.getInstance(mockContext);
            const boundFunc = controllerTransfer_1.getAuthorization.bind(proc);
            const token = mocks_1.entityMockUtils.getSecurityTokenInstance({ ticker });
            const roles = [
                {
                    type: types_2.RoleType.PortfolioCustodian,
                    portfolioId,
                },
            ];
            expect(yield boundFunc({ ticker, originPortfolio, amount })).toEqual({
                roles,
                permissions: {
                    transactions: [types_1.TxTags.asset.ControllerTransfer],
                    tokens: [token],
                    portfolios: [mocks_1.entityMockUtils.getDefaultPortfolioInstance()],
                },
            });
        }));
    });
});
//# sourceMappingURL=controllerTransfer.js.map