"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.PermissionsGrant = void 0;
const time_js_1 = require("../utils/time.js");
const object_js_1 = require("../utils/object.js");
const auth_js_1 = require("../core/auth.js");
const dwn_error_js_1 = require("../core/dwn-error.js");
const message_js_1 = require("../core/message.js");
class PermissionsGrant extends message_js_1.Message {
    static parse(message) {
        return __awaiter(this, void 0, void 0, function* () {
            yield (0, auth_js_1.validateAuthorizationIntegrity)(message);
            return new PermissionsGrant(message);
        });
    }
    static create(options) {
        var _a;
        return __awaiter(this, void 0, void 0, function* () {
            const descriptor = {
                interface: message_js_1.DwnInterfaceName.Permissions,
                method: message_js_1.DwnMethodName.Grant,
                messageTimestamp: (_a = options.messageTimestamp) !== null && _a !== void 0 ? _a : (0, time_js_1.getCurrentTimeInHighPrecision)(),
                description: options.description,
                grantedTo: options.grantedTo,
                grantedBy: options.grantedBy,
                grantedFor: options.grantedFor,
                permissionsRequestId: options === null || options === void 0 ? void 0 : options.permissionsRequestId,
                scope: options.scope,
                conditions: options.conditions,
            };
            // delete all descriptor properties that are `undefined` else the code will encounter the following IPLD issue when attempting to generate CID:
            // Error: `undefined` is not supported by the IPLD Data Model and cannot be encoded
            (0, object_js_1.removeUndefinedProperties)(descriptor);
            const authorization = yield message_js_1.Message.signAsAuthorization(descriptor, options.authorizationSignatureInput);
            const message = { descriptor, authorization };
            message_js_1.Message.validateJsonSchema(message);
            return new PermissionsGrant(message);
        });
    }
    /**
     * generates a PermissionsGrant using the provided PermissionsRequest
     * @param permissionsRequest
     * @param authorizationSignatureInput - the private key and additional signature material of the grantor
     * @param overrides - optional overrides that will be used instead of the properties in `permissionsRequest`
     */
    static createFromPermissionsRequest(permissionsRequest, authorizationSignatureInput, overrides) {
        var _a, _b, _c, _d, _e, _f;
        return __awaiter(this, void 0, void 0, function* () {
            const descriptor = permissionsRequest.message.descriptor;
            return PermissionsGrant.create({
                description: (_a = overrides === null || overrides === void 0 ? void 0 : overrides.description) !== null && _a !== void 0 ? _a : descriptor.description,
                grantedBy: (_b = overrides === null || overrides === void 0 ? void 0 : overrides.grantedBy) !== null && _b !== void 0 ? _b : descriptor.grantedBy,
                grantedTo: (_c = overrides === null || overrides === void 0 ? void 0 : overrides.grantedTo) !== null && _c !== void 0 ? _c : descriptor.grantedTo,
                grantedFor: (_d = overrides === null || overrides === void 0 ? void 0 : overrides.grantedFor) !== null && _d !== void 0 ? _d : descriptor.grantedFor,
                permissionsRequestId: yield message_js_1.Message.getCid(permissionsRequest.message),
                scope: (_e = overrides === null || overrides === void 0 ? void 0 : overrides.scope) !== null && _e !== void 0 ? _e : descriptor.scope,
                conditions: (_f = overrides === null || overrides === void 0 ? void 0 : overrides.conditions) !== null && _f !== void 0 ? _f : descriptor.conditions,
                authorizationSignatureInput,
            });
        });
    }
    authorize() {
        const { grantedBy, grantedFor } = this.message.descriptor;
        if (this.author !== grantedBy) {
            throw new dwn_error_js_1.DwnError(dwn_error_js_1.DwnErrorCode.PermissionsGrantGrantedByMismatch, 'Message author must match grantedBy property');
        }
        else if (grantedBy !== grantedFor) {
            // Without delegation, only the DWN owner may grant access to their own DWN.
            throw new dwn_error_js_1.DwnError(dwn_error_js_1.DwnErrorCode.PermissionsGrantUnauthorizedGrant, `${grantedBy} is not authorized to give access to the DWN belonging to ${grantedFor}`);
        }
    }
}
exports.PermissionsGrant = PermissionsGrant;
//# sourceMappingURL=permissions-grant.js.map