"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 sinon_1 = __importDefault(require("sinon"));
const internal_1 = require("../../../../../internal");
const mocks_1 = require("../../../../../testUtils/mocks");
const internal_2 = require("../../../../../types/internal");
const utilsConversionModule = __importStar(require("../../../../../utils/conversion"));
const TrustedClaimIssuers_1 = require("../TrustedClaimIssuers");
jest.mock('~/api/entities/Identity', require("../../../../../testUtils/mocks/entities").mockIdentityModule('~/api/entities/Identity'));
jest.mock('~/api/entities/SecurityToken', require("../../../../../testUtils/mocks/entities").mockSecurityTokenModule('~/api/entities/SecurityToken'));
jest.mock('~/base/Procedure', require("../../../../../testUtils/mocks/procedure").mockProcedureModule('~/base/Procedure'));
describe('TrustedClaimIssuers class', () => {
    beforeAll(() => {
        mocks_1.entityMockUtils.initMocks();
        mocks_1.dsMockUtils.initMocks();
        mocks_1.procedureMockUtils.initMocks();
    });
    afterEach(() => {
        mocks_1.dsMockUtils.reset();
        mocks_1.procedureMockUtils.reset();
    });
    afterAll(() => {
        mocks_1.dsMockUtils.cleanup();
        mocks_1.procedureMockUtils.cleanup();
    });
    test('should extend namespace', () => {
        expect(TrustedClaimIssuers_1.TrustedClaimIssuers.prototype instanceof internal_1.Namespace).toBe(true);
    });
    describe('method: set', () => {
        afterAll(() => {
            sinon_1.default.restore();
        });
        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 context = mocks_1.dsMockUtils.getContextInstance();
            const token = mocks_1.entityMockUtils.getSecurityTokenInstance();
            const trustedClaimIssuers = new TrustedClaimIssuers_1.TrustedClaimIssuers(token, context);
            const args = {
                claimIssuers: [
                    { identity: mocks_1.entityMockUtils.getIdentityInstance({ did: 'someDid' }) },
                    { identity: mocks_1.entityMockUtils.getIdentityInstance({ did: 'otherDid' }) },
                ],
            };
            const expectedQueue = 'someQueue';
            mocks_1.procedureMockUtils
                .getPrepareStub()
                .withArgs({
                args: Object.assign(Object.assign({ ticker: token.ticker }, args), { operation: internal_2.TrustedClaimIssuerOperation.Set }),
                transformer: undefined,
            }, context)
                .resolves(expectedQueue);
            const queue = yield trustedClaimIssuers.set(Object.assign({}, args));
            expect(queue).toBe(expectedQueue);
        }));
    });
    describe('method: add', () => {
        afterAll(() => {
            sinon_1.default.restore();
        });
        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 context = mocks_1.dsMockUtils.getContextInstance();
            const token = mocks_1.entityMockUtils.getSecurityTokenInstance();
            const trustedClaimIssuers = new TrustedClaimIssuers_1.TrustedClaimIssuers(token, context);
            const args = {
                claimIssuers: [
                    { identity: mocks_1.entityMockUtils.getIdentityInstance({ did: 'someDid' }) },
                    { identity: mocks_1.entityMockUtils.getIdentityInstance({ did: 'otherDid' }) },
                ],
            };
            const expectedQueue = 'someQueue';
            mocks_1.procedureMockUtils
                .getPrepareStub()
                .withArgs({
                args: Object.assign(Object.assign({ ticker: token.ticker }, args), { operation: internal_2.TrustedClaimIssuerOperation.Add }),
                transformer: undefined,
            }, context)
                .resolves(expectedQueue);
            const queue = yield trustedClaimIssuers.add(Object.assign({}, args));
            expect(queue).toBe(expectedQueue);
        }));
    });
    describe('method: remove', () => {
        afterAll(() => {
            sinon_1.default.restore();
        });
        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 context = mocks_1.dsMockUtils.getContextInstance();
            const token = mocks_1.entityMockUtils.getSecurityTokenInstance();
            const trustedClaimIssuers = new TrustedClaimIssuers_1.TrustedClaimIssuers(token, context);
            const args = {
                claimIssuers: ['someDid', 'otherDid'],
            };
            const expectedQueue = 'someQueue';
            mocks_1.procedureMockUtils
                .getPrepareStub()
                .withArgs({
                args: Object.assign(Object.assign({ ticker: token.ticker }, args), { operation: internal_2.TrustedClaimIssuerOperation.Remove }),
                transformer: undefined,
            }, context)
                .resolves(expectedQueue);
            const queue = yield trustedClaimIssuers.remove(Object.assign({}, args));
            expect(queue).toBe(expectedQueue);
        }));
    });
    describe('method: get', () => {
        let ticker;
        let rawTicker;
        let stringToTickerStub;
        let context;
        let token;
        let expectedDids;
        let expectedTrustedClaimIssuers;
        let claimIssuers;
        let trustedClaimIssuerStub;
        let trustedClaimIssuers;
        beforeAll(() => {
            ticker = 'test';
            rawTicker = mocks_1.dsMockUtils.createMockTicker(ticker);
            stringToTickerStub = sinon_1.default.stub(utilsConversionModule, 'stringToTicker');
            context = mocks_1.dsMockUtils.getContextInstance();
            token = mocks_1.entityMockUtils.getSecurityTokenInstance({ ticker });
            expectedDids = ['someDid', 'otherDid', 'yetAnotherDid'];
            expectedTrustedClaimIssuers = [];
            claimIssuers = [];
            expectedDids.forEach(did => {
                expectedTrustedClaimIssuers.push(new internal_1.DefaultTrustedClaimIssuer({ did, ticker }, context));
                claimIssuers.push(mocks_1.dsMockUtils.createMockTrustedIssuer({
                    issuer: mocks_1.dsMockUtils.createMockIdentityId(did),
                    // eslint-disable-next-line @typescript-eslint/naming-convention
                    trusted_for: mocks_1.dsMockUtils.createMockTrustedFor('Any'),
                }));
            });
            stringToTickerStub.withArgs(ticker, context).returns(rawTicker);
            trustedClaimIssuerStub = mocks_1.dsMockUtils.createQueryStub('complianceManager', 'trustedClaimIssuer');
            trustedClaimIssuers = new TrustedClaimIssuers_1.TrustedClaimIssuers(token, context);
        });
        afterAll(() => {
            sinon_1.default.restore();
        });
        test('should return the current default trusted claim issuers', () => __awaiter(void 0, void 0, void 0, function* () {
            trustedClaimIssuerStub.withArgs(rawTicker).resolves(claimIssuers);
            const result = yield trustedClaimIssuers.get();
            expect(result).toEqual(expectedTrustedClaimIssuers);
        }));
        test('should allow subscription', () => __awaiter(void 0, void 0, void 0, function* () {
            const unsubCallback = 'unsubCallback';
            trustedClaimIssuerStub.withArgs(rawTicker).callsFake((_, cbFunc) => {
                cbFunc(claimIssuers);
                return unsubCallback;
            });
            const callback = sinon_1.default.stub();
            const result = yield trustedClaimIssuers.get(callback);
            expect(result).toBe(unsubCallback);
            sinon_1.default.assert.calledWithExactly(callback, expectedTrustedClaimIssuers);
        }));
    });
});
//# sourceMappingURL=TrustedClaimIssuers.js.map