"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.setAssetRequirements = exports.getAuthorization = exports.prepareSetAssetRequirements = 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 prepareSetAssetRequirements(args) {
    return __awaiter(this, void 0, void 0, function* () {
        const { context: { polymeshApi: { tx }, }, context, } = this;
        const { ticker, requirements } = 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');
        utils_1.assertRequirementsNotTooComplex(lodash_1.flatten(requirements), new bignumber_js_1.default(defaultTrustedClaimIssuers.length), context);
        const comparator = (a, b) => {
            return internal_2.hasSameElements(a, b, internal_2.conditionsAreEqual);
        };
        if (internal_2.hasSameElements(requirements, currentConditions, comparator)) {
            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({
                transaction: tx.complianceManager.resetAssetCompliance,
                args: [rawTicker],
            });
        }
        else {
            const rawAssetCompliance = requirements.map((requirement, index) => conversion_1.requirementToComplianceRequirement({ conditions: requirement, id: new bignumber_js_1.default(index) }, context));
            this.addTransaction({
                transaction: tx.complianceManager.replaceAssetCompliance,
                args: [rawTicker, rawAssetCompliance],
            });
        }
        return asset;
    });
}
exports.prepareSetAssetRequirements = prepareSetAssetRequirements;
/**
 * @hidden
 */
function getAuthorization({ ticker, requirements }) {
    return {
        permissions: {
            transactions: requirements.length
                ? [types_1.TxTags.complianceManager.ReplaceAssetCompliance]
                : [types_1.TxTags.complianceManager.ResetAssetCompliance],
            assets: [new internal_1.Asset({ ticker }, this.context)],
            portfolios: [],
        },
    };
}
exports.getAuthorization = getAuthorization;
/**
 * @hidden
 */
const setAssetRequirements = () => new internal_1.Procedure(prepareSetAssetRequirements, getAuthorization);
exports.setAssetRequirements = setAssetRequirements;
//# sourceMappingURL=setAssetRequirements.js.map