import { CallExpressionDriver } from '../../engine/code-writer/drivers/call-expression.driver.js';
import { ImportDriver } from '../../engine/code-writer/drivers/import.driver.js';
import { addToSharedModule, declareBackPackageModule, runGraphqlTypes, } from '../package-helpers.js';
export default async function install(ctx) {
    await ctx.install([
        '@nestjs/axios',
        '@nestjs/jwt',
        '@nestjs/passport',
        'axios',
        'jwks-rsa',
        'passport',
        'passport-jwt',
    ]);
    await ctx.install(['@types/passport-jwt'], { dev: true });
    declareBackPackageModule(ctx, 'AuthorizationCodeModule', '@api/authorization-code/authorization-code.module');
    const sharedModule = ctx.sourceFile('@aurora', 'shared.module.ts');
    ImportDriver.createImportItems(sharedModule, '@nestjs/axios', ['HttpModule']);
    addToSharedModule(sharedModule, 'imports', 'HttpModule');
    addToSharedModule(sharedModule, 'exports', 'HttpModule');
    sharedModule.saveSync();
    // Replace `UseGuards` arg in auth.decorator with the new guards.
    const authDecorator = ctx.sourceFile('@aurora', 'decorators', 'auth.decorator.ts');
    ImportDriver.createImportItems(authDecorator, '@app/authorization-code', [
        'AuthenticationJwtGuard',
        'AuthorizationPermissionsGuard',
    ]);
    const callExpr = CallExpressionDriver.findCallExpression(authDecorator, 'UseGuards');
    if (callExpr) {
        CallExpressionDriver.removeAllArguments(callExpr);
        CallExpressionDriver.addArgument(callExpr, 'AuthenticationJwtGuard');
        CallExpressionDriver.addArgument(callExpr, 'AuthorizationPermissionsGuard');
    }
    authDecorator.saveSync();
    await runGraphqlTypes(ctx);
}
