"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.modifyAsset = exports.getAuthorization = exports.prepareModifyAsset = 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 prepareModifyAsset(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 asset = new internal_1.Asset({ ticker }, context);
        const [{ isDivisible, name }, fundingRound, identifiers] = yield Promise.all([
            asset.details(),
            asset.currentFundingRound(),
            asset.getIdentifiers(),
        ]);
        const transactions = [];
        if (makeDivisible) {
            if (isDivisible) {
                throw new internal_1.PolymeshError({
                    code: types_1.ErrorCode.NoDataChange,
                    message: 'The Asset is already divisible',
                });
            }
            transactions.push(internal_2.checkTxType({
                transaction: tx.asset.makeDivisible,
                args: [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',
                });
            }
            transactions.push(internal_2.checkTxType({
                transaction: tx.asset.renameAsset,
                args: [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',
                });
            }
            transactions.push(internal_2.checkTxType({
                transaction: tx.asset.setFundingRound,
                args: [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',
                });
            }
            transactions.push(internal_2.checkTxType({
                transaction: tx.asset.updateIdentifiers,
                args: [
                    rawTicker,
                    newIdentifiers.map(newIdentifier => conversion_1.securityIdentifierToAssetIdentifier(newIdentifier, context)),
                ],
            }));
        }
        this.addBatchTransaction({ transactions });
        return asset;
    });
}
exports.prepareModifyAsset = prepareModifyAsset;
/**
 * @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: [],
            assets: [new internal_1.Asset({ ticker }, this.context)],
        },
    };
}
exports.getAuthorization = getAuthorization;
/**
 * @hidden
 */
const modifyAsset = () => new internal_1.Procedure(prepareModifyAsset, getAuthorization);
exports.modifyAsset = modifyAsset;
//# sourceMappingURL=modifyAsset.js.map