"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.setAssetRequirements = exports.getAuthorization = exports.prepareSetAssetRequirements = void 0;
const lodash_1 = require("lodash");
const internal_1 = require("../../internal");
const types_1 = require("../../types");
const conversion_1 = require("../../utils/conversion");
/**
 * @hidden
 */
function prepareSetAssetRequirements(args) {
    return __awaiter(this, void 0, void 0, function* () {
        const { context: { polymeshApi: { query, tx, consts }, }, context, } = this;
        const { ticker, requirements } = args;
        const rawTicker = conversion_1.stringToTicker(ticker, context);
        const maxConditionComplexity = conversion_1.u32ToBigNumber(consts.complianceManager.maxConditionComplexity).toNumber();
        if (requirements.length >= maxConditionComplexity) {
            throw new internal_1.PolymeshError({
                code: types_1.ErrorCode.ValidationError,
                message: 'Condition limit reached',
                data: { limit: maxConditionComplexity },
            });
        }
        const rawCurrentAssetCompliance = yield query.complianceManager.assetCompliances(rawTicker);
        const currentRequirements = rawCurrentAssetCompliance.requirements.map(requirement => conversion_1.complianceRequirementToRequirement(requirement, context).conditions);
        const comparator = (a, b) => {
            return !lodash_1.differenceWith(a, b, lodash_1.isEqual).length && a.length === b.length;
        };
        if (!lodash_1.differenceWith(requirements, currentRequirements, comparator).length &&
            requirements.length === currentRequirements.length) {
            throw new internal_1.PolymeshError({
                code: types_1.ErrorCode.NoDataChange,
                message: 'The supplied condition list is equal to the current one',
            });
        }
        if (!requirements.length) {
            this.addTransaction(tx.complianceManager.resetAssetCompliance, {}, rawTicker);
        }
        else {
            const rawAssetCompliance = requirements.map((requirement, index) => conversion_1.requirementToComplianceRequirement({ conditions: requirement, id: index }, context));
            this.addTransaction(tx.complianceManager.replaceAssetCompliance, {}, rawTicker, rawAssetCompliance);
        }
        return new internal_1.SecurityToken({ ticker }, context);
    });
}
exports.prepareSetAssetRequirements = prepareSetAssetRequirements;
/**
 * @hidden
 */
function getAuthorization({ ticker }) {
    return {
        permissions: {
            transactions: [
                types_1.TxTags.complianceManager.ResetAssetCompliance,
                types_1.TxTags.complianceManager.AddComplianceRequirement,
            ],
            tokens: [new internal_1.SecurityToken({ ticker }, this.context)],
            portfolios: [],
        },
    };
}
exports.getAuthorization = getAuthorization;
/**
 * @hidden
 */
const setAssetRequirements = () => new internal_1.Procedure(prepareSetAssetRequirements, getAuthorization);
exports.setAssetRequirements = setAssetRequirements;
//# sourceMappingURL=setAssetRequirements.js.map