"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.prepareStorage = 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, storage: { identity }, } = this;
        const { secondaryAccounts } = args;
        const existingSecondaryAccounts = yield identity.getSecondaryAccounts();
        (0, utils_1.assertSecondaryAccounts)(secondaryAccounts.map(({ account }) => account), existingSecondaryAccounts);
        const signersList = secondaryAccounts.map(({ account, permissions: permissionsLike }) => {
            const permissions = (0, conversion_1.permissionsLikeToPermissions)(permissionsLike, context);
            const rawPermissions = (0, conversion_1.permissionsToMeshPermissions)(permissions, context);
            return (0, utils_2.tuple)((0, conversion_1.signerValueToSignatory)((0, conversion_1.signerToSignerValue)(account), context), rawPermissions);
        });
        const transaction = tx.identity.setPermissionToSigner;
        this.addBatchTransaction({
            transactions: signersList.map(params => ({
                transaction,
                args: params,
            })),
        });
    });
}
exports.prepareModifySignerPermissions = prepareModifySignerPermissions;
/**
 * @hidden
 */
function getAuthorization() {
    return __awaiter(this, void 0, void 0, function* () {
        const { context, storage: { identity }, } = this;
        const signingAccount = context.getSigningAccount();
        const { account: primaryAccount } = yield identity.getPrimaryAccount();
        if (!signingAccount.isEqual(primaryAccount)) {
            return {
                signerPermissions: "Secondary Account permissions can only be modified by the Identity's primary Account",
            };
        }
        return {
            permissions: {
                transactions: [types_1.TxTags.identity.SetPermissionToSigner],
                assets: [],
                portfolios: [],
            },
        };
    });
}
exports.getAuthorization = getAuthorization;
/**
 * @hidden
 */
function prepareStorage() {
    return __awaiter(this, void 0, void 0, function* () {
        const { context } = this;
        return {
            identity: yield context.getSigningIdentity(),
        };
    });
}
exports.prepareStorage = prepareStorage;
/**
 * @hidden
 */
const modifySignerPermissions = () => new internal_1.Procedure(prepareModifySignerPermissions, getAuthorization, prepareStorage);
exports.modifySignerPermissions = modifySignerPermissions;
//# sourceMappingURL=modifySignerPermissions.js.map