"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = default_1;
const xrpc_server_1 = require("@atproto/xrpc-server");
const auth_scope_1 = require("../../../../auth-scope");
const lexicons_1 = require("../../../../lexicon/lexicons");
function default_1(server, ctx) {
    server.com.atproto.identity.requestPlcOperationSignature({
        auth: ctx.authVerifier.authorization({
            // @NOTE Reflect any change in signPlcOperation
            scopes: auth_scope_1.ACCESS_FULL,
            additional: [auth_scope_1.AuthScope.Takendown],
            authorize: (permissions) => {
                permissions.assertIdentity({ attr: '*' });
            },
        }),
        handler: async ({ auth, req }) => {
            if (ctx.entrywayAgent) {
                await ctx.entrywayAgent.com.atproto.identity.requestPlcOperationSignature(undefined, await ctx.entrywayAuthHeaders(req, auth.credentials.did, lexicons_1.ids.ComAtprotoIdentityRequestPlcOperationSignature));
                return;
            }
            const did = auth.credentials.did;
            const account = await ctx.accountManager.getAccount(did, {
                includeDeactivated: true,
                includeTakenDown: true,
            });
            if (!account) {
                throw new xrpc_server_1.InvalidRequestError('account not found');
            }
            else if (!account.email) {
                throw new xrpc_server_1.InvalidRequestError('account does not have an email address');
            }
            const token = await ctx.accountManager.createEmailToken(did, 'plc_operation');
            await ctx.mailer.sendPlcOperation({ token }, { to: account.email });
        },
    });
}
//# sourceMappingURL=requestPlcOperationSignature.js.map