"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());
    });
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.CustomPermissionGroup = void 0;
const bignumber_js_1 = __importDefault(require("bignumber.js"));
const internal_1 = require("../../internal");
const conversion_1 = require("../../utils/conversion");
const internal_2 = require("../../utils/internal");
/**
 * Represents a group of custom permissions for a Security Token
 */
class CustomPermissionGroup extends internal_1.PermissionGroup {
    /**
     * @hidden
     */
    constructor(identifiers, context) {
        super(identifiers, context);
        const { id } = identifiers;
        this.id = id;
        this.setPermissions = internal_2.createProcedureMethod({ getProcedureAndArgs: args => [internal_1.setGroupPermissions, Object.assign({ group: this }, args)] }, context);
    }
    /**
     * @hidden
     * Check if a value is of type [[UniqueIdentifiers]]
     */
    static isUniqueIdentifiers(identifier) {
        const { id, ticker } = identifier;
        return id instanceof bignumber_js_1.default && typeof ticker === 'string';
    }
    /**
     * Retrieve the list of permissions and transaction groups associated with this Permission Group
     */
    getPermissions() {
        return __awaiter(this, void 0, void 0, function* () {
            const { context: { polymeshApi: { query: { externalAgents }, }, }, context, token: { ticker }, id, } = this;
            const rawTicker = conversion_1.stringToTicker(ticker, context);
            const rawAgId = conversion_1.numberToU32(id, context);
            const rawGroupPermissions = yield externalAgents.groupPermissions(rawTicker, rawAgId);
            const transactions = conversion_1.extrinsicPermissionsToTransactionPermissions(rawGroupPermissions.unwrap());
            const transactionGroups = conversion_1.transactionPermissionsToTxGroups(transactions);
            return {
                transactions,
                transactionGroups,
            };
        });
    }
    /**
     * Determine whether this Custom Permission Group exists on chain
     */
    exists() {
        return __awaiter(this, void 0, void 0, function* () {
            const { token: { ticker }, id, context, } = this;
            const nextId = yield context.polymeshApi.query.externalAgents.agIdSequence(conversion_1.stringToTicker(ticker, context));
            // 1 < id < next
            return conversion_1.u32ToBigNumber(nextId).gt(id) && id.gte(1);
        });
    }
    /**
     * Return the Group's static data
     */
    toJson() {
        const { id, token } = this;
        return internal_2.toHumanReadable({
            id,
            ticker: token,
        });
    }
}
exports.CustomPermissionGroup = CustomPermissionGroup;
//# sourceMappingURL=CustomPermissionGroup.js.map