"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());
    });
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.removeAssetRequirement = exports.getAuthorization = exports.prepareRemoveAssetRequirement = void 0;
const bignumber_js_1 = __importDefault(require("bignumber.js"));
const internal_1 = require("../../internal");
const types_1 = require("../../types");
const conversion_1 = require("../../utils/conversion");
/**
 * @hidden
 */
function prepareRemoveAssetRequirement(args) {
    return __awaiter(this, void 0, void 0, function* () {
        const { context: { polymeshApi: { query, tx }, }, context, } = this;
        const { ticker, requirement } = args;
        const rawTicker = (0, conversion_1.stringToTicker)(ticker, context);
        const reqId = requirement instanceof bignumber_js_1.default ? requirement : requirement.id;
        const { requirements } = yield query.complianceManager.assetCompliances(rawTicker);
        if (requirements.filter(({ id: rawId }) => (0, conversion_1.u32ToBigNumber)(rawId).eq(reqId)).length) {
            throw new internal_1.PolymeshError({
                code: types_1.ErrorCode.DataUnavailable,
                message: `There is no compliance requirement with id "${reqId}"`,
            });
        }
        this.addTransaction({
            transaction: tx.complianceManager.removeComplianceRequirement,
            args: [rawTicker, (0, conversion_1.bigNumberToU32)(reqId, context)],
        });
        return new internal_1.Asset({ ticker }, context);
    });
}
exports.prepareRemoveAssetRequirement = prepareRemoveAssetRequirement;
/**
 * @hidden
 */
function getAuthorization({ ticker }) {
    return {
        permissions: {
            transactions: [types_1.TxTags.complianceManager.RemoveComplianceRequirement],
            assets: [new internal_1.Asset({ ticker }, this.context)],
            portfolios: [],
        },
    };
}
exports.getAuthorization = getAuthorization;
/**
 * @hidden
 */
const removeAssetRequirement = () => new internal_1.Procedure(prepareRemoveAssetRequirement, getAuthorization);
exports.removeAssetRequirement = removeAssetRequirement;
//# sourceMappingURL=removeAssetRequirement.js.map