"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 registerIdentity_1 = require("../registerIdentity");
const mocks_1 = require("../../../testUtils/mocks");
const utilsConversionModule = __importStar(require("../../../utils/conversion"));
const utilsInternalModule = __importStar(require("../../../utils/internal"));
describe('registerIdentity procedure', () => {
    let mockContext;
    let stringToAccountIdStub;
    let secondaryAccountToMeshSecondaryKeyStub;
    let addTransactionStub;
    let registerIdentityTransaction;
    let identity;
    beforeAll(() => {
        mocks_1.entityMockUtils.initMocks();
        mocks_1.procedureMockUtils.initMocks();
        mocks_1.dsMockUtils.initMocks();
        stringToAccountIdStub = sinon_1.default.stub(utilsConversionModule, 'stringToAccountId');
        secondaryAccountToMeshSecondaryKeyStub = sinon_1.default.stub(utilsConversionModule, 'secondaryAccountToMeshSecondaryKey');
        identity = 'identity';
    });
    beforeEach(() => {
        mockContext = mocks_1.dsMockUtils.getContextInstance();
        addTransactionStub = mocks_1.procedureMockUtils.getAddTransactionStub().returns([identity]);
        registerIdentityTransaction = mocks_1.dsMockUtils.createTxStub('identity', 'cddRegisterDid');
    });
    afterEach(() => {
        mocks_1.entityMockUtils.reset();
        mocks_1.procedureMockUtils.reset();
        mocks_1.dsMockUtils.reset();
    });
    afterAll(() => {
        mocks_1.procedureMockUtils.cleanup();
        mocks_1.dsMockUtils.cleanup();
    });
    test('should add a cddRegisterIdentity transaction to the queue', () => __awaiter(void 0, void 0, void 0, function* () {
        const targetAccount = 'someAccount';
        const secondaryAccounts = [
            {
                account: mocks_1.entityMockUtils.getAccountInstance({ address: 'someValue' }),
                permissions: {
                    assets: null,
                    portfolios: null,
                    transactions: null,
                    transactionGroups: [],
                },
            },
        ];
        const args = {
            targetAccount,
            secondaryAccounts,
        };
        const rawAccountId = mocks_1.dsMockUtils.createMockAccountId(targetAccount);
        const rawSecondaryAccount = mocks_1.dsMockUtils.createMockSecondaryKey({
            signer: mocks_1.dsMockUtils.createMockSignatory({
                Account: mocks_1.dsMockUtils.createMockAccountId(secondaryAccounts[0].account.address),
            }),
            permissions: mocks_1.dsMockUtils.createMockPermissions(),
        });
        const proc = mocks_1.procedureMockUtils.getInstance(mockContext);
        stringToAccountIdStub.withArgs(targetAccount, mockContext).returns(rawAccountId);
        secondaryAccountToMeshSecondaryKeyStub
            .withArgs(secondaryAccounts[0], mockContext)
            .returns(rawSecondaryAccount);
        let result = yield registerIdentity_1.prepareRegisterIdentity.call(proc, args);
        sinon_1.default.assert.calledWith(addTransactionStub, sinon_1.default.match({
            transaction: registerIdentityTransaction,
            resolvers: sinon_1.default.match.array,
            args: [rawAccountId, [rawSecondaryAccount]],
        }));
        expect(result).toBe(identity);
        result = yield registerIdentity_1.prepareRegisterIdentity.call(proc, { targetAccount });
        sinon_1.default.assert.calledWith(addTransactionStub, sinon_1.default.match({
            transaction: registerIdentityTransaction,
            resolvers: sinon_1.default.match.array,
            args: [rawAccountId, []],
        }));
        expect(result).toBe(identity);
    }));
});
describe('createRegisterIdentityResolver', () => {
    const filterEventRecordsStub = sinon_1.default.stub(utilsInternalModule, 'filterEventRecords');
    const did = 'someDid';
    const rawDid = mocks_1.dsMockUtils.createMockIdentityId(did);
    beforeAll(() => {
        mocks_1.entityMockUtils.initMocks({
            identityOptions: {
                did,
            },
        });
    });
    beforeEach(() => {
        filterEventRecordsStub.returns([
            mocks_1.dsMockUtils.createMockIEvent([rawDid, 'accountId', 'signingItem']),
        ]);
    });
    afterEach(() => {
        filterEventRecordsStub.reset();
    });
    test('should return the new Identity', () => {
        const fakeContext = {};
        const result = registerIdentity_1.createRegisterIdentityResolver(fakeContext)({});
        expect(result.did).toEqual(did);
    });
});
//# sourceMappingURL=registerIdentity.js.map