"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.removeExternalAgent = exports.prepareStorage = exports.getAuthorization = exports.prepareRemoveExternalAgent = void 0;
const utils_1 = require("./utils");
const internal_1 = require("../../internal");
const types_1 = require("../../types");
const conversion_1 = require("../../utils/conversion");
const internal_2 = require("../../utils/internal");
/**
 * @hidden
 */
function prepareRemoveExternalAgent(args) {
    return __awaiter(this, void 0, void 0, function* () {
        const { context: { polymeshApi: { tx: { externalAgents }, }, }, context, storage: { asset }, } = this;
        const { ticker, target } = args;
        const [currentAgents, targetIdentity] = yield Promise.all([
            asset.permissions.getAgents(),
            internal_2.getIdentity(target, context),
        ]);
        const agentWithGroup = currentAgents.find(({ agent }) => agent.isEqual(targetIdentity));
        if (!agentWithGroup) {
            throw new internal_1.PolymeshError({
                code: types_1.ErrorCode.UnmetPrerequisite,
                message: 'The target Identity is not an External Agent',
            });
        }
        if (utils_1.isFullGroupType(agentWithGroup.group)) {
            const fullGroupAgents = currentAgents.filter(({ group }) => utils_1.isFullGroupType(group));
            if (fullGroupAgents.length === 1) {
                throw new internal_1.PolymeshError({
                    code: types_1.ErrorCode.EntityInUse,
                    message: 'The target is the last Agent with full permissions for this Asset. There should always be at least one Agent with full permissions',
                });
            }
        }
        const rawTicker = conversion_1.stringToTicker(ticker, context);
        const rawAgent = conversion_1.stringToIdentityId(targetIdentity.did, context);
        this.addTransaction({
            transaction: externalAgents.removeAgent,
            args: [rawTicker, rawAgent],
        });
    });
}
exports.prepareRemoveExternalAgent = prepareRemoveExternalAgent;
/**
 * @hidden
 */
function getAuthorization() {
    const { storage: { asset }, } = this;
    return {
        permissions: {
            transactions: [types_1.TxTags.externalAgents.RemoveAgent],
            assets: [asset],
            portfolios: [],
        },
    };
}
exports.getAuthorization = getAuthorization;
/**
 * @hidden
 */
function prepareStorage({ ticker }) {
    const { context } = this;
    return {
        asset: new internal_1.Asset({ ticker }, context),
    };
}
exports.prepareStorage = prepareStorage;
/**
 * @hidden
 */
const removeExternalAgent = () => new internal_1.Procedure(prepareRemoveExternalAgent, getAuthorization, prepareStorage);
exports.removeExternalAgent = removeExternalAgent;
//# sourceMappingURL=removeExternalAgent.js.map