"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 types_1 = require("../../../polkadot/types");
const sinon_1 = __importDefault(require("sinon"));
const removeAssetRequirement_1 = require("../removeAssetRequirement");
const mocks_1 = require("../../../testUtils/mocks");
const utilsConversionModule = __importStar(require("../../../utils/conversion"));
jest.mock('~/api/entities/Asset', require("../../../testUtils/mocks/entities").mockAssetModule('~/api/entities/Asset'));
describe('removeAssetRequirement procedure', () => {
    let mockContext;
    let stringToTickerStub;
    let ticker;
    let requirement;
    let rawTicker;
    let senderConditions;
    let receiverConditions;
    let rawComplianceRequirement;
    let args;
    beforeAll(() => {
        mocks_1.dsMockUtils.initMocks();
        mocks_1.procedureMockUtils.initMocks();
        mocks_1.entityMockUtils.initMocks();
        stringToTickerStub = sinon_1.default.stub(utilsConversionModule, 'stringToTicker');
        ticker = 'someTicker';
        requirement = new bignumber_js_1.default(2);
        args = {
            ticker,
            requirement,
        };
    });
    let addTransactionStub;
    let removeComplianceRequirementTransaction;
    beforeEach(() => {
        mocks_1.dsMockUtils.setConstMock('complianceManager', 'maxConditionComplexity', {
            returnValue: mocks_1.dsMockUtils.createMockU32(new bignumber_js_1.default(50)),
        });
        addTransactionStub = mocks_1.procedureMockUtils.getAddTransactionStub();
        removeComplianceRequirementTransaction = mocks_1.dsMockUtils.createTxStub('complianceManager', 'removeComplianceRequirement');
        mockContext = mocks_1.dsMockUtils.getContextInstance();
        stringToTickerStub.withArgs(ticker, mockContext).returns(rawTicker);
        senderConditions = [
            'senderConditions0',
            'senderConditions1',
        ];
        receiverConditions = [
            'receiverConditions0',
            'receiverConditions1',
        ];
        rawComplianceRequirement = senderConditions.map((sConditions, index) => ({
            /* eslint-disable @typescript-eslint/naming-convention */
            sender_conditions: sConditions,
            receiver_conditions: receiverConditions[index],
            /* eslint-enable @typescript-eslint/naming-convention */
            id: mocks_1.dsMockUtils.createMockU32(new bignumber_js_1.default(index)),
        }));
        mocks_1.dsMockUtils.createQueryStub('complianceManager', 'assetCompliances', {
            returnValue: {
                requirements: rawComplianceRequirement,
            },
        });
    });
    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 the supplied id is not present in the current requirements', () => {
        const proc = mocks_1.procedureMockUtils.getInstance(mockContext);
        const complianceRequirementId = new bignumber_js_1.default(1);
        return expect(removeAssetRequirement_1.prepareRemoveAssetRequirement.call(proc, Object.assign(Object.assign({}, args), { requirement: { id: complianceRequirementId, conditions: [] } }))).rejects.toThrow(`There is no compliance requirement with id "${complianceRequirementId}"`);
    });
    test('should add a remove compliance requirement transaction to the queue', () => __awaiter(void 0, void 0, void 0, function* () {
        const rawId = mocks_1.dsMockUtils.createMockU32(requirement);
        sinon_1.default.stub(utilsConversionModule, 'bigNumberToU32').returns(rawId);
        const proc = mocks_1.procedureMockUtils.getInstance(mockContext);
        const result = yield removeAssetRequirement_1.prepareRemoveAssetRequirement.call(proc, args);
        sinon_1.default.assert.calledWith(addTransactionStub, {
            transaction: removeComplianceRequirementTransaction,
            args: [rawTicker, rawId],
        });
        expect(result).toEqual(expect.objectContaining({ ticker }));
    }));
    describe('getAuthorization', () => {
        test('should return the appropriate roles and permissions', () => {
            const proc = mocks_1.procedureMockUtils.getInstance(mockContext);
            const boundFunc = removeAssetRequirement_1.getAuthorization.bind(proc);
            const params = {
                ticker,
            };
            expect(boundFunc(params)).toEqual({
                permissions: {
                    transactions: [types_1.TxTags.complianceManager.RemoveComplianceRequirement],
                    assets: [expect.objectContaining({ ticker })],
                    portfolios: [],
                },
            });
        });
    });
});
//# sourceMappingURL=removeAssetRequirement.js.map