"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: { asset }, } = this;
        const { identity } = args;
        const agents = yield asset.permissions.getAgents();
        const isAgent = agents.some(agentWithGroup => agentWithGroup.agent.isEqual(identity));
        if (!isAgent) {
            throw new internal_1.PolymeshError({
                code: types_1.ErrorCode.UnmetPrerequisite,
                message: 'The Identity is not an Agent for the Asset',
            });
        }
        const rawTicker = conversion_1.stringToTicker(asset.ticker, context);
        this.addTransaction({
            transaction: tx.externalAgents.abdicate,
            args: [rawTicker],
        });
    });
}
exports.prepareWaivePermissions = prepareWaivePermissions;
/**
 * @hidden
 */
function getAuthorization({ identity: { did } }) {
    const { storage: { asset }, } = this;
    return {
        signerPermissions: {
            transactions: [types_1.TxTags.externalAgents.Abdicate],
            assets: [asset],
            portfolios: [],
        },
        roles: [{ type: types_1.RoleType.Identity, did }],
    };
}
exports.getAuthorization = getAuthorization;
/**
 * @hidden
 */
function prepareStorage({ asset }) {
    const { context } = this;
    return {
        asset: internal_2.getAsset(asset, context),
    };
}
exports.prepareStorage = prepareStorage;
/**
 * @hidden
 */
const waivePermissions = () => new internal_1.Procedure(prepareWaivePermissions, getAuthorization, prepareStorage);
exports.waivePermissions = waivePermissions;
//# sourceMappingURL=waivePermissions.js.map