"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 sinon_1 = __importDefault(require("sinon"));
const modifySignerPermissions_1 = require("../modifySignerPermissions");
const mocks_1 = require("../../../testUtils/mocks");
const types_1 = require("../../../types");
const utilsConversionModule = __importStar(require("../../../utils/conversion"));
describe('modifySignerPermissions procedure', () => {
    let mockContext;
    let addBatchTransactionStub;
    let signerValueToSignatoryStub;
    let signerToSignerValueStub;
    let permissionsToMeshPermissionsStub;
    let permissionsLikeToPermissionsStub;
    let identity;
    let account;
    beforeAll(() => {
        mocks_1.dsMockUtils.initMocks();
        mocks_1.procedureMockUtils.initMocks();
        mocks_1.entityMockUtils.initMocks();
        signerValueToSignatoryStub = sinon_1.default.stub(utilsConversionModule, 'signerValueToSignatory');
        signerToSignerValueStub = sinon_1.default.stub(utilsConversionModule, 'signerToSignerValue');
        permissionsToMeshPermissionsStub = sinon_1.default.stub(utilsConversionModule, 'permissionsToMeshPermissions');
        permissionsLikeToPermissionsStub = sinon_1.default.stub(utilsConversionModule, 'permissionsLikeToPermissions');
    });
    beforeEach(() => {
        addBatchTransactionStub = mocks_1.procedureMockUtils.getAddBatchTransactionStub();
        mockContext = mocks_1.dsMockUtils.getContextInstance();
        account = mocks_1.entityMockUtils.getAccountInstance({ address: 'someFakeAccount' });
        identity = mocks_1.entityMockUtils.getIdentityInstance({
            getSecondaryAccounts: [
                {
                    account,
                    permissions: {
                        assets: {
                            type: types_1.PermissionType.Include,
                            values: [],
                        },
                        portfolios: {
                            type: types_1.PermissionType.Include,
                            values: [],
                        },
                        transactions: {
                            type: types_1.PermissionType.Include,
                            values: [],
                        },
                        transactionGroups: [],
                    },
                },
            ],
        });
    });
    afterEach(() => {
        mocks_1.entityMockUtils.reset();
        mocks_1.procedureMockUtils.reset();
        mocks_1.dsMockUtils.reset();
    });
    afterAll(() => {
        mocks_1.procedureMockUtils.cleanup();
        mocks_1.dsMockUtils.cleanup();
    });
    test('should add a batch of Set Permission To Signer transactions to the queue', () => __awaiter(void 0, void 0, void 0, function* () {
        let secondaryAccounts = [
            {
                account,
                permissions: {
                    assets: null,
                    transactions: null,
                    transactionGroups: [],
                    portfolios: null,
                },
            },
        ];
        let fakeMeshPermissions = mocks_1.dsMockUtils.createMockPermissions({
            asset: mocks_1.dsMockUtils.createMockAssetPermissions(),
            extrinsic: mocks_1.dsMockUtils.createMockExtrinsicPermissions(),
            portfolio: mocks_1.dsMockUtils.createMockPortfolioPermissions(),
        });
        const signerValue = {
            type: types_1.SignerType.Account,
            value: secondaryAccounts[0].account.address,
        };
        const rawSignatory = mocks_1.dsMockUtils.createMockSignatory({
            Account: mocks_1.dsMockUtils.createMockAccountId(signerValue.value),
        });
        mocks_1.dsMockUtils.configureMocks({
            contextOptions: {
                secondaryAccounts,
            },
        });
        signerToSignerValueStub.returns(signerValue);
        signerValueToSignatoryStub.withArgs(signerValue, mockContext).returns(rawSignatory);
        const proc = mocks_1.procedureMockUtils.getInstance(mockContext);
        const transaction = mocks_1.dsMockUtils.createTxStub('identity', 'setPermissionToSigner');
        permissionsToMeshPermissionsStub.returns(fakeMeshPermissions);
        let signersList = [[rawSignatory, fakeMeshPermissions]];
        yield modifySignerPermissions_1.prepareModifySignerPermissions.call(proc, { secondaryAccounts });
        sinon_1.default.assert.calledWith(addBatchTransactionStub, {
            transactions: signersList.map(signers => ({ transaction, args: signers })),
        });
        secondaryAccounts = [
            {
                account,
                permissions: {
                    assets: null,
                    transactions: null,
                    transactionGroups: [],
                    portfolios: null,
                },
            },
        ];
        fakeMeshPermissions = mocks_1.dsMockUtils.createMockPermissions({
            asset: mocks_1.dsMockUtils.createMockAssetPermissions('Whole'),
            extrinsic: mocks_1.dsMockUtils.createMockExtrinsicPermissions('Whole'),
            portfolio: mocks_1.dsMockUtils.createMockPortfolioPermissions('Whole'),
        });
        permissionsToMeshPermissionsStub.returns(fakeMeshPermissions);
        signersList = [[rawSignatory, fakeMeshPermissions]];
        permissionsLikeToPermissionsStub.resolves(secondaryAccounts[0].permissions);
        yield modifySignerPermissions_1.prepareModifySignerPermissions.call(proc, { secondaryAccounts, identity });
        sinon_1.default.assert.calledWith(addBatchTransactionStub, {
            transactions: signersList.map(signers => ({ transaction, args: signers })),
        });
    }));
    test('should throw an error if at least one of the Accounts for which to modify permissions is not a secondary Account for the Identity', () => {
        const secondaryAccounts = [
            {
                account: mocks_1.entityMockUtils.getAccountInstance({ address: 'someFakeAccount' }),
                permissions: {
                    assets: null,
                    transactions: null,
                    portfolios: null,
                },
            },
        ];
        const signerValue = {
            type: types_1.SignerType.Account,
            value: secondaryAccounts[0].account.address,
        };
        signerToSignerValueStub.withArgs(secondaryAccounts[0].account).returns(signerValue);
        const proc = mocks_1.procedureMockUtils.getInstance(mockContext);
        return expect(modifySignerPermissions_1.prepareModifySignerPermissions.call(proc, {
            secondaryAccounts,
            identity: mocks_1.entityMockUtils.getIdentityInstance({ getSecondaryAccounts: [] }),
        })).rejects.toThrow('One of the Accounts is not a secondary Account for the Identity');
    });
});
//# sourceMappingURL=modifySignerPermissions.js.map