"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 modifyComplianceRequirement_1 = require("../modifyComplianceRequirement");
const mocks_1 = require("../../../testUtils/mocks");
const types_2 = require("../../../types");
const utilsConversionModule = __importStar(require("../../../utils/conversion"));
jest.mock('~/api/entities/Asset', require("../../../testUtils/mocks/entities").mockAssetModule('~/api/entities/Asset'));
describe('modifyComplianceRequirement procedure', () => {
    let mockContext;
    let stringToTickerStub;
    let requirementToComplianceRequirementStub;
    let ticker;
    let conditions;
    let rawTicker;
    let args;
    beforeAll(() => {
        mocks_1.dsMockUtils.initMocks();
        mocks_1.procedureMockUtils.initMocks();
        mocks_1.entityMockUtils.initMocks();
        stringToTickerStub = sinon_1.default.stub(utilsConversionModule, 'stringToTicker');
        requirementToComplianceRequirementStub = sinon_1.default.stub(utilsConversionModule, 'requirementToComplianceRequirement');
        ticker = 'someTicker';
        conditions = [
            {
                type: types_2.ConditionType.IsIdentity,
                identity: mocks_1.entityMockUtils.getIdentityInstance(),
                target: types_2.ConditionTarget.Both,
            },
            {
                type: types_2.ConditionType.IsExternalAgent,
                target: types_2.ConditionTarget.Both,
            },
        ];
    });
    let addTransactionStub;
    let modifyComplianceRequirementTransaction;
    beforeEach(() => {
        mocks_1.dsMockUtils.setConstMock('complianceManager', 'maxConditionComplexity', {
            returnValue: mocks_1.dsMockUtils.createMockU32(50),
        });
        addTransactionStub = mocks_1.procedureMockUtils.getAddTransactionStub();
        modifyComplianceRequirementTransaction = mocks_1.dsMockUtils.createTxStub('complianceManager', 'changeComplianceRequirement');
        mockContext = mocks_1.dsMockUtils.getContextInstance();
        stringToTickerStub.withArgs(ticker, mockContext).returns(rawTicker);
        mocks_1.entityMockUtils.configureMocks({
            assetOptions: {
                complianceRequirementsGet: {
                    requirements: [
                        {
                            conditions,
                            id: 1,
                        },
                    ],
                    defaultTrustedClaimIssuers: [],
                },
            },
        });
    });
    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 supplied requirement id does not belong to the Asset', () => {
        const fakeConditions = ['condition'];
        args = {
            ticker,
            id: 2,
            conditions: fakeConditions,
        };
        const proc = mocks_1.procedureMockUtils.getInstance(mockContext);
        return expect(modifyComplianceRequirement_1.prepareModifyComplianceRequirement.call(proc, args)).rejects.toThrow('The Compliance Requirement does not exist');
    });
    test('should throw an error if the supplied requirement conditions have no change', () => {
        args = {
            ticker,
            id: 1,
            conditions,
        };
        const proc = mocks_1.procedureMockUtils.getInstance(mockContext);
        return expect(modifyComplianceRequirement_1.prepareModifyComplianceRequirement.call(proc, args)).rejects.toThrow('The supplied condition list is equal to the current one');
    });
    test('should add a modify compliance requirement transaction to the queue', () => __awaiter(void 0, void 0, void 0, function* () {
        const fakeConditions = [{ claim: '' }];
        const fakeSenderConditions = 'senderConditions';
        const fakeReceiverConditions = 'receiverConditions';
        const rawComplianceRequirement = mocks_1.dsMockUtils.createMockComplianceRequirement({
            /* eslint-disable @typescript-eslint/naming-convention */
            sender_conditions: fakeSenderConditions,
            receiver_conditions: fakeReceiverConditions,
            id: mocks_1.dsMockUtils.createMockU32(1),
            /* eslint-enable @typescript-eslint/naming-convention */
        });
        requirementToComplianceRequirementStub
            .withArgs({ conditions: fakeConditions, id: 1 }, mockContext)
            .returns(rawComplianceRequirement);
        args = {
            ticker,
            id: 1,
            conditions: fakeConditions,
        };
        const proc = mocks_1.procedureMockUtils.getInstance(mockContext);
        yield modifyComplianceRequirement_1.prepareModifyComplianceRequirement.call(proc, args);
        sinon_1.default.assert.calledWith(addTransactionStub, {
            transaction: modifyComplianceRequirementTransaction,
            args: [rawTicker, rawComplianceRequirement],
        });
    }));
    describe('getAuthorization', () => {
        test('should return the appropriate roles and permissions', () => {
            const proc = mocks_1.procedureMockUtils.getInstance(mockContext);
            const boundFunc = modifyComplianceRequirement_1.getAuthorization.bind(proc);
            const params = {
                ticker,
            };
            expect(boundFunc(params)).toEqual({
                permissions: {
                    transactions: [types_1.TxTags.complianceManager.ChangeComplianceRequirement],
                    assets: [mocks_1.entityMockUtils.getAssetInstance({ ticker })],
                    portfolios: [],
                },
            });
        });
    });
});
//# sourceMappingURL=modifyComplianceRequirement.js.map