"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 utilsConversionModule = __importStar(require("../../../../utils/conversion"));
jest.mock('~/api/entities/Identity', require("../../../../testUtils/mocks/entities").mockIdentityModule('~/api/entities/Identity'));
jest.mock('~/api/entities/Asset', require("../../../../testUtils/mocks/entities").mockAssetModule('~/api/entities/Asset'));
describe('CheckpointSchedule class', () => {
    let context;
    let id;
    let ticker;
    let period;
    let start;
    let remaining;
    let nextCheckpointDate;
    let stringToTickerStub;
    let numberToU64Stub;
    beforeAll(() => {
        mocks_1.dsMockUtils.initMocks();
        mocks_1.entityMockUtils.initMocks();
        id = new bignumber_js_1.default(1);
        ticker = 'SOME_TICKER';
        period = {
            unit: types_1.CalendarUnit.Month,
            amount: 1,
        };
        start = new Date('10/14/1987 UTC');
        remaining = 11;
        nextCheckpointDate = new Date(new Date().getTime() + 60 * 60 * 1000 * 24 * 365 * 60);
        stringToTickerStub = sinon_1.default.stub(utilsConversionModule, 'stringToTicker');
        numberToU64Stub = sinon_1.default.stub(utilsConversionModule, 'numberToU64');
    });
    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.CheckpointSchedule.prototype instanceof internal_1.Entity).toBe(true);
    });
    describe('constructor', () => {
        test('should assign ticker and id to instance', () => {
            let schedule = new internal_1.CheckpointSchedule({ id, ticker, start, period, remaining: 0, nextCheckpointDate }, context);
            expect(schedule.asset.ticker).toBe(ticker);
            expect(schedule.id).toEqual(id);
            expect(schedule.period).toEqual(period);
            expect(schedule.start).toEqual(start);
            expect(schedule.expiryDate).toBeNull();
            schedule = new internal_1.CheckpointSchedule({
                id,
                ticker,
                start,
                period: { unit: types_1.CalendarUnit.Month, amount: 0 },
                remaining,
                nextCheckpointDate,
            }, context);
            expect(schedule.asset.ticker).toBe(ticker);
            expect(schedule.id).toEqual(id);
            expect(schedule.period).toEqual(null);
            expect(schedule.start).toEqual(start);
            expect(schedule.expiryDate).toEqual(start);
            schedule = new internal_1.CheckpointSchedule({
                id,
                ticker,
                start,
                period: { unit: types_1.CalendarUnit.Year, amount: 1 },
                remaining: 2,
                nextCheckpointDate: start,
            }, context);
            expect(schedule.expiryDate).toEqual(new Date('10/14/1988 UTC'));
        });
    });
    describe('method: isUniqueIdentifiers', () => {
        test('should return true if the object conforms to the interface', () => {
            expect(internal_1.CheckpointSchedule.isUniqueIdentifiers({ id: new bignumber_js_1.default(1), ticker: 'symbol' })).toBe(true);
            expect(internal_1.CheckpointSchedule.isUniqueIdentifiers({})).toBe(false);
            expect(internal_1.CheckpointSchedule.isUniqueIdentifiers({ id: new bignumber_js_1.default(1) })).toBe(false);
            expect(internal_1.CheckpointSchedule.isUniqueIdentifiers({ id: 'id' })).toBe(false);
        });
    });
    describe('method: details', () => {
        test('should throw an error if Schedule does not exists', () => __awaiter(void 0, void 0, void 0, function* () {
            const checkpointSchedule = new internal_1.CheckpointSchedule({ id: new bignumber_js_1.default(2), ticker, period, start, remaining, nextCheckpointDate }, context);
            const rawScheduleId = mocks_1.dsMockUtils.createMockU64(id.toNumber());
            stringToTickerStub.returns(mocks_1.dsMockUtils.createMockTicker(ticker));
            mocks_1.dsMockUtils.createQueryStub('checkpoint', 'schedules', {
                returnValue: [
                    mocks_1.dsMockUtils.createMockStoredSchedule({
                        id: rawScheduleId,
                    }),
                ],
            });
            let error;
            try {
                yield checkpointSchedule.details();
            }
            catch (err) {
                error = err;
            }
            expect(error.message).toBe('Schedule no longer exists. It was either removed or it expired');
        }));
        test('should return the Schedule details ', () => __awaiter(void 0, void 0, void 0, function* () {
            const rawRemaining = new bignumber_js_1.default(2);
            const checkpointSchedule = new internal_1.CheckpointSchedule({ id, ticker, period, start, remaining, nextCheckpointDate }, context);
            const rawScheduleId = mocks_1.dsMockUtils.createMockU64(id.toNumber());
            stringToTickerStub.returns(mocks_1.dsMockUtils.createMockTicker(ticker));
            sinon_1.default.stub(utilsConversionModule, 'u32ToBigNumber').returns(rawRemaining);
            sinon_1.default.stub(utilsConversionModule, 'momentToDate').returns(nextCheckpointDate);
            mocks_1.dsMockUtils.createQueryStub('checkpoint', 'schedules', {
                returnValue: [
                    mocks_1.dsMockUtils.createMockStoredSchedule({
                        schedule: mocks_1.dsMockUtils.createMockCheckpointSchedule({
                            start: mocks_1.dsMockUtils.createMockMoment(start.getTime()),
                            period: mocks_1.dsMockUtils.createMockCalendarPeriod({
                                unit: mocks_1.dsMockUtils.createMockCalendarUnit('Month'),
                                amount: mocks_1.dsMockUtils.createMockU64(1),
                            }),
                        }),
                        id: rawScheduleId,
                        at: mocks_1.dsMockUtils.createMockMoment(nextCheckpointDate.getTime()),
                        remaining: mocks_1.dsMockUtils.createMockU32(rawRemaining.toNumber()),
                    }),
                ],
            });
            const result = yield checkpointSchedule.details();
            expect(result.remainingCheckpoints).toEqual(rawRemaining.toNumber());
            expect(result.nextCheckpointDate).toEqual(nextCheckpointDate);
        }));
    });
    describe('method: getCheckpoints', () => {
        test("should throw an error if the schedule doesn't exist", () => __awaiter(void 0, void 0, void 0, function* () {
            const schedule = new internal_1.CheckpointSchedule({ id: new bignumber_js_1.default(2), ticker, start, period, remaining, nextCheckpointDate }, context);
            const rawScheduleId = mocks_1.dsMockUtils.createMockU64(id.toNumber());
            mocks_1.dsMockUtils.createQueryStub('checkpoint', 'schedules', {
                returnValue: [
                    mocks_1.dsMockUtils.createMockStoredSchedule({
                        id: rawScheduleId,
                    }),
                ],
            });
            let err;
            try {
                yield schedule.getCheckpoints();
            }
            catch (error) {
                err = error;
            }
            expect(err.message).toBe('Schedule no longer exists. It was either removed or it expired');
        }));
        test('should return all the checkpoints created by the schedule', () => __awaiter(void 0, void 0, void 0, function* () {
            const schedule = new internal_1.CheckpointSchedule({ id, ticker, start, period, remaining, nextCheckpointDate }, context);
            const firstId = new bignumber_js_1.default(1);
            const secondId = new bignumber_js_1.default(2);
            const rawFirstId = mocks_1.dsMockUtils.createMockU64(firstId.toNumber());
            const rawSecondId = mocks_1.dsMockUtils.createMockU64(secondId.toNumber());
            const rawScheduleId = mocks_1.dsMockUtils.createMockU64(id.toNumber());
            mocks_1.dsMockUtils.createQueryStub('checkpoint', 'schedules', {
                returnValue: [
                    mocks_1.dsMockUtils.createMockStoredSchedule({
                        id: rawScheduleId,
                    }),
                ],
            });
            mocks_1.dsMockUtils.createQueryStub('checkpoint', 'schedulePoints', {
                returnValue: [rawFirstId, rawSecondId],
            });
            numberToU64Stub.withArgs(firstId).returns(rawFirstId);
            numberToU64Stub.withArgs(secondId).returns(rawSecondId);
            const result = yield schedule.getCheckpoints();
            expect(result[0].id).toEqual(firstId);
            expect(result[1].id).toEqual(secondId);
        }));
    });
    describe('method: exists', () => {
        test('should return whether the schedule exists', () => __awaiter(void 0, void 0, void 0, function* () {
            let schedule = new internal_1.CheckpointSchedule({ id, ticker, start, period, remaining, nextCheckpointDate }, context);
            mocks_1.dsMockUtils.createQueryStub('checkpoint', 'schedules', {
                returnValue: [
                    mocks_1.dsMockUtils.createMockStoredSchedule({
                        id: mocks_1.dsMockUtils.createMockU64(id.toNumber()),
                    }),
                ],
            });
            let result = yield schedule.exists();
            expect(result).toBe(true);
            schedule = new internal_1.CheckpointSchedule({ id: new bignumber_js_1.default(2), ticker, start, period, remaining, nextCheckpointDate }, context);
            result = yield schedule.exists();
            expect(result).toBe(false);
        }));
    });
    describe('method: toJson', () => {
        test('should return a human readable version of the entity', () => {
            var _a;
            const schedule = new internal_1.CheckpointSchedule({
                id: new bignumber_js_1.default(1),
                ticker: 'SOME_TICKER',
                start,
                period,
                remaining,
                nextCheckpointDate,
            }, context);
            expect(schedule.toJson()).toEqual({
                id: '1',
                ticker: 'SOME_TICKER',
                period,
                start: '1987-10-14T00:00:00.000Z',
                complexity: 12,
                expiryDate: (_a = schedule.expiryDate) === null || _a === void 0 ? void 0 : _a.toISOString(),
            });
        });
    });
});
//# sourceMappingURL=index.js.map