"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());
    });
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.removeCorporateAction = exports.getAuthorization = exports.prepareRemoveCorporateAction = void 0;
const bignumber_js_1 = __importDefault(require("bignumber.js"));
const internal_1 = require("../../internal");
const types_1 = require("../../types");
const conversion_1 = require("../../utils/conversion");
const caNotExistsMessage = "The Corporate Action doesn't exist";
/**
 * @hidden
 */
const assertCaIsRemovable = (rawCaId, query, ticker, context, corporateAction) => __awaiter(void 0, void 0, void 0, function* () {
    const distribution = yield query.capitalDistribution.distributions(rawCaId);
    const exists = distribution.isSome;
    if (!exists && !(corporateAction instanceof bignumber_js_1.default)) {
        throw new internal_1.PolymeshError({
            code: types_1.ErrorCode.DataUnavailable,
            message: "The Distribution doesn't exist",
        });
    }
    if (corporateAction instanceof bignumber_js_1.default) {
        const CA = yield query.corporateAction.corporateActions((0, conversion_1.stringToTicker)(ticker, context), (0, conversion_1.bigNumberToU32)(corporateAction, context));
        if (CA.isEmpty) {
            throw new internal_1.PolymeshError({
                code: types_1.ErrorCode.DataUnavailable,
                message: caNotExistsMessage,
            });
        }
    }
    else {
        const { paymentAt: rawPaymentAt } = distribution.unwrap();
        if ((0, conversion_1.momentToDate)(rawPaymentAt) < new Date()) {
            throw new internal_1.PolymeshError({
                code: types_1.ErrorCode.UnmetPrerequisite,
                message: 'The Distribution has already started',
            });
        }
    }
});
/**
 * @hidden
 */
function prepareRemoveCorporateAction(args) {
    return __awaiter(this, void 0, void 0, function* () {
        const { context, context: { polymeshApi: { tx, query }, }, } = this;
        const { ticker, corporateAction } = args;
        const localId = corporateAction instanceof internal_1.CorporateActionBase ? corporateAction.id : corporateAction;
        const rawCaId = (0, conversion_1.corporateActionIdentifierToCaId)({ ticker, localId }, context);
        if (corporateAction instanceof internal_1.DividendDistribution || corporateAction instanceof bignumber_js_1.default) {
            yield assertCaIsRemovable(rawCaId, query, ticker, context, corporateAction);
        }
        else {
            const exists = yield corporateAction.exists();
            if (!exists) {
                throw new internal_1.PolymeshError({
                    code: types_1.ErrorCode.DataUnavailable,
                    message: caNotExistsMessage,
                });
            }
        }
        this.addTransaction({
            transaction: tx.corporateAction.removeCa,
            args: [rawCaId],
        });
    });
}
exports.prepareRemoveCorporateAction = prepareRemoveCorporateAction;
/**
 * @hidden
 */
function getAuthorization({ ticker }) {
    return {
        permissions: {
            transactions: [types_1.TxTags.corporateAction.RemoveCa],
            assets: [new internal_1.Asset({ ticker }, this.context)],
            portfolios: [],
        },
    };
}
exports.getAuthorization = getAuthorization;
/**
 * @hidden
 */
const removeCorporateAction = () => new internal_1.Procedure(prepareRemoveCorporateAction, getAuthorization);
exports.removeCorporateAction = removeCorporateAction;
//# sourceMappingURL=removeCorporateAction.js.map