"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.transferTickerOwnership = exports.getAuthorization = exports.prepareTransferTickerOwnership = void 0;
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 prepareTransferTickerOwnership(args) {
    return __awaiter(this, void 0, void 0, function* () {
        const { context: { polymeshApi: { tx }, }, context, } = this;
        const { ticker, target, expiry } = args;
        const issuer = yield context.getCurrentIdentity();
        const targetIdentity = yield context.getIdentity(target);
        const tickerReservation = new internal_1.TickerReservation({ ticker }, context);
        const { status } = yield tickerReservation.details();
        if (status === types_1.TickerReservationStatus.TokenCreated) {
            throw new internal_1.PolymeshError({
                code: types_1.ErrorCode.UnmetPrerequisite,
                message: 'A Security Token with this ticker has already been created',
            });
        }
        const rawSignatory = conversion_1.signerValueToSignatory({ type: types_1.SignerType.Identity, value: conversion_1.signerToString(target) }, context);
        const authReq = {
            type: types_1.AuthorizationType.TransferTicker,
            value: ticker,
        };
        const rawAuthorizationData = conversion_1.authorizationToAuthorizationData(authReq, context);
        const rawExpiry = internal_2.optionize(conversion_1.dateToMoment)(expiry, context);
        const [auth] = this.addTransaction(tx.identity.addAuthorization, {
            resolvers: [
                utils_1.createAuthorizationResolver(authReq, issuer, targetIdentity, expiry || null, context),
            ],
        }, rawSignatory, rawAuthorizationData, rawExpiry);
        return auth;
    });
}
exports.prepareTransferTickerOwnership = prepareTransferTickerOwnership;
/**
 * @hidden
 */
function getAuthorization({ ticker }) {
    return {
        roles: [{ type: types_1.RoleType.TickerOwner, ticker }],
        permissions: {
            tokens: [],
            transactions: [types_1.TxTags.identity.AddAuthorization],
            portfolios: [],
        },
    };
}
exports.getAuthorization = getAuthorization;
/**
 * @hidden
 */
const transferTickerOwnership = () => new internal_1.Procedure(prepareTransferTickerOwnership, getAuthorization);
exports.transferTickerOwnership = transferTickerOwnership;
//# sourceMappingURL=transferTickerOwnership.js.map