"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 queries_1 = require("../../../middleware/queries");
const mocks_1 = require("../../../testUtils/mocks");
const types_1 = require("../../../types");
const constants_1 = require("../../../utils/constants");
const utilsConversionModule = __importStar(require("../../../utils/conversion"));
const utilsInternalModule = __importStar(require("../../../utils/internal"));
describe('DefaultTrustedClaimIssuer class', () => {
    let context;
    beforeAll(() => {
        mocks_1.dsMockUtils.initMocks();
        mocks_1.entityMockUtils.initMocks();
    });
    beforeEach(() => {
        context = mocks_1.dsMockUtils.getContextInstance();
    });
    afterEach(() => {
        mocks_1.dsMockUtils.reset();
        mocks_1.entityMockUtils.reset();
    });
    afterAll(() => {
        mocks_1.dsMockUtils.cleanup();
        mocks_1.entityMockUtils.cleanup();
    });
    test('should extend Entity', () => {
        expect(internal_1.DefaultTrustedClaimIssuer.prototype instanceof internal_1.Identity).toBe(true);
    });
    describe('constructor', () => {
        test('should assign ticker and Identity to instance', () => {
            const did = 'someDid';
            const ticker = 'SOME_TICKER';
            const trustedClaimIssuer = new internal_1.DefaultTrustedClaimIssuer({ did, ticker }, context);
            expect(trustedClaimIssuer.asset.ticker).toBe(ticker);
            expect(trustedClaimIssuer.did).toEqual(did);
        });
    });
    describe('method: isUniqueIdentifiers', () => {
        test('should return true if the object conforms to the interface', () => {
            expect(internal_1.DefaultTrustedClaimIssuer.isUniqueIdentifiers({ did: 'someDid', ticker: 'symbol' })).toBe(true);
            expect(internal_1.DefaultTrustedClaimIssuer.isUniqueIdentifiers({})).toBe(false);
            expect(internal_1.DefaultTrustedClaimIssuer.isUniqueIdentifiers({ did: 'someDid' })).toBe(false);
            expect(internal_1.DefaultTrustedClaimIssuer.isUniqueIdentifiers({ did: 1 })).toBe(false);
        });
    });
    describe('method: addedAt', () => {
        const did = 'someDid';
        const ticker = 'SOME_TICKER';
        const variables = {
            ticker: utilsInternalModule.padString(ticker, constants_1.MAX_TICKER_LENGTH),
            identityId: did,
        };
        test('should return the event identifier object of the trusted claim issuer creation', () => __awaiter(void 0, void 0, void 0, function* () {
            const blockNumber = new bignumber_js_1.default(1234);
            const blockDate = new Date('4/14/2020');
            const eventIdx = 1;
            const fakeResult = { blockNumber, blockDate, eventIndex: eventIdx };
            const trustedClaimIssuer = new internal_1.DefaultTrustedClaimIssuer({ did, ticker }, context);
            mocks_1.dsMockUtils.createApolloQueryStub(queries_1.eventByAddedTrustedClaimIssuer(variables), {
                /* eslint-disable @typescript-eslint/naming-convention */
                eventByAddedTrustedClaimIssuer: {
                    block_id: blockNumber.toNumber(),
                    block: { datetime: blockDate },
                    event_idx: eventIdx,
                },
                /* eslint-enable @typescript-eslint/naming-convention */
            });
            const result = yield trustedClaimIssuer.addedAt();
            expect(result).toEqual(fakeResult);
        }));
        test('should return null if the query result is empty', () => __awaiter(void 0, void 0, void 0, function* () {
            const trustedClaimIssuer = new internal_1.DefaultTrustedClaimIssuer({ did, ticker }, context);
            mocks_1.dsMockUtils.createApolloQueryStub(queries_1.eventByAddedTrustedClaimIssuer(variables), {});
            const result = yield trustedClaimIssuer.addedAt();
            expect(result).toBeNull();
        }));
    });
    describe('method: trustedFor', () => {
        let ticker;
        let rawTicker;
        let stringToTickerStub;
        let claimIssuers;
        let trustedClaimIssuerStub;
        beforeAll(() => {
            ticker = 'SOME_TICKER';
            rawTicker = mocks_1.dsMockUtils.createMockTicker(ticker);
            stringToTickerStub = sinon_1.default.stub(utilsConversionModule, 'stringToTicker');
            claimIssuers = [
                mocks_1.dsMockUtils.createMockTrustedIssuer({
                    issuer: mocks_1.dsMockUtils.createMockIdentityId('someDid'),
                    // eslint-disable-next-line @typescript-eslint/naming-convention
                    trusted_for: mocks_1.dsMockUtils.createMockTrustedFor('Any'),
                }),
                mocks_1.dsMockUtils.createMockTrustedIssuer({
                    issuer: mocks_1.dsMockUtils.createMockIdentityId('otherDid'),
                    // eslint-disable-next-line @typescript-eslint/naming-convention
                    trusted_for: mocks_1.dsMockUtils.createMockTrustedFor({
                        Specific: [mocks_1.dsMockUtils.createMockClaimType(types_1.ClaimType.Exempted)],
                    }),
                }),
            ];
        });
        beforeEach(() => {
            stringToTickerStub.withArgs(ticker, context).returns(rawTicker);
            trustedClaimIssuerStub = mocks_1.dsMockUtils.createQueryStub('complianceManager', 'trustedClaimIssuer');
            trustedClaimIssuerStub.withArgs(rawTicker).resolves(claimIssuers);
        });
        afterAll(() => {
            sinon_1.default.restore();
        });
        test('should return the claim types for which the Claim Issuer is trusted', () => __awaiter(void 0, void 0, void 0, function* () {
            let trustedClaimIssuer = new internal_1.DefaultTrustedClaimIssuer({ did: 'someDid', ticker }, context);
            let result = yield trustedClaimIssuer.trustedFor();
            expect(result).toBeNull();
            trustedClaimIssuer = new internal_1.DefaultTrustedClaimIssuer({ did: 'otherDid', ticker }, context);
            result = yield trustedClaimIssuer.trustedFor();
            expect(result).toEqual([types_1.ClaimType.Exempted]);
        }));
        test('should throw an error if the Identity is no longer a trusted Claim Issuer', () => __awaiter(void 0, void 0, void 0, function* () {
            const did = 'randomDid';
            const trustedClaimIssuer = new internal_1.DefaultTrustedClaimIssuer({ did, ticker }, context);
            let err;
            try {
                yield trustedClaimIssuer.trustedFor();
            }
            catch (error) {
                err = error;
            }
            expect(err.message).toBe(`The Identity with DID "${did}" is no longer a trusted issuer for "${ticker}"`);
        }));
    });
});
//# sourceMappingURL=DefaultTrustedClaimIssuer.js.map