"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.modifySignerPermissions = exports.getAuthorization = exports.prepareModifySignerPermissions = void 0;
const utils_1 = require("./utils");
const internal_1 = require("../../internal");
const types_1 = require("../../types");
const utils_2 = require("../../types/utils");
const conversion_1 = require("../../utils/conversion");
/**
 * @hidden
 */
function prepareModifySignerPermissions(args) {
    return __awaiter(this, void 0, void 0, function* () {
        const { context: { polymeshApi: { tx }, }, context, } = this;
        const { secondaryKeys: signers, identity } = args;
        const secondaryKeys = yield identity.getSecondaryKeys();
        const signerValues = signers.map(({ signer, permissions }) => {
            return {
                signer: conversion_1.signerToSignerValue(signer),
                permissions,
            };
        });
        utils_1.assertSecondaryKeys(signerValues.map(({ signer }) => signer), secondaryKeys);
        const signersList = signerValues.map(({ signer, permissions: permissionsLike }) => {
            const permissions = conversion_1.permissionsLikeToPermissions(permissionsLike, context);
            const rawPermissions = conversion_1.permissionsToMeshPermissions(permissions, context);
            return utils_2.tuple(conversion_1.signerValueToSignatory(signer, context), rawPermissions);
        });
        this.addBatchTransaction(tx.identity.setPermissionToSigner, {}, signersList);
    });
}
exports.prepareModifySignerPermissions = prepareModifySignerPermissions;
/**
 * @hidden
 */
function getAuthorization({ identity: { did } }) {
    return {
        roles: [{ type: types_1.RoleType.Identity, did }],
        permissions: {
            transactions: [types_1.TxTags.identity.SetPermissionToSigner],
            tokens: [],
            portfolios: [],
        },
    };
}
exports.getAuthorization = getAuthorization;
/**
 * @hidden
 */
const modifySignerPermissions = () => new internal_1.Procedure(prepareModifySignerPermissions, getAuthorization);
exports.modifySignerPermissions = modifySignerPermissions;
//# sourceMappingURL=modifySignerPermissions.js.map