"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 removeCheckpointSchedule_1 = require("../removeCheckpointSchedule");
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('removeCheckpointSchedule procedure', () => {
    let mockContext;
    let stringToTickerStub;
    let numberToU64Stub;
    let u32ToBigNumberStub;
    let ticker;
    let rawTicker;
    let addTransactionStub;
    let id;
    let rawId;
    beforeAll(() => {
        mocks_1.dsMockUtils.initMocks();
        mocks_1.procedureMockUtils.initMocks();
        mocks_1.entityMockUtils.initMocks();
        stringToTickerStub = sinon_1.default.stub(utilsConversionModule, 'stringToTicker');
        numberToU64Stub = sinon_1.default.stub(utilsConversionModule, 'numberToU64');
        u32ToBigNumberStub = sinon_1.default.stub(utilsConversionModule, 'u32ToBigNumber');
        ticker = 'someTicker';
        rawTicker = mocks_1.dsMockUtils.createMockTicker(ticker);
        id = new bignumber_js_1.default(1);
        rawId = mocks_1.dsMockUtils.createMockU64(id.toNumber());
    });
    beforeEach(() => {
        mockContext = mocks_1.dsMockUtils.getContextInstance();
        stringToTickerStub.returns(rawTicker);
        numberToU64Stub.returns(rawId);
        addTransactionStub = mocks_1.procedureMockUtils.getAddTransactionStub();
        mocks_1.dsMockUtils.createQueryStub('checkpoint', 'scheduleRefCount');
    });
    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 Schedule no longer exists', () => {
        const args = {
            ticker,
            schedule: id,
        };
        mocks_1.dsMockUtils.createQueryStub('checkpoint', 'schedules', {
            returnValue: [
                mocks_1.dsMockUtils.createMockStoredSchedule({
                    id: mocks_1.dsMockUtils.createMockU64(5),
                }),
            ],
        });
        const proc = mocks_1.procedureMockUtils.getInstance(mockContext);
        return expect(removeCheckpointSchedule_1.prepareRemoveCheckpointSchedule.call(proc, args)).rejects.toThrow('Schedule no longer exists. It was either removed or it expired');
    });
    test('should throw an error if Schedule Ref Count is not zero', () => {
        const args = {
            ticker,
            schedule: id,
        };
        mocks_1.dsMockUtils.createQueryStub('checkpoint', 'schedules', {
            returnValue: [
                mocks_1.dsMockUtils.createMockStoredSchedule({
                    id: mocks_1.dsMockUtils.createMockU64(id.toNumber()),
                }),
            ],
        });
        u32ToBigNumberStub.returns(new bignumber_js_1.default(1));
        const proc = mocks_1.procedureMockUtils.getInstance(mockContext);
        return expect(removeCheckpointSchedule_1.prepareRemoveCheckpointSchedule.call(proc, args)).rejects.toThrow('This Schedule is being referenced by other Entities. It cannot be removed');
    });
    test('should add a remove schedule transaction to the queue', () => __awaiter(void 0, void 0, void 0, function* () {
        const args = {
            ticker,
            schedule: id,
        };
        mocks_1.dsMockUtils.createQueryStub('checkpoint', 'schedules', {
            returnValue: [
                mocks_1.dsMockUtils.createMockStoredSchedule({
                    id: mocks_1.dsMockUtils.createMockU64(id.toNumber()),
                }),
            ],
        });
        u32ToBigNumberStub.returns(new bignumber_js_1.default(0));
        let transaction = mocks_1.dsMockUtils.createTxStub('checkpoint', 'removeSchedule');
        let proc = mocks_1.procedureMockUtils.getInstance(mockContext);
        yield removeCheckpointSchedule_1.prepareRemoveCheckpointSchedule.call(proc, args);
        sinon_1.default.assert.calledWith(addTransactionStub, transaction, {}, rawTicker);
        transaction = mocks_1.dsMockUtils.createTxStub('checkpoint', 'removeSchedule');
        proc = mocks_1.procedureMockUtils.getInstance(mockContext);
        yield removeCheckpointSchedule_1.prepareRemoveCheckpointSchedule.call(proc, {
            ticker,
            schedule: mocks_1.entityMockUtils.getCheckpointScheduleInstance({
                id: new bignumber_js_1.default(1),
            }),
        });
        sinon_1.default.assert.calledWith(addTransactionStub, transaction, {}, rawTicker);
    }));
    describe('getAuthorization', () => {
        test('should return the appropriate roles and permissions', () => {
            const proc = mocks_1.procedureMockUtils.getInstance(mockContext);
            const boundFunc = removeCheckpointSchedule_1.getAuthorization.bind(proc);
            const args = {
                ticker,
            };
            expect(boundFunc(args)).toEqual({
                permissions: {
                    transactions: [types_1.TxTags.checkpoint.RemoveSchedule],
                    tokens: [mocks_1.entityMockUtils.getSecurityTokenInstance({ ticker })],
                    portfolios: [],
                },
            });
        });
    });
});
//# sourceMappingURL=removeCheckpointSchedule.js.map