"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.waivePermissions = exports.prepareStorage = exports.getAuthorization = exports.prepareWaivePermissions = void 0;
const internal_1 = require("../../internal");
const types_1 = require("../../types");
const conversion_1 = require("../../utils/conversion");
const internal_2 = require("../../utils/internal");
/**
 * @hidden
 */
function prepareWaivePermissions(args) {
    return __awaiter(this, void 0, void 0, function* () {
        const { context: { polymeshApi: { tx }, }, context, storage: { token }, } = this;
        const { identity } = args;
        const agents = yield token.permissions.getAgents();
        const isAgent = agents.some(agentWithGroup => agentWithGroup.agent.did === identity.did);
        if (!isAgent) {
            throw new internal_1.PolymeshError({
                code: types_1.ErrorCode.ValidationError,
                message: 'The Identity is not an Agent for the Security Token',
            });
        }
        const rawTicker = conversion_1.stringToTicker(token.ticker, context);
        this.addTransaction(tx.externalAgents.abdicate, {}, rawTicker);
    });
}
exports.prepareWaivePermissions = prepareWaivePermissions;
/**
 * @hidden
 */
function getAuthorization({ identity: { did } }) {
    const { storage: { token }, } = this;
    return {
        signerPermissions: {
            transactions: [types_1.TxTags.externalAgents.Abdicate],
            tokens: [token],
            portfolios: [],
        },
        roles: [{ type: types_1.RoleType.Identity, did }],
    };
}
exports.getAuthorization = getAuthorization;
/**
 * @hidden
 */
function prepareStorage({ token }) {
    const { context } = this;
    return {
        token: internal_2.getToken(token, context),
    };
}
exports.prepareStorage = prepareStorage;
/**
 * @hidden
 */
const waivePermissions = () => new internal_1.Procedure(prepareWaivePermissions, getAuthorization, prepareStorage);
exports.waivePermissions = waivePermissions;
//# sourceMappingURL=waivePermissions.js.map