"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");
/**
 * @hidden
 */
function prepareRenamePortfolio(args) {
    return __awaiter(this, void 0, void 0, function* () {
        const { context: { polymeshApi: { query: { portfolio: queryPortfolio }, tx: { portfolio }, }, }, context, } = this;
        const { did, id, name: newName } = args;
        const identityId = conversion_1.stringToIdentityId(did, context);
        const rawPortfolioNumber = conversion_1.numberToU64(id, context);
        const [rawPortfolioName, rawPortfolios] = yield Promise.all([
            queryPortfolio.portfolios(identityId, rawPortfolioNumber),
            queryPortfolio.portfolios.entries(identityId),
        ]);
        if (conversion_1.textToString(rawPortfolioName) === newName) {
            throw new internal_1.PolymeshError({
                code: types_1.ErrorCode.NoDataChange,
                message: 'New name is the same as current name',
            });
        }
        const portfolioNames = rawPortfolios.map(([, name]) => conversion_1.textToString(name));
        if (portfolioNames.includes(newName)) {
            throw new internal_1.PolymeshError({
                code: types_1.ErrorCode.UnmetPrerequisite,
                message: 'A portfolio with that name already exists',
            });
        }
        this.addTransaction(portfolio.renamePortfolio, {}, rawPortfolioNumber, conversion_1.stringToText(newName, context));
        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)],
            tokens: [],
        },
    };
}
exports.getAuthorization = getAuthorization;
/**
 * @hidden
 */
const renamePortfolio = () => new internal_1.Procedure(prepareRenamePortfolio, getAuthorization);
exports.renamePortfolio = renamePortfolio;
//# sourceMappingURL=renamePortfolio.js.map