"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.createGroup = exports.prepareStorage = exports.getAuthorization = exports.prepareCreateGroup = exports.createCreateGroupResolver = 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
 */
const createCreateGroupResolver = (context) => (receipt) => {
    const [{ data }] = (0, internal_2.filterEventRecords)(receipt, 'externalAgents', 'GroupCreated');
    return new internal_1.CustomPermissionGroup({ id: (0, conversion_1.u32ToBigNumber)(data[2]), ticker: (0, conversion_1.tickerToString)(data[1]) }, context);
};
exports.createCreateGroupResolver = createCreateGroupResolver;
/**
 * @hidden
 */
function prepareCreateGroup(args) {
    return __awaiter(this, void 0, void 0, function* () {
        const { context: { polymeshApi: { tx: { externalAgents }, }, }, context, storage: { asset }, } = this;
        const { ticker, permissions } = args;
        const rawTicker = (0, conversion_1.stringToTicker)(ticker, context);
        const { transactions } = (0, conversion_1.permissionsLikeToPermissions)(permissions, context);
        yield (0, utils_1.assertGroupDoesNotExist)(asset, transactions);
        const rawExtrinsicPermissions = (0, conversion_1.transactionPermissionsToExtrinsicPermissions)(transactions, context);
        const [customPermissionGroup] = this.addTransaction({
            transaction: externalAgents.createGroup,
            resolvers: [(0, exports.createCreateGroupResolver)(context)],
            args: [rawTicker, rawExtrinsicPermissions],
        });
        return customPermissionGroup;
    });
}
exports.prepareCreateGroup = prepareCreateGroup;
/**
 * @hidden
 */
function getAuthorization() {
    const { storage: { asset }, } = this;
    return {
        permissions: {
            transactions: [types_1.TxTags.externalAgents.CreateGroup],
            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 createGroup = () => new internal_1.Procedure(prepareCreateGroup, getAuthorization, prepareStorage);
exports.createGroup = createGroup;
//# sourceMappingURL=createGroup.js.map