"use strict";
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 types_1 = require("../../../polkadot/types");
const sinon_1 = __importDefault(require("sinon"));
const toggleFreezeSecondaryAccounts_1 = require("../toggleFreezeSecondaryAccounts");
const mocks_1 = require("../../../testUtils/mocks");
describe('toggleFreezeSecondaryAccounts procedure', () => {
    let mockContext;
    beforeAll(() => {
        mocks_1.dsMockUtils.initMocks();
        mocks_1.procedureMockUtils.initMocks();
        mocks_1.entityMockUtils.initMocks();
    });
    let addTransactionStub;
    beforeEach(() => {
        addTransactionStub = mocks_1.procedureMockUtils.getAddTransactionStub();
        mockContext = mocks_1.dsMockUtils.getContextInstance({
            areSecondaryAccountsFrozen: true,
        });
    });
    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 freeze is set to true and the secondary Accounts are already frozen', () => {
        const proc = mocks_1.procedureMockUtils.getInstance(mockContext);
        return expect(toggleFreezeSecondaryAccounts_1.prepareToggleFreezeSecondaryAccounts.call(proc, {
            freeze: true,
            identity: mocks_1.entityMockUtils.getIdentityInstance({
                areSecondaryAccountsFrozen: true,
            }),
        })).rejects.toThrow('The secondary Accounts are already frozen');
    });
    test('should throw an error if freeze is set to false and the secondary Accounts are already unfrozen', () => {
        mocks_1.dsMockUtils.configureMocks({
            contextOptions: {
                areSecondaryAccountsFrozen: false,
            },
        });
        const proc = mocks_1.procedureMockUtils.getInstance(mockContext);
        return expect(toggleFreezeSecondaryAccounts_1.prepareToggleFreezeSecondaryAccounts.call(proc, {
            freeze: false,
        })).rejects.toThrow('The secondary Accounts are already unfrozen');
    });
    test('should add a freeze secondary Accounts transaction to the queue', () => __awaiter(void 0, void 0, void 0, function* () {
        mocks_1.dsMockUtils.configureMocks({
            contextOptions: {
                areSecondaryAccountsFrozen: false,
            },
        });
        const proc = mocks_1.procedureMockUtils.getInstance(mockContext);
        const transaction = mocks_1.dsMockUtils.createTxStub('identity', 'freezeSecondaryKeys');
        yield toggleFreezeSecondaryAccounts_1.prepareToggleFreezeSecondaryAccounts.call(proc, {
            freeze: true,
        });
        sinon_1.default.assert.calledWith(addTransactionStub, { transaction });
    }));
    test('should add a unfreeze secondary Accounts transaction to the queue', () => __awaiter(void 0, void 0, void 0, function* () {
        mocks_1.dsMockUtils.configureMocks({
            contextOptions: {
                areSecondaryAccountsFrozen: true,
            },
        });
        const proc = mocks_1.procedureMockUtils.getInstance(mockContext);
        const transaction = mocks_1.dsMockUtils.createTxStub('identity', 'unfreezeSecondaryKeys');
        yield toggleFreezeSecondaryAccounts_1.prepareToggleFreezeSecondaryAccounts.call(proc, {
            freeze: false,
        });
        sinon_1.default.assert.calledWith(addTransactionStub, { transaction });
    }));
    describe('getAuthorization', () => {
        test('should return the appropriate roles and permissions', () => {
            const proc = mocks_1.procedureMockUtils.getInstance(mockContext);
            const boundFunc = toggleFreezeSecondaryAccounts_1.getAuthorization.bind(proc);
            expect(boundFunc({ freeze: true })).toEqual({
                permissions: {
                    transactions: [types_1.TxTags.identity.FreezeSecondaryKeys],
                    assets: [],
                    portfolios: [],
                },
            });
            expect(boundFunc({ freeze: false })).toEqual({
                permissions: {
                    transactions: [types_1.TxTags.identity.UnfreezeSecondaryKeys],
                    assets: [],
                    portfolios: [],
                },
            });
        });
    });
});
//# sourceMappingURL=toggleFreezeSecondayAccounts.js.map