"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.modifyAllowance = exports.getAuthorization = exports.prepareModifyAllowance = void 0;
const internal_1 = require("../../internal");
const types_1 = require("../../types");
const internal_2 = require("../../types/internal");
const conversion_1 = require("../../utils/conversion");
/**
 * @hidden
 */
function prepareModifyAllowance(args) {
    return __awaiter(this, void 0, void 0, function* () {
        const { context: { polymeshApi: { tx }, }, context, } = this;
        const { subsidy: { beneficiary: { address: beneficiaryAddress }, }, subsidy, allowance, operation, } = args;
        const [exists, currentAllowance] = yield Promise.all([subsidy.exists(), subsidy.getAllowance()]);
        if (!exists) {
            throw new internal_1.PolymeshError({
                code: types_1.ErrorCode.DataUnavailable,
                message: 'The Subsidy no longer exists',
            });
        }
        const rawAllowance = conversion_1.bigNumberToBalance(allowance, context);
        const rawBeneficiaryAccount = conversion_1.stringToAccountId(beneficiaryAddress, context);
        let transaction = tx.relayer.increasePolyxLimit;
        if (operation === internal_2.AllowanceOperation.Set) {
            if (currentAllowance.eq(allowance)) {
                throw new internal_1.PolymeshError({
                    code: types_1.ErrorCode.NoDataChange,
                    message: 'Amount of allowance to set is equal to the current allowance',
                });
            }
            transaction = tx.relayer.updatePolyxLimit;
        }
        if (operation === internal_2.AllowanceOperation.Decrease) {
            if (currentAllowance.lte(allowance)) {
                throw new internal_1.PolymeshError({
                    code: types_1.ErrorCode.ValidationError,
                    message: 'Amount of allowance to decrease cannot be more than the current allowance',
                });
            }
            transaction = tx.relayer.decreasePolyxLimit;
        }
        this.addTransaction({
            transaction,
            args: [rawBeneficiaryAccount, rawAllowance],
        });
    });
}
exports.prepareModifyAllowance = prepareModifyAllowance;
/**
 * @hidden
 *
 * To modify the allowance for a Subsidy, the caller must be the subsidizer
 */
function getAuthorization(args) {
    const { context } = this;
    const { subsidy: { subsidizer }, operation, } = args;
    const currentAccount = context.getSigningAccount();
    const hasRoles = subsidizer.isEqual(currentAccount);
    const transactionMap = {
        [internal_2.AllowanceOperation.Increase]: types_1.TxTags.relayer.IncreasePolyxLimit,
        [internal_2.AllowanceOperation.Decrease]: types_1.TxTags.relayer.DecreasePolyxLimit,
        [internal_2.AllowanceOperation.Set]: types_1.TxTags.relayer.UpdatePolyxLimit,
    };
    return {
        roles: hasRoles || 'Only the subsidizer is allowed to modify the allowance of a Subsidy',
        permissions: {
            transactions: [transactionMap[operation]],
        },
    };
}
exports.getAuthorization = getAuthorization;
/**
 * @hidden
 */
const modifyAllowance = () => new internal_1.Procedure(prepareModifyAllowance, getAuthorization);
exports.modifyAllowance = modifyAllowance;
//# sourceMappingURL=modifyAllowance.js.map