"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.controllerTransfer = exports.getAuthorization = exports.prepareControllerTransfer = void 0;
const internal_1 = require("../../internal");
const types_1 = require("../../types");
const conversion_1 = require("../../utils/conversion");
/**
 * @hidden
 */
function prepareControllerTransfer(args) {
    return __awaiter(this, void 0, void 0, function* () {
        const { context: { polymeshApi: { tx }, }, context, } = this;
        const { ticker, originPortfolio, amount } = args;
        const token = new internal_1.SecurityToken({ ticker }, context);
        const originPortfolioId = conversion_1.portfolioLikeToPortfolioId(originPortfolio);
        const fromPortfolio = conversion_1.portfolioIdToPortfolio(originPortfolioId, context);
        const [{ free }] = yield fromPortfolio.getTokenBalances({
            tokens: [token],
        });
        if (free.lt(amount)) {
            throw new internal_1.PolymeshError({
                code: types_1.ErrorCode.ValidationError,
                message: 'The Portfolio does not have enough free balance for this transfer',
                data: { free },
            });
        }
        this.addTransaction(tx.asset.controllerTransfer, {}, conversion_1.stringToTicker(ticker, context), conversion_1.numberToBalance(amount, context), conversion_1.portfolioIdToMeshPortfolioId(originPortfolioId, context));
    });
}
exports.prepareControllerTransfer = prepareControllerTransfer;
/**
 * @hidden
 */
function getAuthorization({ ticker }) {
    return __awaiter(this, void 0, void 0, function* () {
        const { context } = this;
        const token = new internal_1.SecurityToken({ ticker }, context);
        const { did } = yield context.getCurrentIdentity();
        const portfolioId = { did };
        return {
            roles: [{ type: types_1.RoleType.PortfolioCustodian, portfolioId }],
            permissions: {
                tokens: [token],
                transactions: [types_1.TxTags.asset.ControllerTransfer],
                portfolios: [new internal_1.DefaultPortfolio({ did }, context)],
            },
        };
    });
}
exports.getAuthorization = getAuthorization;
/**
 * @hidden
 */
const controllerTransfer = () => new internal_1.Procedure(prepareControllerTransfer, getAuthorization);
exports.controllerTransfer = controllerTransfer;
//# sourceMappingURL=controllerTransfer.js.map