"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 __1 = require("../");
jest.mock('~/api/entities/Checkpoint', require("../../../../../testUtils/mocks/entities").mockCheckpointModule('~/api/entities/Checkpoint'));
jest.mock('~/api/entities/CheckpointSchedule', require("../../../../../testUtils/mocks/entities").mockCheckpointScheduleModule('~/api/entities/CheckpointSchedule'));
jest.mock('~/base/Procedure', require("../../../../../testUtils/mocks/procedure").mockProcedureModule('~/base/Procedure'));
describe('Checkpoints class', () => {
    const ticker = 'SOME_TICKER';
    const rawTicker = mocks_1.dsMockUtils.createMockTicker(ticker);
    let checkpoints;
    let context;
    beforeAll(() => {
        mocks_1.entityMockUtils.initMocks();
        mocks_1.dsMockUtils.initMocks();
        mocks_1.procedureMockUtils.initMocks();
    });
    afterEach(() => {
        mocks_1.entityMockUtils.reset();
        mocks_1.dsMockUtils.reset();
        mocks_1.procedureMockUtils.reset();
        context = mocks_1.dsMockUtils.getContextInstance();
        const token = mocks_1.entityMockUtils.getSecurityTokenInstance({ ticker });
        checkpoints = new __1.Checkpoints(token, context);
    });
    afterAll(() => {
        mocks_1.entityMockUtils.cleanup();
        mocks_1.dsMockUtils.cleanup();
        mocks_1.procedureMockUtils.cleanup();
    });
    test('should extend namespace', () => {
        expect(__1.Checkpoints.prototype instanceof internal_1.Namespace).toBe(true);
    });
    describe('method: create', () => {
        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 expectedQueue = 'someQueue';
            mocks_1.procedureMockUtils
                .getPrepareStub()
                .withArgs({ args: { ticker }, transformer: undefined }, context)
                .resolves(expectedQueue);
            const queue = yield checkpoints.create();
            expect(queue).toBe(expectedQueue);
        }));
    });
    describe('method: getOne', () => {
        afterAll(() => {
            sinon_1.default.restore();
        });
        test('should return the requested Checkpoint', () => __awaiter(void 0, void 0, void 0, function* () {
            const id = new bignumber_js_1.default(1);
            return expect(checkpoints.getOne({ id })).resolves.toEqual(mocks_1.entityMockUtils.getCheckpointInstance({ id }));
        }));
        test('should throw an error if the Checkpoint does not exist', () => __awaiter(void 0, void 0, void 0, function* () {
            const id = new bignumber_js_1.default(1);
            mocks_1.entityMockUtils.configureMocks({ checkpointOptions: { exists: false } });
            return expect(checkpoints.getOne({ id })).rejects.toThrow('The Checkpoint does not exist');
        }));
    });
    describe('method: get', () => {
        afterAll(() => {
            sinon_1.default.restore();
        });
        test('should return all created checkpoints with their timestamps and total supply', () => __awaiter(void 0, void 0, void 0, function* () {
            const stringToTickerStub = sinon_1.default.stub(utilsConversionModule, 'stringToTicker');
            mocks_1.dsMockUtils.createQueryStub('checkpoint', 'totalSupply');
            const requestPaginatedStub = sinon_1.default.stub(utilsInternalModule, 'requestPaginated');
            const totalSupply = [
                {
                    checkpointId: new bignumber_js_1.default(1),
                    balance: new bignumber_js_1.default(100),
                    moment: new Date('10/10/2020'),
                },
                {
                    checkpointId: new bignumber_js_1.default(2),
                    balance: new bignumber_js_1.default(1000),
                    moment: new Date('11/11/2020'),
                },
            ];
            stringToTickerStub.withArgs(ticker, context).returns(rawTicker);
            const rawTotalSupply = totalSupply.map(({ checkpointId, balance }) => ({
                checkpointId: mocks_1.dsMockUtils.createMockU64(checkpointId.toNumber()),
                balance: mocks_1.dsMockUtils.createMockBalance(balance.toNumber()),
            }));
            const totalSupplyEntries = rawTotalSupply.map(({ checkpointId, balance }) => utils_1.tuple({ args: [rawTicker, checkpointId] }, balance));
            requestPaginatedStub.resolves({ entries: totalSupplyEntries, lastKey: null });
            const timestampsStub = mocks_1.dsMockUtils.createQueryStub('checkpoint', 'timestamps');
            timestampsStub.multi.resolves(totalSupply.map(({ moment }) => mocks_1.dsMockUtils.createMockMoment(moment.getTime())));
            const result = yield checkpoints.get();
            result.data.forEach(({ checkpoint, totalSupply: ts, createdAt }, index) => {
                const { checkpointId: expectedCheckpointId, balance: expectedBalance, moment: expectedMoment, } = totalSupply[index];
                expect(checkpoint).toEqual(mocks_1.entityMockUtils.getCheckpointInstance({ id: expectedCheckpointId }));
                expect(ts).toEqual(expectedBalance.shiftedBy(-6));
                expect(createdAt).toEqual(expectedMoment);
            });
            expect(result.next).toBeNull();
        }));
    });
});
//# sourceMappingURL=index.js.map