"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.modifyComplianceRequirement = exports.getAuthorization = exports.prepareModifyComplianceRequirement = void 0;
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 prepareModifyComplianceRequirement(args) {
    return __awaiter(this, void 0, void 0, function* () {
        const { context: { polymeshApi: { tx }, }, context, } = this;
        const { ticker, id, conditions: newConditions } = args;
        const rawTicker = conversion_1.stringToTicker(ticker, context);
        const token = new internal_1.SecurityToken({ ticker }, context);
        const { requirements: currentRequirements, defaultTrustedClaimIssuers, } = yield token.compliance.requirements.get();
        const existingRequirements = lodash_1.remove(currentRequirements, ({ id: currentRequirementId }) => id === currentRequirementId);
        if (!existingRequirements.length) {
            throw new internal_1.PolymeshError({
                code: types_1.ErrorCode.DataUnavailable,
                message: 'The Compliance Requirement does not exist',
            });
        }
        const [{ conditions: existingConditions }] = existingRequirements;
        if (internal_2.hasSameElements(newConditions, existingConditions, internal_2.conditionsAreEqual)) {
            throw new internal_1.PolymeshError({
                code: types_1.ErrorCode.NoDataChange,
                message: 'The supplied condition list is equal to the current one',
            });
        }
        const unchangedConditions = lodash_1.flatMap(currentRequirements, 'conditions');
        utils_1.assertRequirementsNotTooComplex([...unchangedConditions, ...newConditions], defaultTrustedClaimIssuers.length, context);
        const rawComplianceRequirement = conversion_1.requirementToComplianceRequirement({ conditions: newConditions, id }, context);
        this.addTransaction(tx.complianceManager.changeComplianceRequirement, {}, rawTicker, rawComplianceRequirement);
    });
}
exports.prepareModifyComplianceRequirement = prepareModifyComplianceRequirement;
/**
 * @hidden
 */
function getAuthorization({ ticker }) {
    return {
        permissions: {
            transactions: [types_1.TxTags.complianceManager.ChangeComplianceRequirement],
            tokens: [new internal_1.SecurityToken({ ticker }, this.context)],
            portfolios: [],
        },
    };
}
exports.getAuthorization = getAuthorization;
/**
 * @hidden
 */
const modifyComplianceRequirement = () => new internal_1.Procedure(prepareModifyComplianceRequirement, getAuthorization);
exports.modifyComplianceRequirement = modifyComplianceRequirement;
//# sourceMappingURL=modifyComplianceRequirement.js.map