"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.modifyPrimaryIssuanceAgent = exports.getAuthorization = exports.prepareModifyPrimaryIssuanceAgent = void 0;
const internal_1 = require("../../internal");
const types_1 = require("../../types");
const conversion_1 = require("../../utils/conversion");
/**
 * @hidden
 *
 * @deprecated in favor of `inviteAgent`
 */
function prepareModifyPrimaryIssuanceAgent(args) {
    return __awaiter(this, void 0, void 0, function* () {
        const { context: { polymeshApi: { tx: { identity }, }, }, context, } = this;
        const { target, ticker, requestExpiry } = args;
        const securityToken = new internal_1.SecurityToken({ ticker }, context);
        const [invalidDids, { primaryIssuanceAgents }] = yield Promise.all([
            context.getInvalidDids([target]),
            securityToken.details(),
        ]);
        if (primaryIssuanceAgents.length) {
            throw new internal_1.PolymeshError({
                code: types_1.ErrorCode.ValidationError,
                message: 'The Primary Issuance Agents must be undefined to perform this procedure',
            });
        }
        if (invalidDids.length) {
            throw new internal_1.PolymeshError({
                code: types_1.ErrorCode.ValidationError,
                message: 'The supplied Identity does not exist',
            });
        }
        const rawSignatory = conversion_1.signerValueToSignatory({ type: types_1.SignerType.Identity, value: conversion_1.signerToString(target) }, context);
        const rawAuthorizationData = conversion_1.authorizationToAuthorizationData({
            type: types_1.AuthorizationType.BecomeAgent,
            value: new internal_1.KnownPermissionGroup({ type: types_1.PermissionGroupType.PolymeshV1Pia, ticker }, context),
        }, context);
        let rawExpiry;
        if (requestExpiry) {
            if (requestExpiry <= new Date()) {
                throw new internal_1.PolymeshError({
                    code: types_1.ErrorCode.ValidationError,
                    message: 'The request expiry must be a future date',
                });
            }
            else {
                rawExpiry = conversion_1.dateToMoment(requestExpiry, context);
            }
        }
        else {
            rawExpiry = null;
        }
        this.addTransaction(identity.addAuthorization, {}, rawSignatory, rawAuthorizationData, rawExpiry);
    });
}
exports.prepareModifyPrimaryIssuanceAgent = prepareModifyPrimaryIssuanceAgent;
/**
 * @hidden
 */
function getAuthorization({ ticker }) {
    return {
        permissions: {
            transactions: [types_1.TxTags.identity.AddAuthorization],
            portfolios: [],
            tokens: [new internal_1.SecurityToken({ ticker }, this.context)],
        },
    };
}
exports.getAuthorization = getAuthorization;
/**
 * @hidden
 */
const modifyPrimaryIssuanceAgent = () => new internal_1.Procedure(prepareModifyPrimaryIssuanceAgent, getAuthorization);
exports.modifyPrimaryIssuanceAgent = modifyPrimaryIssuanceAgent;
//# sourceMappingURL=modifyPrimaryIssuanceAgent.js.map