"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.removeSecondaryKeys = exports.getAuthorization = exports.prepareRemoveSecondaryKeys = void 0;
const lodash_1 = require("lodash");
const utils_1 = require("./utils");
const internal_1 = require("../../internal");
const types_1 = require("../../types");
const conversion_1 = require("../../utils/conversion");
/**
 * @hidden
 */
function prepareRemoveSecondaryKeys(args) {
    return __awaiter(this, void 0, void 0, function* () {
        const { context: { polymeshApi: { tx }, }, context, } = this;
        const { signers, identity } = args;
        const [primaryKey, secondaryKeys] = yield Promise.all([
            identity.getPrimaryKey(),
            identity.getSecondaryKeys(),
        ]);
        const signerValues = signers.map(signer => conversion_1.signerToSignerValue(signer));
        const isPrimaryKeyPresent = lodash_1.find(signerValues, ({ value }) => value === primaryKey.address);
        if (isPrimaryKeyPresent) {
            throw new internal_1.PolymeshError({
                code: types_1.ErrorCode.ValidationError,
                message: 'You cannot remove the primary key',
            });
        }
        utils_1.assertSecondaryKeys(signerValues, secondaryKeys);
        this.addTransaction(tx.identity.removeSecondaryKeys, {}, signerValues.map(signer => conversion_1.signerValueToSignatory(signer, context)));
    });
}
exports.prepareRemoveSecondaryKeys = prepareRemoveSecondaryKeys;
/**
 * @hidden
 */
function getAuthorization({ identity: { did } }) {
    return {
        roles: [{ type: types_1.RoleType.Identity, did }],
        permissions: {
            transactions: [types_1.TxTags.identity.RemoveSecondaryKeys],
            tokens: [],
            portfolios: [],
        },
    };
}
exports.getAuthorization = getAuthorization;
/**
 * @hidden
 */
const removeSecondaryKeys = () => new internal_1.Procedure(prepareRemoveSecondaryKeys, getAuthorization);
exports.removeSecondaryKeys = removeSecondaryKeys;
//# sourceMappingURL=removeSecondaryKeys.js.map