"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.addAssetRequirement = exports.getAuthorization = exports.prepareAddAssetRequirement = void 0;
const bignumber_js_1 = __importDefault(require("bignumber.js"));
const lodash_1 = require("lodash");
const utils_1 = require("./utils");
const internal_1 = require("../../internal");
const types_1 = require("../../types");
const conversion_1 = require("../../utils/conversion");
const internal_2 = require("../../utils/internal");
/**
 * @hidden
 */
function prepareAddAssetRequirement(args) {
    return __awaiter(this, void 0, void 0, function* () {
        const { context: { polymeshApi: { tx }, }, context, } = this;
        const { ticker, conditions } = args;
        const rawTicker = conversion_1.stringToTicker(ticker, context);
        const asset = new internal_1.Asset({ ticker }, context);
        const { requirements: currentRequirements, defaultTrustedClaimIssuers } = yield asset.compliance.requirements.get();
        const currentConditions = lodash_1.map(currentRequirements, 'conditions');
        // if the new requirement has the same conditions as any existing one, we throw an error
        if (currentConditions.some(requirementConditions => internal_2.hasSameElements(requirementConditions, conditions, internal_2.conditionsAreEqual))) {
            throw new internal_1.PolymeshError({
                code: types_1.ErrorCode.NoDataChange,
                message: 'There already exists a Requirement with the same conditions for this Asset',
            });
        }
        // check that the new requirement won't cause the current ones to exceed the max complexity
        utils_1.assertRequirementsNotTooComplex([...lodash_1.flatten(currentConditions), ...conditions], new bignumber_js_1.default(defaultTrustedClaimIssuers.length), context);
        const { sender_conditions: senderConditions, receiver_conditions: receiverConditions } = conversion_1.requirementToComplianceRequirement({ conditions, id: new bignumber_js_1.default(1) }, context);
        this.addTransaction({
            transaction: tx.complianceManager.addComplianceRequirement,
            args: [rawTicker, senderConditions, receiverConditions],
        });
        return asset;
    });
}
exports.prepareAddAssetRequirement = prepareAddAssetRequirement;
/**
 * @hidden
 */
function getAuthorization({ ticker }) {
    return {
        permissions: {
            transactions: [types_1.TxTags.complianceManager.AddComplianceRequirement],
            assets: [new internal_1.Asset({ ticker }, this.context)],
            portfolios: [],
        },
    };
}
exports.getAuthorization = getAuthorization;
/**
 * @hidden
 */
const addAssetRequirement = () => new internal_1.Procedure(prepareAddAssetRequirement, getAuthorization);
exports.addAssetRequirement = addAssetRequirement;
//# sourceMappingURL=addAssetRequirement.js.map