"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 rescheduleInstruction_1 = require("../rescheduleInstruction");
const mocks_1 = require("../../../testUtils/mocks");
const types_1 = require("../../../types");
const utilsConversionModule = __importStar(require("../../../utils/conversion"));
jest.mock('~/api/entities/Instruction', require("../../../testUtils/mocks/entities").mockInstructionModule('~/api/entities/Instruction'));
describe('rescheduleInstruction procedure', () => {
    const id = new bignumber_js_1.default(1);
    const rawId = mocks_1.dsMockUtils.createMockU64(id);
    let mockContext;
    let bigNumberToU64Stub;
    beforeAll(() => {
        mocks_1.dsMockUtils.initMocks();
        mocks_1.procedureMockUtils.initMocks();
        mocks_1.entityMockUtils.initMocks();
        bigNumberToU64Stub = sinon_1.default.stub(utilsConversionModule, 'bigNumberToU64');
    });
    beforeEach(() => {
        mockContext = mocks_1.dsMockUtils.getContextInstance();
        bigNumberToU64Stub.withArgs(id, mockContext).returns(rawId);
        mocks_1.entityMockUtils.configureMocks({
            instructionOptions: {
                details: {
                    status: types_1.InstructionStatus.Failed,
                },
            },
        });
    });
    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 instruction is not Failed', () => {
        mocks_1.entityMockUtils.configureMocks({
            instructionOptions: {
                details: {
                    status: types_1.InstructionStatus.Pending,
                },
            },
        });
        const proc = mocks_1.procedureMockUtils.getInstance(mockContext);
        return expect(rescheduleInstruction_1.prepareRescheduleInstruction.call(proc, {
            id,
        })).rejects.toThrow('Only failed Instructions can be rescheduled');
    });
    test('should add a reschedule Instruction transaction to the queue', () => __awaiter(void 0, void 0, void 0, function* () {
        const proc = mocks_1.procedureMockUtils.getInstance(mockContext);
        const transaction = mocks_1.dsMockUtils.createTxStub('settlement', 'rescheduleInstruction');
        yield rescheduleInstruction_1.prepareRescheduleInstruction.call(proc, {
            id,
        });
        const addTransactionStub = mocks_1.procedureMockUtils.getAddTransactionStub();
        sinon_1.default.assert.calledWith(addTransactionStub, { transaction, args: [rawId] });
    }));
});
//# sourceMappingURL=rescheduleInstruction.js.map