"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.quitCustody = exports.prepareStorage = exports.getAuthorization = exports.prepareQuitCustody = void 0;
const utils_1 = require("./utils");
const internal_1 = require("../../internal");
const types_1 = require("../../types");
const conversion_1 = require("../../utils/conversion");
/**
 * @hidden
 */
function prepareQuitCustody(args) {
    return __awaiter(this, void 0, void 0, function* () {
        const { context: { polymeshApi: { tx }, }, storage: { portfolioId }, context, } = this;
        const { portfolio } = args;
        const isOwnedBy = yield portfolio.isOwnedBy();
        if (isOwnedBy) {
            throw new internal_1.PolymeshError({
                code: types_1.ErrorCode.ValidationError,
                message: 'The Portfolio owner cannot quit custody',
            });
        }
        yield utils_1.assertPortfolioExists(portfolioId, context);
        const rawPortfolioId = conversion_1.portfolioIdToMeshPortfolioId(portfolioId, context);
        this.addTransaction(tx.portfolio.quitPortfolioCustody, {}, rawPortfolioId);
    });
}
exports.prepareQuitCustody = prepareQuitCustody;
/**
 * @hidden
 */
function getAuthorization({ portfolio }) {
    const { storage: { portfolioId }, } = this;
    return {
        permissions: {
            transactions: [types_1.TxTags.portfolio.QuitPortfolioCustody],
            tokens: [],
            portfolios: [portfolio],
        },
        roles: [{ type: types_1.RoleType.PortfolioCustodian, portfolioId }],
    };
}
exports.getAuthorization = getAuthorization;
/**
 * @hidden
 */
function prepareStorage({ portfolio }) {
    return __awaiter(this, void 0, void 0, function* () {
        return {
            portfolioId: conversion_1.portfolioLikeToPortfolioId(portfolio),
        };
    });
}
exports.prepareStorage = prepareStorage;
/**
 * @hidden
 */
const quitCustody = () => new internal_1.Procedure(prepareQuitCustody, getAuthorization, prepareStorage);
exports.quitCustody = quitCustody;
//# sourceMappingURL=quitCustody.js.map