"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.toggleFreezeSto = exports.getAuthorization = exports.prepareToggleFreezeSto = void 0;
const internal_1 = require("../../internal");
const types_1 = require("../../types");
const conversion_1 = require("../../utils/conversion");
/**
 * @hidden
 */
function prepareToggleFreezeSto(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 sto = new internal_1.Sto({ ticker, id }, context);
        const { status: { timing, sale }, } = yield sto.details();
        if (timing === types_1.StoTimingStatus.Expired) {
            throw new internal_1.PolymeshError({
                code: types_1.ErrorCode.ValidationError,
                message: 'The STO has already ended',
            });
        }
        if (freeze) {
            if (sale === types_1.StoSaleStatus.Frozen) {
                throw new internal_1.PolymeshError({
                    code: types_1.ErrorCode.ValidationError,
                    message: 'The STO is already frozen',
                });
            }
            this.addTransaction(txSto.freezeFundraiser, {}, rawTicker, rawId);
        }
        else {
            if ([types_1.StoSaleStatus.Closed, types_1.StoSaleStatus.ClosedEarly].includes(sale)) {
                throw new internal_1.PolymeshError({
                    code: types_1.ErrorCode.ValidationError,
                    message: 'The STO is already closed',
                });
            }
            if (sale !== types_1.StoSaleStatus.Frozen) {
                throw new internal_1.PolymeshError({
                    code: types_1.ErrorCode.ValidationError,
                    message: 'The STO is already unfrozen',
                });
            }
            this.addTransaction(txSto.unfreezeFundraiser, {}, rawTicker, rawId);
        }
        return sto;
    });
}
exports.prepareToggleFreezeSto = prepareToggleFreezeSto;
/**
 * @hidden
 */
function getAuthorization({ ticker, freeze }) {
    return {
        permissions: {
            transactions: [freeze ? types_1.TxTags.sto.FreezeFundraiser : types_1.TxTags.sto.UnfreezeFundraiser],
            tokens: [new internal_1.SecurityToken({ ticker }, this.context)],
            portfolios: [],
        },
    };
}
exports.getAuthorization = getAuthorization;
/**
 * @hidden
 */
const toggleFreezeSto = () => new internal_1.Procedure(prepareToggleFreezeSto, getAuthorization);
exports.toggleFreezeSto = toggleFreezeSto;
//# sourceMappingURL=toggleFreezeSto.js.map