"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 internal_1 = require("../../internal");
const types_1 = require("../../types");
const conversion_1 = require("../../utils/conversion");
/**
 * @hidden
 */
function prepareTransferTickerOwnership(args) {
    return __awaiter(this, void 0, void 0, function* () {
        const { context: { polymeshApi: { tx }, }, context, } = this;
        const { ticker, target, expiry } = args;
        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.ValidationError,
                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 rawAuthorizationData = conversion_1.authorizationToAuthorizationData({ type: types_1.AuthorizationType.TransferTicker, value: ticker }, context);
        const rawExpiry = expiry ? conversion_1.dateToMoment(expiry, context) : null;
        this.addTransaction(tx.identity.addAuthorization, {}, rawSignatory, rawAuthorizationData, rawExpiry);
        return tickerReservation;
    });
}
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