"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.subsidizeAccount = exports.prepareSubsidizeAccount = void 0;
const utils_1 = require("./utils");
const internal_1 = require("../../internal");
const types_1 = require("../../types");
const conversion_1 = require("../../utils/conversion");
/**
 * @hidden
 */
function prepareSubsidizeAccount(args) {
    return __awaiter(this, void 0, void 0, function* () {
        const { context: { polymeshApi: { tx }, }, context, } = this;
        const { beneficiary, allowance } = args;
        let account;
        if (beneficiary instanceof internal_1.Account) {
            account = beneficiary;
        }
        else {
            account = new internal_1.Account({ address: beneficiary }, context);
        }
        const { address: beneficiaryAddress } = account;
        const identity = yield context.getSigningIdentity();
        const authorizationRequests = yield identity.authorizations.getSent();
        const hasPendingAuth = !!authorizationRequests.data.find(authorizationRequest => {
            const { target, data } = authorizationRequest;
            return (conversion_1.signerToString(target) === beneficiaryAddress &&
                !authorizationRequest.isExpired() &&
                data.type === types_1.AuthorizationType.AddRelayerPayingKey &&
                data.value.allowance.isEqualTo(allowance));
        });
        if (hasPendingAuth) {
            throw new internal_1.PolymeshError({
                code: types_1.ErrorCode.NoDataChange,
                message: 'The Beneficiary Account already has a pending invitation to add this account as a subsidizer with the same allowance',
            });
        }
        const rawBeneficiary = conversion_1.stringToAccountId(beneficiaryAddress, context);
        const rawAllowance = conversion_1.bigNumberToBalance(allowance, context);
        const authRequest = {
            type: types_1.AuthorizationType.AddRelayerPayingKey,
            value: {
                beneficiary: account,
                subsidizer: context.getSigningAccount(),
                allowance,
            },
        };
        const [auth] = this.addTransaction({
            transaction: tx.relayer.setPayingKey,
            resolvers: [utils_1.createAuthorizationResolver(authRequest, identity, account, null, context)],
            args: [rawBeneficiary, rawAllowance],
        });
        return auth;
    });
}
exports.prepareSubsidizeAccount = prepareSubsidizeAccount;
/**
 * @hidden
 */
const subsidizeAccount = () => new internal_1.Procedure(prepareSubsidizeAccount, {
    permissions: {
        transactions: [types_1.TxTags.relayer.SetPayingKey],
        assets: [],
        portfolios: [],
    },
});
exports.subsidizeAccount = subsidizeAccount;
//# sourceMappingURL=subsidizeAccount.js.map