"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.Permissions = void 0;
const internal_1 = require("../../../internal");
const types_1 = require("../../../types");
const conversion_1 = require("../../../utils/conversion");
const internal_2 = require("../../../utils/internal");
/**
 * Handles all Asset Permissions related functionality
 */
class Permissions extends internal_1.Namespace {
    /**
     * @hidden
     */
    constructor(parent, context) {
        super(parent, context);
        const { ticker } = parent;
        this.createGroup = internal_2.createProcedureMethod({ getProcedureAndArgs: args => [internal_1.createGroup, Object.assign({ ticker }, args)] }, context);
        this.inviteAgent = internal_2.createProcedureMethod({ getProcedureAndArgs: args => [internal_1.inviteExternalAgent, Object.assign({ ticker }, args)] }, context);
        this.removeAgent = internal_2.createProcedureMethod({ getProcedureAndArgs: args => [internal_1.removeExternalAgent, Object.assign({ ticker }, args)] }, context);
    }
    // eslint-disable-next-line require-jsdoc
    getGroup(args) {
        return __awaiter(this, void 0, void 0, function* () {
            const { parent: { ticker }, context, } = this;
            if ('type' in args) {
                return new internal_1.KnownPermissionGroup({ ticker, type: args.type }, context);
            }
            const customGroup = new internal_1.CustomPermissionGroup({ ticker, id: args.id }, context);
            const exists = yield customGroup.exists();
            if (!exists) {
                throw new internal_1.PolymeshError({
                    code: types_1.ErrorCode.DataUnavailable,
                    message: 'The Permission Group does not exist',
                });
            }
            return customGroup;
        });
    }
    /**
     * Retrieve all Permission Groups of this Asset
     */
    getGroups() {
        return __awaiter(this, void 0, void 0, function* () {
            const { context: { polymeshApi: { query: { externalAgents }, }, }, context, parent: { ticker }, } = this;
            const known = Object.values(types_1.PermissionGroupType).map(type => new internal_1.KnownPermissionGroup({ type, ticker }, context));
            const rawCustomPermissionGroups = yield externalAgents.groupPermissions.entries(conversion_1.stringToTicker(ticker, context));
            const custom = rawCustomPermissionGroups.map(([storageKey]) => new internal_1.CustomPermissionGroup({ ticker, id: conversion_1.u32ToBigNumber(storageKey.args[1]) }, context));
            return {
                known,
                custom,
            };
        });
    }
    /**
     * Retrieve a list of agents (Identities which have permissions over the Asset) and
     *   their respective Permission Groups
     */
    getAgents() {
        return __awaiter(this, void 0, void 0, function* () {
            const { context: { polymeshApi: { query: { externalAgents }, }, }, parent: { ticker }, context, } = this;
            const groups = yield externalAgents.groupOfAgent.entries(conversion_1.stringToTicker(ticker, context));
            return groups.map(([storageKey, agentGroup]) => {
                const rawAgentGroup = agentGroup.unwrap();
                return {
                    agent: new internal_1.Identity({ did: conversion_1.identityIdToString(storageKey.args[1]) }, context),
                    group: conversion_1.agentGroupToPermissionGroup(rawAgentGroup, ticker, context),
                };
            });
        });
    }
}
exports.Permissions = Permissions;
//# sourceMappingURL=Permissions.js.map