"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.IdentityAuthorizations = void 0;
const internal_1 = require("../../../internal");
const utils_1 = require("../../../types/utils");
const conversion_1 = require("../../../utils/conversion");
const internal_2 = require("../../../utils/internal");
/**
 * Handles all Identity Authorization related functionality
 */
class IdentityAuthorizations extends internal_1.Authorizations {
    /**
     * Fetch all pending authorization requests issued by this Identity
     *
     * @note supports pagination
     */
    getSent(paginationOpts) {
        return __awaiter(this, void 0, void 0, function* () {
            const { context: { polymeshApi: { query: { identity }, }, }, context, parent: { did }, } = this;
            const { entries, lastKey: next } = yield internal_2.requestPaginated(identity.authorizationsGiven, {
                arg: conversion_1.stringToIdentityId(did, context),
                paginationOpts,
            });
            const authQueryParams = entries.map(([storageKey, signatory]) => utils_1.tuple(signatory, storageKey.args[1]));
            const authorizations = yield identity.authorizations.multi(authQueryParams);
            const data = this.createAuthorizationRequests(authorizations.map((auth, index) => ({
                auth: auth.unwrap(),
                target: conversion_1.signatoryToSignerValue(authQueryParams[index][0]),
            })));
            return {
                data,
                next,
            };
        });
    }
}
exports.IdentityAuthorizations = IdentityAuthorizations;
//# sourceMappingURL=IdentityAuthorizations.js.map