"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 types_1 = require("../../../polkadot/types");
const sinon_1 = __importDefault(require("sinon"));
const modifyDistributionCheckpoint_1 = require("../modifyDistributionCheckpoint");
const internal_1 = require("../../../internal");
const mocks_1 = require("../../../testUtils/mocks");
jest.mock('~/api/entities/CheckpointSchedule', require("../../../testUtils/mocks/entities").mockCheckpointScheduleModule('~/api/entities/CheckpointSchedule'));
jest.mock('~/base/Procedure', require("../../../testUtils/mocks/procedure").mockProcedureModule('~/base/Procedure'));
jest.mock('~/api/entities/Asset', require("../../../testUtils/mocks/entities").mockAssetModule('~/api/entities/Asset'));
describe('modifyDistributionCheckpoint procedure', () => {
    const ticker = 'SOME_TICKER';
    let mockContext;
    let addProcedureStub;
    beforeAll(() => {
        mocks_1.dsMockUtils.initMocks();
        mocks_1.procedureMockUtils.initMocks();
        mocks_1.entityMockUtils.initMocks();
    });
    beforeEach(() => {
        addProcedureStub = mocks_1.procedureMockUtils.getAddProcedureStub();
        mockContext = mocks_1.dsMockUtils.getContextInstance();
    });
    afterEach(() => {
        mocks_1.entityMockUtils.reset();
        mocks_1.procedureMockUtils.reset();
        mocks_1.dsMockUtils.reset();
    });
    afterAll(() => {
        mocks_1.procedureMockUtils.cleanup();
        mocks_1.dsMockUtils.cleanup();
    });
    test('should throw an error if the distribution has already started', () => __awaiter(void 0, void 0, void 0, function* () {
        const args = {
            distribution: mocks_1.entityMockUtils.getDividendDistributionInstance({
                paymentDate: new Date('10/14/1987'),
            }),
            checkpoint: new Date(),
        };
        const proc = mocks_1.procedureMockUtils.getInstance(mockContext);
        let err;
        try {
            yield modifyDistributionCheckpoint_1.prepareModifyDistributionCheckpoint.call(proc, args);
        }
        catch (error) {
            err = error;
        }
        expect(err.message).toBe('Cannot modify a Distribution checkpoint after the payment date');
    }));
    test('should throw an error if the payment date is earlier than the Checkpoint date', () => __awaiter(void 0, void 0, void 0, function* () {
        const checkpoint = new Date(new Date().getTime() + 1000);
        const args = {
            distribution: mocks_1.entityMockUtils.getDividendDistributionInstance({
                paymentDate: new Date(checkpoint.getTime() - 100),
            }),
            checkpoint,
        };
        const proc = mocks_1.procedureMockUtils.getInstance(mockContext);
        let err;
        try {
            yield modifyDistributionCheckpoint_1.prepareModifyDistributionCheckpoint.call(proc, args);
        }
        catch (error) {
            err = error;
        }
        expect(err.message).toBe('Payment date must be after the Checkpoint date');
    }));
    test('should throw an error if the checkpoint date is after the expiry date', () => __awaiter(void 0, void 0, void 0, function* () {
        const checkpoint = new Date(new Date().getTime() + 1000);
        const paymentDate = new Date(checkpoint.getTime() + 2000);
        const args = {
            distribution: mocks_1.entityMockUtils.getDividendDistributionInstance({
                paymentDate,
                expiryDate: new Date(checkpoint.getTime() - 1000),
            }),
            checkpoint: mocks_1.entityMockUtils.getCheckpointScheduleInstance({
                details: {
                    nextCheckpointDate: checkpoint,
                },
            }),
        };
        const proc = mocks_1.procedureMockUtils.getInstance(mockContext);
        let err;
        try {
            yield modifyDistributionCheckpoint_1.prepareModifyDistributionCheckpoint.call(proc, args);
        }
        catch (error) {
            err = error;
        }
        expect(err.message).toBe('Expiry date must be after the Checkpoint date');
    }));
    test('should add a modifyCaCheckpoint procedure transaction to the queue', () => __awaiter(void 0, void 0, void 0, function* () {
        const checkpoint = mocks_1.entityMockUtils.getCheckpointInstance();
        const distribution = mocks_1.entityMockUtils.getDividendDistributionInstance({
            paymentDate: new Date(new Date().getTime() + 100000),
            expiryDate: new Date(new Date().getTime() + 200000),
        });
        const args = {
            distribution,
            checkpoint,
        };
        const proc = mocks_1.procedureMockUtils.getInstance(mockContext);
        yield modifyDistributionCheckpoint_1.prepareModifyDistributionCheckpoint.call(proc, args);
        sinon_1.default.assert.calledWith(addProcedureStub, internal_1.modifyCaCheckpoint(), {
            checkpoint,
            corporateAction: distribution,
        });
    }));
    describe('getAuthorization', () => {
        test('should return the appropriate roles and permissions', () => {
            const proc = mocks_1.procedureMockUtils.getInstance(mockContext);
            const boundFunc = modifyDistributionCheckpoint_1.getAuthorization.bind(proc);
            const args = {
                distribution: {
                    asset: { ticker },
                },
            };
            expect(boundFunc(args)).toEqual({
                permissions: {
                    assets: [expect.objectContaining({ ticker })],
                    transactions: [types_1.TxTags.corporateAction.ChangeRecordDate],
                    portfolios: [],
                },
            });
        });
    });
});
//# sourceMappingURL=modifyDistributionCheckpoint.js.map