"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 sinon_1 = __importDefault(require("sinon"));
const internal_1 = require("../../../internal");
const mocks_1 = require("../../../testUtils/mocks");
const utilsConversionModule = __importStar(require("../../../utils/conversion"));
jest.mock('~/base/Procedure', require("../../../testUtils/mocks/procedure").mockProcedureModule('~/base/Procedure'));
describe('CustomPermissionGroup class', () => {
    const ticker = 'TOKENNAME';
    const id = new bignumber_js_1.default(1);
    let context;
    beforeAll(() => {
        mocks_1.dsMockUtils.initMocks();
        mocks_1.entityMockUtils.initMocks();
        mocks_1.procedureMockUtils.initMocks();
        sinon_1.default.stub(utilsConversionModule, 'stringToTicker');
    });
    beforeEach(() => {
        context = mocks_1.dsMockUtils.getContextInstance();
    });
    afterEach(() => {
        mocks_1.dsMockUtils.reset();
        mocks_1.entityMockUtils.reset();
        mocks_1.procedureMockUtils.reset();
    });
    afterAll(() => {
        mocks_1.dsMockUtils.cleanup();
        mocks_1.entityMockUtils.cleanup();
        mocks_1.procedureMockUtils.cleanup();
    });
    test('should extend PermissionGroup', () => {
        expect(internal_1.CustomPermissionGroup.prototype instanceof internal_1.PermissionGroup).toBe(true);
    });
    describe('constructor', () => {
        test('should assign id to instance', () => {
            const customPermissionGroup = new internal_1.CustomPermissionGroup({ id, ticker }, context);
            expect(customPermissionGroup.id).toBe(id);
            expect(customPermissionGroup.ticker).toBe(ticker);
        });
    });
    describe('method: isUniqueIdentifiers', () => {
        test('should return true if the object conforms to the interface', () => {
            expect(internal_1.CustomPermissionGroup.isUniqueIdentifiers({ id: new bignumber_js_1.default(1), ticker })).toBe(true);
            expect(internal_1.CustomPermissionGroup.isUniqueIdentifiers({})).toBe(false);
            expect(internal_1.CustomPermissionGroup.isUniqueIdentifiers({ id: 1 })).toBe(false);
        });
    });
    describe('method: toJson', () => {
        test('should return a human readable version of the entity', () => {
            const customPermissionGroup = new internal_1.CustomPermissionGroup({ id, ticker }, context);
            expect(customPermissionGroup.toJson()).toEqual({
                id,
                ticker,
            });
        });
    });
    describe('method: setPermissions', () => {
        test('should prepare the procedure with the correct arguments and context, and return the resulting transaction queue', () => __awaiter(void 0, void 0, void 0, function* () {
            const customPermissionGroup = new internal_1.CustomPermissionGroup({ id, ticker }, context);
            const args = {
                permissions: {
                    transactionGroups: [],
                },
            };
            const expectedQueue = 'someQueue';
            mocks_1.procedureMockUtils
                .getPrepareStub()
                .withArgs({ args: Object.assign(Object.assign({}, args), { group: customPermissionGroup }), transformer: undefined }, context)
                .resolves(expectedQueue);
            const queue = yield customPermissionGroup.setPermissions(args);
            expect(queue).toBe(expectedQueue);
        }));
    });
    describe('method: getPermissions', () => {
        test('should return a list of permissions and transaction groups', () => __awaiter(void 0, void 0, void 0, function* () {
            const customPermissionGroup = new internal_1.CustomPermissionGroup({ id, ticker }, context);
            sinon_1.default.stub(utilsConversionModule, 'numberToU32');
            mocks_1.dsMockUtils.createQueryStub('externalAgents', 'groupPermissions', {
                returnValue: mocks_1.dsMockUtils.createMockOption(mocks_1.dsMockUtils.createMockExtrinsicPermissions({
                    These: [
                        /* eslint-disable @typescript-eslint/naming-convention */
                        mocks_1.dsMockUtils.createMockPalletPermissions({
                            pallet_name: mocks_1.dsMockUtils.createMockPalletName('Sto'),
                            dispatchable_names: mocks_1.dsMockUtils.createMockDispatchableNames({
                                These: [mocks_1.dsMockUtils.createMockDispatchableName('invest')],
                            }),
                        }),
                        mocks_1.dsMockUtils.createMockPalletPermissions({
                            pallet_name: mocks_1.dsMockUtils.createMockPalletName('Identity'),
                            dispatchable_names: mocks_1.dsMockUtils.createMockDispatchableNames({
                                These: [mocks_1.dsMockUtils.createMockDispatchableName('add_claim')],
                            }),
                        }),
                        /* eslint-enable @typescript-eslint/naming-convention */
                    ],
                })),
            });
            const result = yield customPermissionGroup.getPermissions();
            expect(result).toEqual({
                transactions: { type: 'Include', values: ['sto.invest', 'identity.addClaim'] },
                transactionGroups: [],
            });
        }));
    });
    describe('method: exists', () => {
        test('should return whether the Custom Permission Group exists', () => __awaiter(void 0, void 0, void 0, function* () {
            const customPermissionGroup = new internal_1.CustomPermissionGroup({ id, ticker }, context);
            mocks_1.dsMockUtils.createQueryStub('externalAgents', 'agIdSequence', {
                returnValue: mocks_1.dsMockUtils.createMockU32(0),
            });
            yield expect(customPermissionGroup.exists()).resolves.toBe(false);
            mocks_1.dsMockUtils.createQueryStub('externalAgents', 'agIdSequence', {
                returnValue: mocks_1.dsMockUtils.createMockU32(10),
            });
            yield expect(customPermissionGroup.exists()).resolves.toBe(true);
            customPermissionGroup.id = new bignumber_js_1.default(0);
            return expect(customPermissionGroup.exists()).resolves.toBe(false);
        }));
    });
});
//# sourceMappingURL=CustomPermissionGroup.js.map