"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 types_1 = require("../../../polkadot/types");
const sinon_1 = __importDefault(require("sinon"));
const modifyCaCheckpoint_1 = require("../modifyCaCheckpoint");
const mocks_1 = require("../../../testUtils/mocks");
const utilsConversionModule = __importStar(require("../../../utils/conversion"));
jest.mock('~/api/entities/SecurityToken', require("../../../testUtils/mocks/entities").mockSecurityTokenModule('~/api/entities/SecurityToken'));
describe('modifyCaCheckpoint procedure', () => {
    const ticker = 'SOMETICKER';
    let mockContext;
    let addTransactionStub;
    let changeRecordDateTransaction;
    beforeAll(() => {
        mocks_1.dsMockUtils.initMocks();
        mocks_1.procedureMockUtils.initMocks();
        mocks_1.entityMockUtils.initMocks();
    });
    beforeEach(() => {
        addTransactionStub = mocks_1.procedureMockUtils.getAddTransactionStub();
        changeRecordDateTransaction = mocks_1.dsMockUtils.createTxStub('corporateAction', 'changeRecordDate');
        mockContext = mocks_1.dsMockUtils.getContextInstance();
    });
    afterEach(() => {
        mocks_1.entityMockUtils.reset();
        mocks_1.procedureMockUtils.reset();
        mocks_1.dsMockUtils.reset();
    });
    afterAll(() => {
        mocks_1.entityMockUtils.cleanup();
        mocks_1.procedureMockUtils.cleanup();
        mocks_1.dsMockUtils.cleanup();
    });
    test('should throw an error if the checkpoint does not exist', () => __awaiter(void 0, void 0, void 0, function* () {
        const args = {
            corporateAction: mocks_1.entityMockUtils.getCorporateActionInstance(),
            checkpoint: mocks_1.entityMockUtils.getCheckpointInstance({
                exists: false,
            }),
        };
        const proc = mocks_1.procedureMockUtils.getInstance(mockContext);
        let err;
        try {
            yield modifyCaCheckpoint_1.prepareModifyCaCheckpoint.call(proc, args);
        }
        catch (error) {
            err = error;
        }
        expect(err.message).toBe("Checkpoint doesn't exist");
    }));
    test('should throw an error if checkpoint schedule no longer exists', () => __awaiter(void 0, void 0, void 0, function* () {
        const args = {
            corporateAction: mocks_1.entityMockUtils.getCorporateActionInstance(),
            checkpoint: mocks_1.entityMockUtils.getCheckpointScheduleInstance({
                exists: false,
            }),
        };
        const proc = mocks_1.procedureMockUtils.getInstance(mockContext);
        let err;
        try {
            yield modifyCaCheckpoint_1.prepareModifyCaCheckpoint.call(proc, args);
        }
        catch (error) {
            err = error;
        }
        expect(err.message).toBe("Checkpoint Schedule doesn't exist");
    }));
    test('should throw an error if date is in the past', () => __awaiter(void 0, void 0, void 0, function* () {
        const args = {
            corporateAction: mocks_1.entityMockUtils.getCorporateActionInstance(),
            checkpoint: new Date(new Date().getTime() - 100000),
        };
        const proc = mocks_1.procedureMockUtils.getInstance(mockContext);
        let err;
        try {
            yield modifyCaCheckpoint_1.prepareModifyCaCheckpoint.call(proc, args);
        }
        catch (error) {
            err = error;
        }
        expect(err.message).toBe('Checkpoint date must be in the future');
    }));
    test('should add a change record date transaction to the queue', () => __awaiter(void 0, void 0, void 0, function* () {
        const proc = mocks_1.procedureMockUtils.getInstance(mockContext);
        const id = new bignumber_js_1.default(1);
        // eslint-disable-next-line @typescript-eslint/naming-convention
        const rawCaId = mocks_1.dsMockUtils.createMockCAId({ ticker, local_id: id.toNumber() });
        sinon_1.default.stub(utilsConversionModule, 'corporateActionIdentifierToCaId').returns(rawCaId);
        const rawRecordDateSpec = mocks_1.dsMockUtils.createMockRecordDateSpec({
            Scheduled: mocks_1.dsMockUtils.createMockMoment(new Date().getTime()),
        });
        sinon_1.default.stub(utilsConversionModule, 'checkpointToRecordDateSpec').returns(rawRecordDateSpec);
        yield modifyCaCheckpoint_1.prepareModifyCaCheckpoint.call(proc, {
            corporateAction: mocks_1.entityMockUtils.getCorporateActionInstance({
                id,
            }),
            checkpoint: mocks_1.entityMockUtils.getCheckpointInstance({
                exists: true,
            }),
        });
        sinon_1.default.assert.calledWith(addTransactionStub, changeRecordDateTransaction, {}, rawCaId, rawRecordDateSpec);
        yield modifyCaCheckpoint_1.prepareModifyCaCheckpoint.call(proc, {
            corporateAction: mocks_1.entityMockUtils.getCorporateActionInstance({
                id,
            }),
            checkpoint: mocks_1.entityMockUtils.getCheckpointScheduleInstance({
                exists: true,
            }),
        });
        sinon_1.default.assert.calledWith(addTransactionStub, changeRecordDateTransaction, {}, rawCaId, rawRecordDateSpec);
        yield modifyCaCheckpoint_1.prepareModifyCaCheckpoint.call(proc, {
            corporateAction: mocks_1.entityMockUtils.getCorporateActionInstance({
                id,
            }),
            checkpoint: new Date(new Date().getTime() + 100000),
        });
        sinon_1.default.assert.calledWith(addTransactionStub, changeRecordDateTransaction, {}, rawCaId, rawRecordDateSpec);
    }));
    describe('getAuthorization', () => {
        test('should return the appropriate roles and permissions', () => {
            const proc = mocks_1.procedureMockUtils.getInstance(mockContext);
            const boundFunc = modifyCaCheckpoint_1.getAuthorization.bind(proc);
            const args = {
                corporateAction: {
                    ticker,
                },
            };
            expect(boundFunc(args)).toEqual({
                permissions: {
                    tokens: [mocks_1.entityMockUtils.getSecurityTokenInstance({ ticker })],
                    transactions: [types_1.TxTags.corporateAction.ChangeRecordDate],
                    portfolios: [],
                },
            });
        });
    });
});
//# sourceMappingURL=modifyCaCheckpoint.js.map