"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 removeSecondaryAccounts_1 = require("../removeSecondaryAccounts");
const mocks_1 = require("../../../testUtils/mocks");
const types_1 = require("../../../types");
const utilsConversionModule = __importStar(require("../../../utils/conversion"));
describe('removeSecondaryAccounts procedure', () => {
    let mockContext;
    let addTransactionStub;
    let signerValueToSignatoryStub;
    let signerToSignerValueStub;
    let args;
    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');
    });
    beforeEach(() => {
        addTransactionStub = mocks_1.procedureMockUtils.getAddTransactionStub();
        mockContext = mocks_1.dsMockUtils.getContextInstance();
        const accounts = [mocks_1.entityMockUtils.getAccountInstance({ address: 'someFakeAccount' })];
        args = {
            accounts,
        };
    });
    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 add a remove secondary items transaction to the queue', () => __awaiter(void 0, void 0, void 0, function* () {
        const { accounts } = args;
        const signerValue = { type: types_1.SignerType.Account, value: accounts[0].address };
        const rawSignatory = mocks_1.dsMockUtils.createMockSignatory({
            Account: mocks_1.dsMockUtils.createMockAccountId(accounts[0].address),
        });
        mocks_1.dsMockUtils.configureMocks({
            contextOptions: {
                secondaryAccounts: accounts.map(secondaryAccount => ({
                    account: secondaryAccount,
                    permissions: {
                        assets: null,
                        transactions: null,
                        transactionGroups: [],
                        portfolios: null,
                    },
                })),
            },
        });
        signerToSignerValueStub.withArgs(accounts[0]).returns(signerValue);
        signerValueToSignatoryStub.withArgs(signerValue, mockContext).returns(rawSignatory);
        const proc = mocks_1.procedureMockUtils.getInstance(mockContext);
        const transaction = mocks_1.dsMockUtils.createTxStub('identity', 'removeSecondaryKeys');
        yield removeSecondaryAccounts_1.prepareRemoveSecondaryAccounts.call(proc, args);
        sinon_1.default.assert.calledWith(addTransactionStub, {
            transaction,
            feeMultiplier: 1,
            args: [[rawSignatory]],
        });
    }));
    test('should throw an error if attempting to remove the primary Account', () => {
        const proc = mocks_1.procedureMockUtils.getInstance(mockContext);
        const account = mocks_1.entityMockUtils.getAccountInstance({ address: 'primaryAccount' });
        return expect(removeSecondaryAccounts_1.prepareRemoveSecondaryAccounts.call(proc, Object.assign(Object.assign({}, args), { accounts: [account] }))).rejects.toThrow('You cannot remove the primary Account');
    });
    test('should throw an error if at least one of the secondary Accounts to remove is not present in the secondary Accounts list', () => {
        const { accounts } = args;
        const signerValue = { type: types_1.SignerType.Account, value: accounts[0].address };
        signerToSignerValueStub.withArgs(accounts[0]).returns(signerValue);
        const proc = mocks_1.procedureMockUtils.getInstance(mockContext);
        return expect(removeSecondaryAccounts_1.prepareRemoveSecondaryAccounts.call(proc, Object.assign({}, args))).rejects.toThrow('One of the Accounts is not a secondary Account for the Identity');
    });
});
//# sourceMappingURL=removeSecondaryAccounts.js.map