"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.addInvestorUniquenessClaim = exports.getAuthorization = exports.prepareAddInvestorUniquenessClaim = void 0;
const internal_1 = require("../../internal");
const types_1 = require("../../types");
const conversion_1 = require("../../utils/conversion");
/**
 * @hidden
 */
function prepareAddInvestorUniquenessClaim(args) {
    return __awaiter(this, void 0, void 0, function* () {
        const { context: { polymeshApi: { tx }, }, context, } = this;
        const { scope, cddId, scopeId, proof, expiry } = args;
        const { did } = yield context.getCurrentIdentity();
        if (expiry && expiry < new Date()) {
            throw new internal_1.PolymeshError({
                code: types_1.ErrorCode.ValidationError,
                message: 'Expiry date must be in the future',
            });
        }
        const meshIdentityId = conversion_1.stringToIdentityId(did, context);
        const meshExpiry = expiry ? conversion_1.dateToMoment(expiry, context) : null;
        if (typeof proof === 'string') {
            const meshClaim = conversion_1.claimToMeshClaim({ type: types_1.ClaimType.InvestorUniqueness, scope, cddId, scopeId }, context);
            this.addTransaction(tx.identity.addInvestorUniquenessClaim, {}, meshIdentityId, meshClaim, conversion_1.stringToInvestorZKProofData(proof, context), meshExpiry);
        }
        else {
            const meshClaim = conversion_1.claimToMeshClaim({ type: types_1.ClaimType.InvestorUniquenessV2, cddId }, context);
            this.addTransaction(tx.identity.addInvestorUniquenessClaimV2, {}, meshIdentityId, conversion_1.scopeToMeshScope(scope, context), meshClaim, conversion_1.scopeClaimProofToMeshScopeClaimProof(proof, scopeId, context), meshExpiry);
        }
    });
}
exports.prepareAddInvestorUniquenessClaim = prepareAddInvestorUniquenessClaim;
/**
 * @hidden
 */
function getAuthorization({ proof }) {
    let transactions;
    if (typeof proof === 'string') {
        transactions = [types_1.TxTags.identity.AddInvestorUniquenessClaim];
    }
    else {
        transactions = [types_1.TxTags.identity.AddInvestorUniquenessClaimV2];
    }
    return {
        permissions: {
            tokens: [],
            portfolios: [],
            transactions,
        },
    };
}
exports.getAuthorization = getAuthorization;
/**
 * @hidden
 */
const addInvestorUniquenessClaim = () => new internal_1.Procedure(prepareAddInvestorUniquenessClaim, getAuthorization);
exports.addInvestorUniquenessClaim = addInvestorUniquenessClaim;
//# sourceMappingURL=addInvestorUniquenessClaim.js.map