"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.modifyTokenTrustedClaimIssuers = exports.getAuthorization = exports.prepareModifyTokenTrustedClaimIssuers = void 0;
const lodash_1 = require("lodash");
const types_1 = require("../../polkadot/types");
const internal_1 = require("../../internal");
const types_2 = require("../../types");
const internal_2 = require("../../types/internal");
const utils_1 = require("../../types/utils");
const conversion_1 = require("../../utils/conversion");
const convertArgsToRaw = (claimIssuers, rawTicker, context) => {
    const claimIssuersToAdd = [];
    const inputDids = [];
    claimIssuers.forEach(({ identity, trustedFor }) => {
        let issuerIdentity;
        if (typeof identity === 'string') {
            issuerIdentity = new internal_1.Identity({ did: identity }, context);
        }
        else {
            issuerIdentity = identity;
        }
        claimIssuersToAdd.push(utils_1.tuple(rawTicker, conversion_1.trustedClaimIssuerToTrustedIssuer({ identity: issuerIdentity, trustedFor }, context)));
        inputDids.push(issuerIdentity.did);
    });
    return {
        claimIssuersToAdd,
        inputDids,
    };
};
const areSameClaimIssuers = (currentClaimIssuers, claimIssuers) => !lodash_1.differenceWith(currentClaimIssuers, claimIssuers, ({ identity: { did: aDid }, trustedFor: aTrustedFor }, { identity: bIdentity, trustedFor: bTrustedFor }) => {
    const sameClaimTypes = (aTrustedFor === undefined && bTrustedFor === undefined) ||
        (aTrustedFor && bTrustedFor && lodash_1.isEqual(lodash_1.sortBy(aTrustedFor), lodash_1.sortBy(bTrustedFor)));
    return aDid === conversion_1.signerToString(bIdentity) && !!sameClaimTypes;
}).length && currentClaimIssuers.length === claimIssuers.length;
/**
 * @hidden
 */
function prepareModifyTokenTrustedClaimIssuers(args) {
    return __awaiter(this, void 0, void 0, function* () {
        const { context: { polymeshApi: { query, tx }, }, context, } = this;
        const { ticker } = args;
        const rawTicker = conversion_1.stringToTicker(ticker, context);
        let claimIssuersToDelete = [];
        let claimIssuersToAdd = [];
        let inputDids;
        const rawCurrentClaimIssuers = yield query.complianceManager.trustedClaimIssuer(rawTicker);
        const currentClaimIssuers = rawCurrentClaimIssuers.map(issuer => conversion_1.trustedIssuerToTrustedClaimIssuer(issuer, context));
        const currentClaimIssuerDids = currentClaimIssuers.map(({ identity: { did } }) => did);
        if (args.operation === internal_2.TrustedClaimIssuerOperation.Remove) {
            inputDids = args.claimIssuers.map(conversion_1.signerToString);
            const notPresent = lodash_1.difference(inputDids, currentClaimIssuerDids);
            if (notPresent.length) {
                throw new internal_1.PolymeshError({
                    code: types_2.ErrorCode.UnmetPrerequisite,
                    message: 'One or more of the supplied Identities are not Trusted Claim Issuers',
                    data: {
                        notPresent,
                    },
                });
            }
            claimIssuersToDelete = currentClaimIssuers
                .filter(({ identity: { did } }) => inputDids.includes(did))
                .map(({ identity: { did } }) => utils_1.tuple(rawTicker, conversion_1.stringToIdentityId(did, context)));
        }
        else {
            ({ claimIssuersToAdd, inputDids } = convertArgsToRaw(args.claimIssuers, rawTicker, context));
        }
        if (args.operation === internal_2.TrustedClaimIssuerOperation.Set) {
            claimIssuersToDelete = rawCurrentClaimIssuers.map(({ issuer }) => [rawTicker, issuer]);
            if (areSameClaimIssuers(currentClaimIssuers, args.claimIssuers)) {
                throw new internal_1.PolymeshError({
                    code: types_2.ErrorCode.NoDataChange,
                    message: 'The supplied claim issuer list is equal to the current one',
                });
            }
        }
        if (args.operation === internal_2.TrustedClaimIssuerOperation.Add) {
            const present = lodash_1.intersection(inputDids, currentClaimIssuerDids);
            if (present.length) {
                throw new internal_1.PolymeshError({
                    code: types_2.ErrorCode.UnmetPrerequisite,
                    message: 'One or more of the supplied Identities already are Trusted Claim Issuers',
                    data: {
                        present,
                    },
                });
            }
        }
        const nonExistentDids = yield context.getInvalidDids(inputDids);
        if (nonExistentDids.length) {
            throw new internal_1.PolymeshError({
                code: types_2.ErrorCode.DataUnavailable,
                message: 'Some of the supplied Identities do not exist',
                data: {
                    nonExistentDids,
                },
            });
        }
        if (claimIssuersToDelete.length) {
            this.addBatchTransaction(tx.complianceManager.removeDefaultTrustedClaimIssuer, {}, claimIssuersToDelete);
        }
        if (claimIssuersToAdd.length) {
            this.addBatchTransaction(tx.complianceManager.addDefaultTrustedClaimIssuer, {}, claimIssuersToAdd);
        }
        return new internal_1.SecurityToken({ ticker }, context);
    });
}
exports.prepareModifyTokenTrustedClaimIssuers = prepareModifyTokenTrustedClaimIssuers;
/**
 * @hidden
 */
function getAuthorization({ ticker, operation }) {
    const transactions = [];
    if (operation !== internal_2.TrustedClaimIssuerOperation.Add) {
        transactions.push(types_1.TxTags.complianceManager.RemoveDefaultTrustedClaimIssuer);
    }
    if (operation !== internal_2.TrustedClaimIssuerOperation.Remove) {
        transactions.push(types_1.TxTags.complianceManager.AddDefaultTrustedClaimIssuer);
    }
    return {
        permissions: {
            transactions,
            tokens: [new internal_1.SecurityToken({ ticker }, this.context)],
            portfolios: [],
        },
    };
}
exports.getAuthorization = getAuthorization;
/**
 * @hidden
 */
const modifyTokenTrustedClaimIssuers = () => new internal_1.Procedure(prepareModifyTokenTrustedClaimIssuers, getAuthorization);
exports.modifyTokenTrustedClaimIssuers = modifyTokenTrustedClaimIssuers;
//# sourceMappingURL=modifyTokenTrustedClaimIssuers.js.map