"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());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.toggleFreezeOffering = exports.getAuthorization = exports.prepareToggleFreezeOffering = void 0;
const internal_1 = require("../../internal");
const types_1 = require("../../types");
const conversion_1 = require("../../utils/conversion");
/**
 * @hidden
 */
function prepareToggleFreezeOffering(args) {
    return __awaiter(this, void 0, void 0, function* () {
        const { context: { polymeshApi: { tx: { sto: txSto }, }, }, context, } = this;
        const { ticker, id, freeze } = args;
        const rawTicker = conversion_1.stringToTicker(ticker, context);
        const rawId = conversion_1.numberToU64(id, context);
        const offering = new internal_1.Offering({ ticker, id }, context);
        const { status: { timing, sale }, } = yield offering.details();
        if (timing === types_1.OfferingTimingStatus.Expired) {
            throw new internal_1.PolymeshError({
                code: types_1.ErrorCode.UnmetPrerequisite,
                message: 'The Offering has already ended',
            });
        }
        if (freeze) {
            if (sale === types_1.OfferingSaleStatus.Frozen) {
                throw new internal_1.PolymeshError({
                    code: types_1.ErrorCode.NoDataChange,
                    message: 'The Offering is already frozen',
                });
            }
            this.addTransaction({
                transaction: txSto.freezeFundraiser,
                args: [rawTicker, rawId],
            });
        }
        else {
            if ([types_1.OfferingSaleStatus.Closed, types_1.OfferingSaleStatus.ClosedEarly].includes(sale)) {
                throw new internal_1.PolymeshError({
                    code: types_1.ErrorCode.UnmetPrerequisite,
                    message: 'The Offering is already closed',
                });
            }
            if (sale !== types_1.OfferingSaleStatus.Frozen) {
                throw new internal_1.PolymeshError({
                    code: types_1.ErrorCode.NoDataChange,
                    message: 'The Offering is already unfrozen',
                });
            }
            this.addTransaction({
                transaction: txSto.unfreezeFundraiser,
                args: [rawTicker, rawId],
            });
        }
        return offering;
    });
}
exports.prepareToggleFreezeOffering = prepareToggleFreezeOffering;
/**
 * @hidden
 */
function getAuthorization({ ticker, freeze }) {
    return {
        permissions: {
            transactions: [freeze ? types_1.TxTags.sto.FreezeFundraiser : types_1.TxTags.sto.UnfreezeFundraiser],
            assets: [new internal_1.Asset({ ticker }, this.context)],
            portfolios: [],
        },
    };
}
exports.getAuthorization = getAuthorization;
/**
 * @hidden
 */
const toggleFreezeOffering = () => new internal_1.Procedure(prepareToggleFreezeOffering, getAuthorization);
exports.toggleFreezeOffering = toggleFreezeOffering;
//# sourceMappingURL=toggleFreezeOffering.js.map