"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 mocks_1 = require("../../../../testUtils/mocks");
const types_1 = require("../../../../types");
const utils_1 = require("../../../../types/utils");
const utilsConversionModule = __importStar(require("../../../../utils/conversion"));
const utilsInternalModule = __importStar(require("../../../../utils/internal"));
const IdentityAuthorizations_1 = require("../IdentityAuthorizations");
jest.mock('~/api/entities/Identity', require("../../../../testUtils/mocks/entities").mockIdentityModule('~/api/entities/Identity'));
jest.mock('~/api/entities/AuthorizationRequest', require("../../../../testUtils/mocks/entities").mockAuthorizationRequestModule('~/api/entities/AuthorizationRequest'));
describe('IdentityAuthorizations class', () => {
    beforeAll(() => {
        mocks_1.entityMockUtils.initMocks();
        mocks_1.dsMockUtils.initMocks();
    });
    afterEach(() => {
        mocks_1.entityMockUtils.reset();
        mocks_1.dsMockUtils.reset();
    });
    afterAll(() => {
        mocks_1.entityMockUtils.cleanup();
        mocks_1.dsMockUtils.cleanup();
    });
    test('should extend namespace', () => {
        expect(IdentityAuthorizations_1.IdentityAuthorizations.prototype instanceof internal_1.Namespace).toBe(true);
    });
    describe('method: getSent', () => {
        afterAll(() => {
            sinon_1.default.restore();
        });
        test('should retrieve all pending authorizations sent by the Identity', () => __awaiter(void 0, void 0, void 0, function* () {
            sinon_1.default.stub(utilsConversionModule, 'signerValueToSignatory');
            mocks_1.dsMockUtils.createQueryStub('identity', 'authorizationsGiven');
            const requestPaginatedStub = sinon_1.default.stub(utilsInternalModule, 'requestPaginated');
            const did = 'someDid';
            const context = mocks_1.dsMockUtils.getContextInstance({ did });
            const identity = mocks_1.entityMockUtils.getIdentityInstance({ did });
            const authsNamespace = new IdentityAuthorizations_1.IdentityAuthorizations(identity, context);
            /* eslint-disable @typescript-eslint/naming-convention */
            const authParams = [
                {
                    authId: new bignumber_js_1.default(1),
                    expiry: null,
                    data: { type: types_1.AuthorizationType.TransferAssetOwnership, value: 'myTicker' },
                    target: mocks_1.entityMockUtils.getIdentityInstance({ did: 'alice' }),
                    issuer: identity,
                },
                {
                    authId: new bignumber_js_1.default(2),
                    expiry: new Date('10/14/3040'),
                    data: { type: types_1.AuthorizationType.TransferAssetOwnership, value: 'otherTicker' },
                    target: mocks_1.entityMockUtils.getIdentityInstance({ did: 'bob' }),
                    issuer: identity,
                },
            ];
            const authorizations = authParams.map(({ authId, expiry, data }) => mocks_1.dsMockUtils.createMockAuthorization({
                auth_id: mocks_1.dsMockUtils.createMockU64(authId.toNumber()),
                expiry: mocks_1.dsMockUtils.createMockOption(expiry ? mocks_1.dsMockUtils.createMockMoment(expiry.getTime()) : expiry),
                authorization_data: mocks_1.dsMockUtils.createMockAuthorizationData({
                    TransferAssetOwnership: mocks_1.dsMockUtils.createMockTicker(data.value),
                }),
                authorized_by: mocks_1.dsMockUtils.createMockIdentityId(did),
            }));
            /* eslint-enable @typescript-eslint/naming-convention */
            const authorizationsGivenEntries = authorizations.map(({ authorized_by: issuer, auth_id: authId }, index) => utils_1.tuple({ args: [issuer, authId] }, mocks_1.dsMockUtils.createMockSignatory({
                Identity: mocks_1.dsMockUtils.createMockIdentityId(authParams[index].target.did),
            })));
            requestPaginatedStub.resolves({ entries: authorizationsGivenEntries, lastKey: null });
            const authsMultiArgs = authorizationsGivenEntries.map(([keys, signatory]) => utils_1.tuple(signatory, keys.args[1]));
            const authorizationsStub = mocks_1.dsMockUtils.createQueryStub('identity', 'authorizations');
            authorizationsStub.multi
                .withArgs(authsMultiArgs)
                .resolves(authorizations.map(mocks_1.dsMockUtils.createMockOption));
            const expectedAuthorizations = authParams.map(({ authId, target, issuer, expiry, data }) => mocks_1.entityMockUtils.getAuthorizationRequestInstance({
                authId,
                issuer,
                target,
                expiry,
                data,
            }));
            const result = yield authsNamespace.getSent();
            result.data.forEach(({ issuer, authId, target, expiry, data }, index) => {
                const { issuer: expectedIssuer, authId: expectedAuthId, target: expectedTarget, expiry: expectedExpiry, data: expectedData, } = expectedAuthorizations[index];
                expect(issuer.did).toBe(expectedIssuer.did);
                expect(utilsConversionModule.signerToString(target)).toBe(utilsConversionModule.signerToString(expectedTarget));
                expect(authId).toEqual(expectedAuthId);
                expect(expiry).toEqual(expectedExpiry);
                expect(data).toEqual(expectedData);
            });
            expect(result.next).toBeNull();
        }));
    });
});
//# sourceMappingURL=IdentityAuthorizations.js.map