"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());
    });
};
var __rest = (this && this.__rest) || function (s, e) {
    var t = {};
    for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
        t[p] = s[p];
    if (s != null && typeof Object.getOwnPropertySymbols === "function")
        for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
            if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
                t[p[i]] = s[p[i]];
        }
    return t;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.AuthorizationRequest = void 0;
const bignumber_js_1 = __importDefault(require("bignumber.js"));
const internal_1 = require("../../internal");
const types_1 = require("../../types");
const conversion_1 = require("../../utils/conversion");
const internal_2 = require("../../utils/internal");
/**
 * Represents a request made by an Identity to another Identity (or Account) for some sort of authorization. This has multiple uses. For example, if Alice
 *   wants to transfer ownership of one of her Assets to Bob, this method emits an authorization request for Bob,
 *   who then has to accept it in order to complete the ownership transfer
 */
class AuthorizationRequest extends internal_1.Entity {
    /**
     * @hidden
     */
    constructor(args, context) {
        const { target, issuer, expiry, data } = args, identifiers = __rest(args, ["target", "issuer", "expiry", "data"]);
        super(identifiers, context);
        const { authId } = identifiers;
        this.target = target;
        this.issuer = issuer;
        this.authId = authId;
        this.expiry = expiry;
        this.data = data;
        this.accept = internal_2.createProcedureMethod({
            getProcedureAndArgs: () => {
                switch (this.data.type) {
                    case types_1.AuthorizationType.JoinIdentity:
                    case types_1.AuthorizationType.RotatePrimaryKey:
                    case types_1.AuthorizationType.RotatePrimaryKeyToSecondary: {
                        return [internal_1.consumeJoinOrRotateAuthorization, { authRequest: this, accept: true }];
                    }
                    case types_1.AuthorizationType.AddMultiSigSigner: {
                        return [internal_1.consumeAddMultiSigSignerAuthorization, { authRequest: this, accept: true }];
                    }
                    default: {
                        return [internal_1.consumeAuthorizationRequests, { authRequests: [this], accept: true }];
                    }
                }
            },
            voidArgs: true,
        }, context);
        this.remove = internal_2.createProcedureMethod({
            getProcedureAndArgs: () => {
                switch (this.data.type) {
                    case types_1.AuthorizationType.JoinIdentity: {
                        return [internal_1.consumeJoinOrRotateAuthorization, { authRequest: this, accept: false }];
                    }
                    case types_1.AuthorizationType.RotatePrimaryKeyToSecondary: {
                        return [internal_1.consumeJoinOrRotateAuthorization, { authRequest: this, accept: false }];
                    }
                    case types_1.AuthorizationType.AddMultiSigSigner: {
                        return [internal_1.consumeAddMultiSigSignerAuthorization, { authRequest: this, accept: false }];
                    }
                    default: {
                        return [internal_1.consumeAuthorizationRequests, { authRequests: [this], accept: false }];
                    }
                }
            },
            voidArgs: true,
        }, context);
    }
    /**
     * @hidden
     * Check if a value is of type [[UniqueIdentifiers]]
     */
    static isUniqueIdentifiers(identifier) {
        const { authId } = identifier;
        return authId instanceof bignumber_js_1.default;
    }
    /**
     * Returns whether the Authorization Request has expired
     */
    isExpired() {
        const { expiry } = this;
        return expiry !== null && expiry < new Date();
    }
    /**
     * Determine whether this Authorization Request exists on chain
     */
    exists() {
        return __awaiter(this, void 0, void 0, function* () {
            const { authId, target, context } = this;
            const auth = yield context.polymeshApi.query.identity.authorizations(conversion_1.signerValueToSignatory(conversion_1.signerToSignerValue(target), context), conversion_1.bigNumberToU64(authId, context));
            return auth.isSome;
        });
    }
    /**
     * Return the Authorization's static data
     */
    toJson() {
        const { data, issuer, target, expiry, authId } = this;
        return internal_2.toHumanReadable({
            id: authId,
            expiry,
            data,
            issuer,
            target: conversion_1.signerToSignerValue(target),
        });
    }
}
exports.AuthorizationRequest = AuthorizationRequest;
//# sourceMappingURL=AuthorizationRequest.js.map