"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 bignumber_js_1 = __importDefault(require("bignumber.js"));
const sinon_1 = __importDefault(require("sinon"));
const internal_1 = require("../../../../../internal");
const mocks_1 = require("../../../../../testUtils/mocks");
const types_1 = require("../../../../../types");
const Count_1 = require("../Count");
const Percentage_1 = require("../Percentage");
const TransferRestrictionBase_1 = require("../TransferRestrictionBase");
jest.mock('~/api/entities/Identity', require("../../../../../testUtils/mocks/entities").mockIdentityModule('~/api/entities/Identity'));
jest.mock('~/base/Procedure', require("../../../../../testUtils/mocks/procedure").mockProcedureModule('~/base/Procedure'));
describe('TransferRestrictionBase class', () => {
    beforeAll(() => {
        mocks_1.entityMockUtils.initMocks();
        mocks_1.dsMockUtils.initMocks();
        mocks_1.procedureMockUtils.initMocks();
    });
    afterEach(() => {
        mocks_1.dsMockUtils.reset();
        mocks_1.entityMockUtils.reset();
        mocks_1.procedureMockUtils.initMocks();
    });
    afterAll(() => {
        mocks_1.dsMockUtils.cleanup();
        mocks_1.entityMockUtils.cleanup();
        mocks_1.procedureMockUtils.cleanup();
    });
    test('should extend namespace', () => {
        expect(TransferRestrictionBase_1.TransferRestrictionBase.prototype instanceof internal_1.Namespace).toBe(true);
    });
    describe('method: addRestriction', () => {
        let context;
        let token;
        beforeEach(() => {
            context = mocks_1.dsMockUtils.getContextInstance();
            token = mocks_1.entityMockUtils.getSecurityTokenInstance();
        });
        afterEach(() => {
            sinon_1.default.restore();
        });
        test('should prepare the procedure (count) with the correct arguments and context, and return the resulting transaction queue', () => __awaiter(void 0, void 0, void 0, function* () {
            const count = new Count_1.Count(token, context);
            const args = {
                count: new bignumber_js_1.default(3),
                exemptedScopeIds: ['someScopeId'],
            };
            const expectedQueue = 'someQueue';
            mocks_1.procedureMockUtils
                .getPrepareStub()
                .withArgs({
                args: Object.assign(Object.assign({ ticker: token.ticker }, args), { type: types_1.TransferRestrictionType.Count }),
                transformer: undefined,
            }, context)
                .resolves(expectedQueue);
            const queue = yield count.addRestriction(Object.assign({}, args));
            expect(queue).toBe(expectedQueue);
        }));
        test('should prepare the procedure (percentage) with the correct arguments and context, and return the resulting transaction queue', () => __awaiter(void 0, void 0, void 0, function* () {
            const percentage = new Percentage_1.Percentage(token, context);
            const args = {
                percentage: new bignumber_js_1.default(3),
                exemptedScopeIds: ['someScopeId'],
            };
            const expectedQueue = 'someQueue';
            mocks_1.procedureMockUtils
                .getPrepareStub()
                .withArgs({
                args: Object.assign(Object.assign({ ticker: token.ticker }, args), { type: types_1.TransferRestrictionType.Percentage }),
                transformer: undefined,
            }, context)
                .resolves(expectedQueue);
            const queue = yield percentage.addRestriction(Object.assign({}, args));
            expect(queue).toBe(expectedQueue);
        }));
    });
    describe('method: setRestrictions', () => {
        let context;
        let token;
        beforeEach(() => {
            context = mocks_1.dsMockUtils.getContextInstance();
            token = mocks_1.entityMockUtils.getSecurityTokenInstance();
        });
        afterEach(() => {
            sinon_1.default.restore();
        });
        test('should prepare the procedure (count) with the correct arguments and context, and return the resulting transaction queue', () => __awaiter(void 0, void 0, void 0, function* () {
            const count = new Count_1.Count(token, context);
            const args = {
                restrictions: [{ count: new bignumber_js_1.default(3), exemptedScopeIds: ['someScopeId'] }],
            };
            const expectedQueue = 'someQueue';
            mocks_1.procedureMockUtils
                .getPrepareStub()
                .withArgs({
                args: Object.assign(Object.assign({ ticker: token.ticker }, args), { type: types_1.TransferRestrictionType.Count }),
                transformer: undefined,
            }, context)
                .resolves(expectedQueue);
            const queue = yield count.setRestrictions(Object.assign({}, args));
            expect(queue).toBe(expectedQueue);
        }));
        test('should prepare the procedure (percentage) with the correct arguments and context, and return the resulting transaction queue', () => __awaiter(void 0, void 0, void 0, function* () {
            const percentage = new Percentage_1.Percentage(token, context);
            const args = {
                restrictions: [{ percentage: new bignumber_js_1.default(49), exemptedScopeIds: ['someScopeId'] }],
            };
            const expectedQueue = 'someQueue';
            mocks_1.procedureMockUtils
                .getPrepareStub()
                .withArgs({
                args: Object.assign(Object.assign({ ticker: token.ticker }, args), { type: types_1.TransferRestrictionType.Percentage }),
                transformer: undefined,
            }, context)
                .resolves(expectedQueue);
            const queue = yield percentage.setRestrictions(Object.assign({}, args));
            expect(queue).toBe(expectedQueue);
        }));
    });
    describe('method: removeRestrictions', () => {
        let context;
        let token;
        beforeEach(() => {
            context = mocks_1.dsMockUtils.getContextInstance();
            token = mocks_1.entityMockUtils.getSecurityTokenInstance();
        });
        afterEach(() => {
            sinon_1.default.restore();
        });
        test('should prepare the procedure (count) with the correct arguments and context, and return the resulting transaction queue', () => __awaiter(void 0, void 0, void 0, function* () {
            const count = new Count_1.Count(token, context);
            const expectedQueue = 'someQueue';
            mocks_1.procedureMockUtils
                .getPrepareStub()
                .withArgs({
                args: { ticker: token.ticker, restrictions: [], type: types_1.TransferRestrictionType.Count },
                transformer: undefined,
            }, context)
                .resolves(expectedQueue);
            const queue = yield count.removeRestrictions();
            expect(queue).toBe(expectedQueue);
        }));
        test('should prepare the procedure (percentage) with the correct arguments and context, and return the resulting transaction queue', () => __awaiter(void 0, void 0, void 0, function* () {
            const percentage = new Percentage_1.Percentage(token, context);
            const expectedQueue = 'someQueue';
            mocks_1.procedureMockUtils
                .getPrepareStub()
                .withArgs({
                args: {
                    ticker: token.ticker,
                    restrictions: [],
                    type: types_1.TransferRestrictionType.Percentage,
                },
                transformer: undefined,
            }, context)
                .resolves(expectedQueue);
            const queue = yield percentage.removeRestrictions();
            expect(queue).toBe(expectedQueue);
        }));
    });
    describe('method: get', () => {
        let context;
        let token;
        let scopeId;
        let countRestriction;
        let percentageRestriction;
        let rawCountRestriction;
        let rawPercentageRestriction;
        beforeAll(() => {
            scopeId = 'someScopeId';
            countRestriction = {
                exemptedScopeIds: [scopeId],
                count: new bignumber_js_1.default(10),
            };
            percentageRestriction = {
                exemptedScopeIds: [scopeId],
                percentage: new bignumber_js_1.default(49),
            };
            rawCountRestriction = mocks_1.dsMockUtils.createMockTransferManager({
                CountTransferManager: mocks_1.dsMockUtils.createMockU64(countRestriction.count.toNumber()),
            });
            rawPercentageRestriction = mocks_1.dsMockUtils.createMockTransferManager({
                PercentageTransferManager: mocks_1.dsMockUtils.createMockPermill(percentageRestriction.percentage.toNumber() * 10000),
            });
        });
        beforeEach(() => {
            context = mocks_1.dsMockUtils.getContextInstance();
            token = mocks_1.entityMockUtils.getSecurityTokenInstance();
            mocks_1.dsMockUtils.setConstMock('statistics', 'maxTransferManagersPerAsset', {
                returnValue: mocks_1.dsMockUtils.createMockU32(3),
            });
            mocks_1.dsMockUtils.createQueryStub('statistics', 'activeTransferManagers', {
                returnValue: [rawCountRestriction, rawPercentageRestriction],
            });
            mocks_1.dsMockUtils.createQueryStub('statistics', 'exemptEntities', {
                entries: [[[null, mocks_1.dsMockUtils.createMockScopeId(scopeId)], true]],
            });
        });
        afterEach(() => {
            sinon_1.default.restore();
        });
        test('should return all count transfer restrictions', () => __awaiter(void 0, void 0, void 0, function* () {
            const count = new Count_1.Count(token, context);
            const result = yield count.get();
            expect(result).toEqual({
                restrictions: [countRestriction],
                availableSlots: 1,
            });
        }));
        test('should return all percentage transfer restrictions', () => __awaiter(void 0, void 0, void 0, function* () {
            const percentage = new Percentage_1.Percentage(token, context);
            let result = yield percentage.get();
            expect(result).toEqual({
                restrictions: [percentageRestriction],
                availableSlots: 1,
            });
            mocks_1.dsMockUtils.createQueryStub('statistics', 'exemptEntities', {
                entries: [],
            });
            result = yield percentage.get();
            expect(result).toEqual({
                restrictions: [
                    {
                        percentage: new bignumber_js_1.default(49),
                    },
                ],
                availableSlots: 1,
            });
        }));
    });
});
//# sourceMappingURL=TransferRestrictionBase.js.map