"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.transferAssetOwnership = exports.getAuthorization = exports.prepareTransferAssetOwnership = 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 prepareTransferAssetOwnership(args) {
    return __awaiter(this, void 0, void 0, function* () {
        const { context: { polymeshApi: { tx }, }, context, } = this;
        const { ticker, target, expiry = null } = args;
        const issuer = yield context.getSigningIdentity();
        const targetIdentity = yield context.getIdentity(target);
        const rawSignatory = conversion_1.signerValueToSignatory({ type: types_1.SignerType.Identity, value: conversion_1.signerToString(target) }, context);
        const authRequest = {
            type: types_1.AuthorizationType.TransferAssetOwnership,
            value: ticker,
        };
        const rawAuthorizationData = conversion_1.authorizationToAuthorizationData(authRequest, context);
        const rawExpiry = internal_2.optionize(conversion_1.dateToMoment)(expiry, context);
        const [auth] = this.addTransaction({
            transaction: tx.identity.addAuthorization,
            resolvers: [utils_1.createAuthorizationResolver(authRequest, issuer, targetIdentity, expiry, context)],
            args: [rawSignatory, rawAuthorizationData, rawExpiry],
        });
        return auth;
    });
}
exports.prepareTransferAssetOwnership = prepareTransferAssetOwnership;
/**
 * @hidden
 */
function getAuthorization({ ticker }) {
    return {
        permissions: {
            assets: [new internal_1.Asset({ ticker }, this.context)],
            transactions: [types_1.TxTags.identity.AddAuthorization],
            portfolios: [],
        },
    };
}
exports.getAuthorization = getAuthorization;
/**
 * @hidden
 */
const transferAssetOwnership = () => new internal_1.Procedure(prepareTransferAssetOwnership, getAuthorization);
exports.transferAssetOwnership = transferAssetOwnership;
//# sourceMappingURL=transferAssetOwnership.js.map