"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.issueTokens = exports.getAuthorization = exports.prepareIssueTokens = void 0;
const internal_1 = require("../../internal");
const types_1 = require("../../types");
const constants_1 = require("../../utils/constants");
const conversion_1 = require("../../utils/conversion");
/**
 * @hidden
 */
function prepareIssueTokens(args) {
    return __awaiter(this, void 0, void 0, function* () {
        const { context: { polymeshApi: { tx: { asset }, }, }, context, } = this;
        const { ticker, amount } = args;
        const securityToken = new internal_1.SecurityToken({ ticker }, context);
        const { isDivisible, totalSupply } = yield securityToken.details();
        const supplyAfterMint = amount.plus(totalSupply);
        if (supplyAfterMint.isGreaterThan(constants_1.MAX_BALANCE)) {
            throw new internal_1.PolymeshError({
                code: types_1.ErrorCode.ValidationError,
                message: `This issuance operation will cause the total supply of "${ticker}" to exceed the supply limit`,
                data: {
                    currentSupply: totalSupply,
                    supplyLimit: constants_1.MAX_BALANCE,
                },
            });
        }
        const rawTicker = conversion_1.stringToTicker(ticker, context);
        const rawValue = conversion_1.numberToBalance(amount, context, isDivisible);
        this.addTransaction(asset.issue, {}, rawTicker, rawValue);
        return securityToken;
    });
}
exports.prepareIssueTokens = prepareIssueTokens;
/**
 * @hidden
 */
function getAuthorization({ ticker }) {
    const { context } = this;
    return {
        permissions: {
            transactions: [types_1.TxTags.asset.Issue],
            tokens: [new internal_1.SecurityToken({ ticker }, context)],
            portfolios: [],
        },
    };
}
exports.getAuthorization = getAuthorization;
/**
 * @hidden
 */
const issueTokens = () => new internal_1.Procedure(prepareIssueTokens, getAuthorization);
exports.issueTokens = issueTokens;
//# sourceMappingURL=issueTokens.js.map