"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.setGroupPermissions = exports.getAuthorization = exports.prepareSetGroupPermissions = void 0;
const lodash_1 = require("lodash");
const internal_1 = require("../../internal");
const types_1 = require("../../types");
const conversion_1 = require("../../utils/conversion");
/**
 * @hidden
 */
function prepareSetGroupPermissions(args) {
    return __awaiter(this, void 0, void 0, function* () {
        const { context: { polymeshApi: { tx: { externalAgents }, }, }, context, } = this;
        const { group, permissions } = args;
        const { transactions } = conversion_1.permissionsLikeToPermissions(permissions, context);
        const { transactions: transactionPermissions } = yield group.getPermissions();
        if (lodash_1.isEqual(transactionPermissions, transactions)) {
            throw new internal_1.PolymeshError({
                code: types_1.ErrorCode.NoDataChange,
                message: 'New permissions are the same as the current ones',
            });
        }
        const { asset: { ticker }, id, } = group;
        const rawTicker = conversion_1.stringToTicker(ticker, context);
        const rawAgId = conversion_1.bigNumberToU32(id, context);
        const rawExtrinsicPermissions = conversion_1.transactionPermissionsToExtrinsicPermissions(transactions, context);
        this.addTransaction({
            transaction: externalAgents.setGroupPermissions,
            args: [rawTicker, rawAgId, rawExtrinsicPermissions],
        });
    });
}
exports.prepareSetGroupPermissions = prepareSetGroupPermissions;
/**
 * @hidden
 */
function getAuthorization({ group: { asset } }) {
    return {
        permissions: {
            transactions: [types_1.TxTags.externalAgents.SetGroupPermissions],
            portfolios: [],
            assets: [asset],
        },
    };
}
exports.getAuthorization = getAuthorization;
/**
 * @hidden
 */
const setGroupPermissions = () => new internal_1.Procedure(prepareSetGroupPermissions, getAuthorization);
exports.setGroupPermissions = setGroupPermissions;
//# sourceMappingURL=setGroupPermissions.js.map