"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 types_1 = require("../../../polkadot/types");
const sinon_1 = __importDefault(require("sinon"));
const modifyPrimaryIssuanceAgent_1 = require("../modifyPrimaryIssuanceAgent");
const internal_1 = require("../../../internal");
const mocks_1 = require("../../../testUtils/mocks");
const utilsConversionModule = __importStar(require("../../../utils/conversion"));
jest.mock('~/api/entities/SecurityToken', require("../../../testUtils/mocks/entities").mockSecurityTokenModule('~/api/entities/SecurityToken'));
describe('modifyPrimaryIssuanceAgent procedure', () => {
    let mockContext;
    let authorizationToAuthorizationDataStub;
    let dateToMomentStub;
    let signerToStringStub;
    let signerValueToSignatoryStub;
    let ticker;
    let rawTicker;
    let target;
    let addTransactionStub;
    let rawSignatory;
    let rawAuthorizationData;
    beforeAll(() => {
        mocks_1.dsMockUtils.initMocks();
        mocks_1.procedureMockUtils.initMocks();
        mocks_1.entityMockUtils.initMocks();
        authorizationToAuthorizationDataStub = sinon_1.default.stub(utilsConversionModule, 'authorizationToAuthorizationData');
        dateToMomentStub = sinon_1.default.stub(utilsConversionModule, 'dateToMoment');
        signerToStringStub = sinon_1.default.stub(utilsConversionModule, 'signerToString');
        signerValueToSignatoryStub = sinon_1.default.stub(utilsConversionModule, 'signerValueToSignatory');
        ticker = 'someTicker';
        rawTicker = mocks_1.dsMockUtils.createMockTicker(ticker);
        target = 'someDid';
        rawSignatory = mocks_1.dsMockUtils.createMockSignatory({
            Identity: mocks_1.dsMockUtils.createMockIdentityId(target),
        });
        rawAuthorizationData = mocks_1.dsMockUtils.createMockAuthorizationData({
            TransferAssetOwnership: rawTicker,
        });
    });
    beforeEach(() => {
        mockContext = mocks_1.dsMockUtils.getContextInstance();
        authorizationToAuthorizationDataStub.returns(rawAuthorizationData);
        signerToStringStub.returns(target);
        signerValueToSignatoryStub.returns(rawSignatory);
        addTransactionStub = mocks_1.procedureMockUtils.getAddTransactionStub();
    });
    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 throw an error if the primary issuance agents list is not empty', () => {
        const args = {
            target,
            ticker,
            requestExpiry: new Date(),
        };
        mocks_1.entityMockUtils.configureMocks({
            securityTokenOptions: {
                details: {
                    primaryIssuanceAgents: [new internal_1.Identity({ did: 'otherDid' }, mockContext)],
                },
            },
        });
        const proc = mocks_1.procedureMockUtils.getInstance(mockContext);
        return expect(modifyPrimaryIssuanceAgent_1.prepareModifyPrimaryIssuanceAgent.call(proc, args)).rejects.toThrow('The Primary Issuance Agents must be undefined to perform this procedure');
    });
    test("should throw an error if the supplied target doesn't exist", () => {
        const args = {
            target,
            ticker,
        };
        mocks_1.dsMockUtils.configureMocks({ contextOptions: { invalidDids: [target] } });
        const proc = mocks_1.procedureMockUtils.getInstance(mockContext);
        return expect(modifyPrimaryIssuanceAgent_1.prepareModifyPrimaryIssuanceAgent.call(proc, args)).rejects.toThrow('The supplied Identity does not exist');
    });
    test('should throw an error if the supplied expiry date is not a future date', () => {
        const args = {
            target,
            ticker,
            requestExpiry: new Date(),
        };
        mocks_1.entityMockUtils.configureMocks({
            securityTokenOptions: {
                details: {
                    primaryIssuanceAgents: [],
                },
            },
        });
        const proc = mocks_1.procedureMockUtils.getInstance(mockContext);
        return expect(modifyPrimaryIssuanceAgent_1.prepareModifyPrimaryIssuanceAgent.call(proc, args)).rejects.toThrow('The request expiry must be a future date');
    });
    test('should add a add authorization transaction to the queue', () => __awaiter(void 0, void 0, void 0, function* () {
        const args = {
            target,
            ticker,
        };
        const requestExpiry = new Date('12/12/2050');
        const rawExpiry = mocks_1.dsMockUtils.createMockMoment(requestExpiry.getTime());
        dateToMomentStub.withArgs(requestExpiry, mockContext).returns(rawExpiry);
        mocks_1.entityMockUtils.configureMocks({
            securityTokenOptions: {
                details: {
                    primaryIssuanceAgents: [],
                },
            },
        });
        const transaction = mocks_1.dsMockUtils.createTxStub('identity', 'addAuthorization');
        const proc = mocks_1.procedureMockUtils.getInstance(mockContext);
        yield modifyPrimaryIssuanceAgent_1.prepareModifyPrimaryIssuanceAgent.call(proc, args);
        sinon_1.default.assert.calledWith(addTransactionStub, transaction, {}, rawSignatory, rawAuthorizationData, null);
        mocks_1.entityMockUtils.configureMocks({
            securityTokenOptions: {
                details: {
                    primaryIssuanceAgents: [],
                },
            },
        });
        yield modifyPrimaryIssuanceAgent_1.prepareModifyPrimaryIssuanceAgent.call(proc, args);
        sinon_1.default.assert.calledWith(addTransactionStub, transaction, {}, rawSignatory, rawAuthorizationData, null);
        yield modifyPrimaryIssuanceAgent_1.prepareModifyPrimaryIssuanceAgent.call(proc, Object.assign(Object.assign({}, args), { requestExpiry }));
        sinon_1.default.assert.calledWith(addTransactionStub, transaction, {}, rawSignatory, rawAuthorizationData, rawExpiry);
    }));
    describe('getAuthorization', () => {
        test('should return the appropriate roles and permissions', () => {
            const proc = mocks_1.procedureMockUtils.getInstance(mockContext);
            const boundFunc = modifyPrimaryIssuanceAgent_1.getAuthorization.bind(proc);
            const args = {
                ticker,
            };
            expect(boundFunc(args)).toEqual({
                permissions: {
                    portfolios: [],
                    transactions: [types_1.TxTags.identity.AddAuthorization],
                    tokens: [mocks_1.entityMockUtils.getSecurityTokenInstance({ ticker })],
                },
            });
        });
    });
});
//# sourceMappingURL=modifyPrimaryIssuanceAgent.js.map