"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.modifyToken = exports.getAuthorization = exports.prepareModifyToken = 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 prepareModifyToken(args) {
    return __awaiter(this, void 0, void 0, function* () {
        const { context: { polymeshApi: { tx }, }, context, } = this;
        const { ticker, makeDivisible, name: newName, fundingRound: newFundingRound, identifiers: newIdentifiers, } = args;
        const noArguments = makeDivisible === undefined &&
            newName === undefined &&
            newFundingRound === undefined &&
            newIdentifiers === undefined;
        if (noArguments) {
            throw new internal_1.PolymeshError({
                code: types_1.ErrorCode.NoDataChange,
                message: 'Nothing to modify',
            });
        }
        const rawTicker = conversion_1.stringToTicker(ticker, context);
        const securityToken = new internal_1.SecurityToken({ ticker }, context);
        const [{ isDivisible, name }, fundingRound, identifiers] = yield Promise.all([
            securityToken.details(),
            securityToken.currentFundingRound(),
            securityToken.getIdentifiers(),
        ]);
        if (makeDivisible) {
            if (isDivisible) {
                throw new internal_1.PolymeshError({
                    code: types_1.ErrorCode.NoDataChange,
                    message: 'The Security Token is already divisible',
                });
            }
            this.addTransaction(tx.asset.makeDivisible, {}, rawTicker);
        }
        if (newName) {
            if (newName === name) {
                throw new internal_1.PolymeshError({
                    code: types_1.ErrorCode.NoDataChange,
                    message: 'New name is the same as current name',
                });
            }
            this.addTransaction(tx.asset.renameAsset, {}, rawTicker, conversion_1.stringToAssetName(newName, context));
        }
        if (newFundingRound) {
            if (newFundingRound === fundingRound) {
                throw new internal_1.PolymeshError({
                    code: types_1.ErrorCode.NoDataChange,
                    message: 'New funding round name is the same as current funding round',
                });
            }
            this.addTransaction(tx.asset.setFundingRound, {}, rawTicker, conversion_1.stringToFundingRoundName(newFundingRound, context));
        }
        if (newIdentifiers) {
            const identifiersAreEqual = internal_2.hasSameElements(identifiers, newIdentifiers);
            if (identifiersAreEqual) {
                throw new internal_1.PolymeshError({
                    code: types_1.ErrorCode.NoDataChange,
                    message: 'New identifiers are the same as current identifiers',
                });
            }
            this.addTransaction(tx.asset.updateIdentifiers, {}, rawTicker, newIdentifiers.map(newIdentifier => conversion_1.tokenIdentifierToAssetIdentifier(newIdentifier, context)));
        }
        return securityToken;
    });
}
exports.prepareModifyToken = prepareModifyToken;
/**
 * @hidden
 */
function getAuthorization({ ticker, makeDivisible, name, fundingRound, identifiers }) {
    const transactions = [];
    if (makeDivisible !== undefined) {
        transactions.push(types_1.TxTags.asset.MakeDivisible);
    }
    if (name) {
        transactions.push(types_1.TxTags.asset.RenameAsset);
    }
    if (fundingRound) {
        transactions.push(types_1.TxTags.asset.SetFundingRound);
    }
    if (identifiers) {
        transactions.push(types_1.TxTags.asset.UpdateIdentifiers);
    }
    return {
        permissions: {
            transactions,
            portfolios: [],
            tokens: [new internal_1.SecurityToken({ ticker }, this.context)],
        },
    };
}
exports.getAuthorization = getAuthorization;
/**
 * @hidden
 */
const modifyToken = () => new internal_1.Procedure(prepareModifyToken, getAuthorization);
exports.modifyToken = modifyToken;
//# sourceMappingURL=modifyToken.js.map