"use strict";
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 internal_1 = require("../../../../internal");
const mocks_1 = require("../../../../testUtils/mocks");
const types_1 = require("../../../../types");
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('~/api/entities/SecurityToken', require("../../../../testUtils/mocks/entities").mockSecurityTokenModule('~/api/entities/SecurityToken'));
jest.mock('~/base/Procedure', require("../../../../testUtils/mocks/procedure").mockProcedureModule('~/base/Procedure'));
describe('CorporateAction class', () => {
    let context;
    let id;
    let ticker;
    let declarationDate;
    let kind;
    let description;
    let targets;
    let defaultTaxWithholding;
    let taxWithholdings;
    let corporateAction;
    let corporateActionsQueryStub;
    // eslint-disable-next-line require-jsdoc
    class NonAbstract extends internal_1.CorporateActionBase {
        constructor() {
            super(...arguments);
            // eslint-disable-next-line @typescript-eslint/no-explicit-any
            this.modifyCheckpoint = {};
        }
    }
    beforeAll(() => {
        mocks_1.dsMockUtils.initMocks();
        mocks_1.entityMockUtils.initMocks();
        mocks_1.procedureMockUtils.initMocks();
    });
    beforeEach(() => {
        context = mocks_1.dsMockUtils.getContextInstance();
        id = new bignumber_js_1.default(1);
        ticker = 'SOME_TICKER';
        declarationDate = new Date('10/14/1987 UTC');
        kind = types_1.CorporateActionKind.UnpredictableBenefit;
        description = 'someDescription';
        targets = {
            identities: [],
            treatment: types_1.TargetTreatment.Exclude,
        };
        defaultTaxWithholding = new bignumber_js_1.default(10);
        taxWithholdings = [];
        corporateActionsQueryStub = mocks_1.dsMockUtils.createQueryStub('corporateAction', 'corporateActions', {
            returnValue: mocks_1.dsMockUtils.createMockOption(mocks_1.dsMockUtils.createMockCorporateAction({
                kind,
                /* eslint-disable @typescript-eslint/naming-convention */
                decl_date: declarationDate.getTime(),
                record_date: mocks_1.dsMockUtils.createMockRecordDate({
                    date: new Date('10/14/2019').getTime(),
                    checkpoint: { Scheduled: [mocks_1.dsMockUtils.createMockU64(1), mocks_1.dsMockUtils.createMockU64(1)] },
                }),
                targets: {
                    identities: [],
                    treatment: types_1.TargetTreatment.Exclude,
                },
                default_withholding_tax: 100000,
                withholding_tax: [],
                /* eslint-enable @typescript-eslint/naming-convention */
            })),
        });
        corporateAction = new NonAbstract({
            id,
            ticker,
            kind,
            declarationDate,
            description,
            targets,
            defaultTaxWithholding,
            taxWithholdings,
        }, context);
    });
    afterEach(() => {
        mocks_1.dsMockUtils.reset();
        mocks_1.entityMockUtils.reset();
        mocks_1.procedureMockUtils.reset();
    });
    afterAll(() => {
        mocks_1.dsMockUtils.cleanup();
        mocks_1.entityMockUtils.cleanup();
        mocks_1.procedureMockUtils.cleanup();
    });
    test('should extend Entity', () => {
        expect(internal_1.CorporateActionBase.prototype instanceof internal_1.Entity).toBe(true);
    });
    describe('constructor', () => {
        test('should assign parameters to instance', () => {
            expect(corporateAction.id).toEqual(id);
            expect(corporateAction.token.ticker).toBe(ticker);
            expect(corporateAction.declarationDate).toEqual(declarationDate);
            expect(corporateAction.description).toEqual(description);
            expect(corporateAction.targets).toEqual(targets);
            expect(corporateAction.defaultTaxWithholding).toEqual(defaultTaxWithholding);
            expect(corporateAction.taxWithholdings).toEqual(taxWithholdings);
        });
    });
    describe('method: isUniqueIdentifiers', () => {
        test('should return true if the object conforms to the interface', () => {
            expect(internal_1.CorporateActionBase.isUniqueIdentifiers({ ticker: 'SYMBOL', id: new bignumber_js_1.default(1) })).toBe(true);
            expect(internal_1.CorporateActionBase.isUniqueIdentifiers({})).toBe(false);
            expect(internal_1.CorporateActionBase.isUniqueIdentifiers({ ticker: 'SYMBOL' })).toBe(false);
            expect(internal_1.CorporateActionBase.isUniqueIdentifiers({ id: 1 })).toBe(false);
        });
    });
    describe('method: linkDocuments', () => {
        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 args = {
                documents: [
                    {
                        name: 'someName',
                        uri: 'someUri',
                        contentHash: 'someHash',
                    },
                ],
            };
            const expectedQueue = 'someQueue';
            mocks_1.procedureMockUtils
                .getPrepareStub()
                .withArgs({ args: Object.assign({ id, ticker }, args), transformer: undefined }, context)
                .resolves(expectedQueue);
            const queue = yield corporateAction.linkDocuments(args);
            expect(queue).toBe(expectedQueue);
        }));
    });
    describe('method: exists', () => {
        test('should return whether the CA exists', () => __awaiter(void 0, void 0, void 0, function* () {
            let result = yield corporateAction.exists();
            expect(result).toBe(true);
            corporateActionsQueryStub.resolves(mocks_1.dsMockUtils.createMockOption());
            result = yield corporateAction.exists();
            expect(result).toBe(false);
        }));
    });
    describe('method: checkpoint', () => {
        let schedulePointsQueryStub;
        beforeEach(() => {
            mocks_1.dsMockUtils.createQueryStub('checkpoint', 'schedules', {
                returnValue: [
                    mocks_1.dsMockUtils.createMockStoredSchedule({
                        schedule: {
                            start: new Date('10/14/1987').getTime(),
                            period: {
                                unit: 'Month',
                                amount: 2,
                            },
                        },
                        id: 1,
                        at: new Date('10/14/1987').getTime(),
                        remaining: 2,
                    }),
                ],
            });
            schedulePointsQueryStub = mocks_1.dsMockUtils.createQueryStub('checkpoint', 'schedulePoints', {
                returnValue: [],
            });
        });
        test('should throw an error if the Corporate Action does not exist', () => __awaiter(void 0, void 0, void 0, function* () {
            corporateActionsQueryStub.resolves(mocks_1.dsMockUtils.createMockOption());
            let err;
            try {
                yield corporateAction.checkpoint();
            }
            catch (error) {
                err = error;
            }
            expect(err.message).toBe('The Corporate Action no longer exists');
        }));
        test('should return the Checkpoint Schedule associated to the Corporate Action', () => __awaiter(void 0, void 0, void 0, function* () {
            const result = (yield corporateAction.checkpoint());
            expect(result.id).toEqual(new bignumber_js_1.default(1));
            expect(result.period).toEqual({ unit: types_1.CalendarUnit.Month, amount: 2 });
            expect(result.start).toEqual(new Date('10/14/1987'));
        }));
        test('should return null if the CA does not have a record date', () => __awaiter(void 0, void 0, void 0, function* () {
            corporateActionsQueryStub.resolves(mocks_1.dsMockUtils.createMockOption(mocks_1.dsMockUtils.createMockCorporateAction({
                kind,
                /* eslint-disable @typescript-eslint/naming-convention */
                decl_date: declarationDate.getTime(),
                record_date: null,
                targets: {
                    identities: [],
                    treatment: types_1.TargetTreatment.Exclude,
                },
                default_withholding_tax: 100000,
                withholding_tax: [],
                /* eslint-enable @typescript-eslint/naming-convention */
            })));
            const result = yield corporateAction.checkpoint();
            expect(result).toBeNull();
        }));
        test('should return null if the CA does not have a record date', () => __awaiter(void 0, void 0, void 0, function* () {
            schedulePointsQueryStub.resolves(['someCheckpoint', mocks_1.dsMockUtils.createMockU64(1)]);
            let result = (yield corporateAction.checkpoint());
            expect(result.id).toEqual(new bignumber_js_1.default(1));
            expect(result instanceof internal_1.Checkpoint);
            corporateActionsQueryStub.resolves(mocks_1.dsMockUtils.createMockOption(mocks_1.dsMockUtils.createMockCorporateAction({
                kind,
                /* eslint-disable @typescript-eslint/naming-convention */
                decl_date: declarationDate.getTime(),
                record_date: mocks_1.dsMockUtils.createMockRecordDate({
                    date: new Date('10/14/1987').getTime(),
                    checkpoint: { Existing: mocks_1.dsMockUtils.createMockU64(1) },
                }),
                targets: {
                    identities: [],
                    treatment: types_1.TargetTreatment.Exclude,
                },
                default_withholding_tax: 100000,
                withholding_tax: [],
                /* eslint-enable @typescript-eslint/naming-convention */
            })));
            result = (yield corporateAction.checkpoint());
            expect(result.id).toEqual(new bignumber_js_1.default(1));
            expect(result instanceof internal_1.Checkpoint);
        }));
    });
    describe('method: toJson', () => {
        test('should return a human readable version of the entity', () => {
            expect(corporateAction.toJson()).toEqual({
                id: '1',
                ticker: 'SOME_TICKER',
                declarationDate: '1987-10-14T00:00:00.000Z',
                defaultTaxWithholding: '10',
                description: 'someDescription',
                targets: {
                    identities: [],
                    treatment: types_1.TargetTreatment.Exclude,
                },
                taxWithholdings: [],
            });
        });
    });
});
//# sourceMappingURL=index.js.map