"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 utils_1 = require("../../../../types/utils");
const utilsConversionModule = __importStar(require("../../../../utils/conversion"));
const utilsInternalModule = __importStar(require("../../../../utils/internal"));
const AssetHolders_1 = require("../AssetHolders");
jest.mock('~/api/entities/Asset', require("../../../../testUtils/mocks/entities").mockAssetModule('~/api/entities/Asset'));
jest.mock('~/api/entities/Identity', require("../../../../testUtils/mocks/entities").mockIdentityModule('~/api/entities/Identity'));
describe('AssetHolder class', () => {
    let ticker;
    let mockContext;
    let rawTicker;
    let requestPaginatedStub;
    let identityIdToStringStub;
    let balanceToBigNumberStub;
    const fakeData = [
        {
            identity: 'someIdentity',
            value: new bignumber_js_1.default(1000),
        },
        {
            identity: 'otherIdentity',
            value: new bignumber_js_1.default(2000),
        },
    ];
    beforeAll(() => {
        mocks_1.entityMockUtils.initMocks();
        mocks_1.dsMockUtils.initMocks();
        ticker = 'TEST';
        mockContext = mocks_1.dsMockUtils.getContextInstance();
        rawTicker = mocks_1.dsMockUtils.createMockTicker(ticker);
        requestPaginatedStub = sinon_1.default.stub(utilsInternalModule, 'requestPaginated');
        identityIdToStringStub = sinon_1.default.stub(utilsConversionModule, 'identityIdToString');
        balanceToBigNumberStub = sinon_1.default.stub(utilsConversionModule, 'balanceToBigNumber');
        sinon_1.default
            .stub(utilsConversionModule, 'stringToTicker')
            .withArgs(ticker, mockContext)
            .returns(rawTicker);
    });
    afterEach(() => {
        mocks_1.entityMockUtils.reset();
        mocks_1.dsMockUtils.reset();
    });
    afterAll(() => {
        mocks_1.dsMockUtils.cleanup();
    });
    test('should extend namespace', () => {
        expect(AssetHolders_1.AssetHolders.prototype instanceof internal_1.Namespace).toBe(true);
    });
    describe('method: get', () => {
        afterAll(() => {
            sinon_1.default.restore();
        });
        test('should retrieve all the Asset Holders with balance', () => __awaiter(void 0, void 0, void 0, function* () {
            mocks_1.dsMockUtils.createQueryStub('asset', 'balanceOf');
            const expectedHolders = [];
            const balanceOfEntries = [];
            const context = mocks_1.dsMockUtils.getContextInstance();
            fakeData.forEach(({ identity, value }) => {
                const identityId = mocks_1.dsMockUtils.createMockIdentityId(identity);
                const fakeBalance = mocks_1.dsMockUtils.createMockBalance(value);
                const balance = new bignumber_js_1.default(value);
                identityIdToStringStub.withArgs(identityId).returns(identity);
                balanceToBigNumberStub.withArgs(fakeBalance).returns(balance);
                balanceOfEntries.push(utils_1.tuple({ args: [rawTicker, identityId] }, fakeBalance));
                expectedHolders.push({
                    identity: expect.objectContaining({ did: identity }),
                    balance,
                });
            });
            requestPaginatedStub.resolves({ entries: balanceOfEntries, lastKey: null });
            const asset = mocks_1.entityMockUtils.getAssetInstance();
            const assetHolders = new AssetHolders_1.AssetHolders(asset, context);
            const result = yield assetHolders.get();
            expect(result.data).toEqual(expectedHolders);
            expect(result.next).toBeNull();
        }));
        test('should retrieve the first page of results with only one Asset Holder', () => __awaiter(void 0, void 0, void 0, function* () {
            mocks_1.dsMockUtils.createQueryStub('asset', 'balanceOf');
            const expectedHolders = [];
            const balanceOfEntries = [];
            const context = mocks_1.dsMockUtils.getContextInstance();
            const { identity, value } = fakeData[0];
            const identityId = mocks_1.dsMockUtils.createMockIdentityId(identity);
            const fakeBalance = mocks_1.dsMockUtils.createMockBalance(value);
            const balance = new bignumber_js_1.default(value);
            identityIdToStringStub.withArgs(identityId).returns(identity);
            balanceToBigNumberStub.withArgs(fakeBalance).returns(balance);
            balanceOfEntries.push(utils_1.tuple({ args: [rawTicker, identityId] }, fakeBalance));
            expectedHolders.push({
                identity: expect.objectContaining({ did: identity }),
                balance,
            });
            requestPaginatedStub.resolves({ entries: balanceOfEntries, lastKey: 'someKey' });
            const asset = mocks_1.entityMockUtils.getAssetInstance();
            const assetHolders = new AssetHolders_1.AssetHolders(asset, context);
            const result = yield assetHolders.get({ size: new bignumber_js_1.default(1) });
            expect(result.data).toEqual(expectedHolders);
            expect(result.next).toBe('someKey');
        }));
    });
});
//# sourceMappingURL=AssetHolders.js.map