"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.renamePortfolio = exports.getAuthorization = exports.prepareRenamePortfolio = void 0;
const internal_1 = require("../../internal");
const types_1 = require("../../types");
const conversion_1 = require("../../utils/conversion");
const internal_2 = require("../../utils/internal");
/**
 * @hidden
 */
function prepareRenamePortfolio(args) {
    return __awaiter(this, void 0, void 0, function* () {
        const { context: { polymeshApi: { tx: { portfolio }, }, }, context, } = this;
        const { did, id, name: newName } = args;
        const identityId = (0, conversion_1.stringToIdentityId)(did, context);
        const rawNewName = (0, conversion_1.stringToText)(newName, context);
        const existingPortfolioNumber = yield (0, internal_2.getPortfolioIdByName)(identityId, rawNewName, context);
        if (existingPortfolioNumber) {
            if (id.eq(existingPortfolioNumber)) {
                throw new internal_1.PolymeshError({
                    code: types_1.ErrorCode.NoDataChange,
                    message: 'New name is the same as current name',
                });
            }
            else {
                throw new internal_1.PolymeshError({
                    code: types_1.ErrorCode.UnmetPrerequisite,
                    message: 'A Portfolio with that name already exists',
                });
            }
        }
        this.addTransaction({
            transaction: portfolio.renamePortfolio,
            args: [(0, conversion_1.bigNumberToU64)(id, context), rawNewName],
        });
        return new internal_1.NumberedPortfolio({ did, id }, context);
    });
}
exports.prepareRenamePortfolio = prepareRenamePortfolio;
/**
 * @hidden
 */
function getAuthorization({ did, id }) {
    const portfolioId = { did, number: id };
    return {
        roles: [{ type: types_1.RoleType.PortfolioCustodian, portfolioId }],
        permissions: {
            transactions: [types_1.TxTags.portfolio.RenamePortfolio],
            portfolios: [new internal_1.NumberedPortfolio({ did, id }, this.context)],
            assets: [],
        },
    };
}
exports.getAuthorization = getAuthorization;
/**
 * @hidden
 */
const renamePortfolio = () => new internal_1.Procedure(prepareRenamePortfolio, getAuthorization);
exports.renamePortfolio = renamePortfolio;
//# sourceMappingURL=renamePortfolio.js.map