"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 toggleFreezeOffering_1 = require("../toggleFreezeOffering");
const internal_1 = require("../../../internal");
const mocks_1 = require("../../../testUtils/mocks");
const types_2 = require("../../../types");
const utilsConversionModule = __importStar(require("../../../utils/conversion"));
jest.mock('~/api/entities/Asset', require("../../../testUtils/mocks/entities").mockAssetModule('~/api/entities/Asset'));
jest.mock('~/api/entities/Offering', require("../../../testUtils/mocks/entities").mockOfferingModule('~/api/entities/Offering'));
describe('toggleFreezeOffering procedure', () => {
    let mockContext;
    let stringToTickerStub;
    let numberToU64Stub;
    let ticker;
    let rawTicker;
    let id;
    let rawId;
    let offering;
    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');
        ticker = 'tickerFrozen';
        id = new bignumber_js_1.default(1);
        rawTicker = mocks_1.dsMockUtils.createMockTicker(ticker);
        rawId = mocks_1.dsMockUtils.createMockU64(id.toNumber());
        offering = new internal_1.Offering({ ticker, id }, mockContext);
    });
    let addTransactionStub;
    beforeEach(() => {
        addTransactionStub = mocks_1.procedureMockUtils.getAddTransactionStub();
        mockContext = mocks_1.dsMockUtils.getContextInstance();
        stringToTickerStub.withArgs(ticker, mockContext).returns(rawTicker);
        numberToU64Stub.withArgs(id, mockContext).returns(rawId);
    });
    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 Offering has reached its end date', () => {
        mocks_1.entityMockUtils.configureMocks({
            offeringOptions: {
                details: {
                    status: {
                        timing: types_2.OfferingTimingStatus.Expired,
                        balance: types_2.OfferingBalanceStatus.Available,
                        sale: types_2.OfferingSaleStatus.Closed,
                    },
                },
            },
        });
        const proc = mocks_1.procedureMockUtils.getInstance(mockContext);
        return expect(toggleFreezeOffering_1.prepareToggleFreezeOffering.call(proc, {
            ticker,
            id,
            freeze: true,
        })).rejects.toThrow('The Offering has already ended');
    });
    test('should throw an error if freeze is set to true and the Offering is already frozen', () => {
        mocks_1.entityMockUtils.configureMocks({
            offeringOptions: {
                details: {
                    status: {
                        sale: types_2.OfferingSaleStatus.Frozen,
                        timing: types_2.OfferingTimingStatus.Started,
                        balance: types_2.OfferingBalanceStatus.Available,
                    },
                },
            },
        });
        const proc = mocks_1.procedureMockUtils.getInstance(mockContext);
        return expect(toggleFreezeOffering_1.prepareToggleFreezeOffering.call(proc, {
            ticker,
            id,
            freeze: true,
        })).rejects.toThrow('The Offering is already frozen');
    });
    test('should throw an error if freeze is set to false and the Offering status is live or close', () => __awaiter(void 0, void 0, void 0, function* () {
        const proc = mocks_1.procedureMockUtils.getInstance(mockContext);
        mocks_1.entityMockUtils.configureMocks({
            offeringOptions: {
                details: {
                    status: {
                        sale: types_2.OfferingSaleStatus.Live,
                        timing: types_2.OfferingTimingStatus.Started,
                        balance: types_2.OfferingBalanceStatus.Available,
                    },
                },
            },
        });
        yield expect(toggleFreezeOffering_1.prepareToggleFreezeOffering.call(proc, {
            ticker,
            id,
            freeze: false,
        })).rejects.toThrow('The Offering is already unfrozen');
        mocks_1.entityMockUtils.configureMocks({
            offeringOptions: {
                details: {
                    status: {
                        sale: types_2.OfferingSaleStatus.Closed,
                        timing: types_2.OfferingTimingStatus.Started,
                        balance: types_2.OfferingBalanceStatus.Available,
                    },
                },
            },
        });
        return expect(toggleFreezeOffering_1.prepareToggleFreezeOffering.call(proc, {
            ticker,
            id,
            freeze: false,
        })).rejects.toThrow('The Offering is already closed');
    }));
    test('should add a freeze 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('sto', 'freezeFundraiser');
        const result = yield toggleFreezeOffering_1.prepareToggleFreezeOffering.call(proc, {
            ticker,
            id,
            freeze: true,
        });
        sinon_1.default.assert.calledWith(addTransactionStub, { transaction, args: [rawTicker, rawId] });
        expect(offering.asset.ticker).toBe(result.asset.ticker);
    }));
    test('should add a unfreeze transaction to the queue', () => __awaiter(void 0, void 0, void 0, function* () {
        mocks_1.entityMockUtils.configureMocks({
            offeringOptions: {
                details: {
                    status: {
                        timing: types_2.OfferingTimingStatus.Started,
                        balance: types_2.OfferingBalanceStatus.Available,
                        sale: types_2.OfferingSaleStatus.Frozen,
                    },
                },
            },
        });
        const proc = mocks_1.procedureMockUtils.getInstance(mockContext);
        const transaction = mocks_1.dsMockUtils.createTxStub('sto', 'unfreezeFundraiser');
        const result = yield toggleFreezeOffering_1.prepareToggleFreezeOffering.call(proc, {
            ticker,
            id,
            freeze: false,
        });
        sinon_1.default.assert.calledWith(addTransactionStub, { transaction, args: [rawTicker, rawId] });
        expect(offering.asset.ticker).toBe(result.asset.ticker);
    }));
    describe('getAuthorization', () => {
        test('should return the appropriate roles and permissions', () => {
            const proc = mocks_1.procedureMockUtils.getInstance(mockContext);
            const boundFunc = toggleFreezeOffering_1.getAuthorization.bind(proc);
            const asset = mocks_1.entityMockUtils.getAssetInstance({ ticker });
            expect(boundFunc({ ticker, id, freeze: true })).toEqual({
                permissions: {
                    transactions: [types_1.TxTags.sto.FreezeFundraiser],
                    assets: [asset],
                    portfolios: [],
                },
            });
            expect(boundFunc({ ticker, id, freeze: false })).toEqual({
                permissions: {
                    transactions: [types_1.TxTags.sto.UnfreezeFundraiser],
                    assets: [asset],
                    portfolios: [],
                },
            });
        });
    });
});
//# sourceMappingURL=toggleFreezeOffering.js.map