"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.generator = exports.generatorId = void 0;
const Generator = __importStar(require("@postxl/generator"));
const backend_core_1 = require("../backend-core");
const base_1 = require("../base");
const s3_config_generator_1 = require("./generators/s3.config.generator");
const s3_module_generator_1 = require("./generators/s3.module.generator");
const s3_service_generator_1 = require("./generators/s3.service.generator");
exports.generatorId = Generator.toGeneratorInterfaceId('backend-s3');
exports.generator = {
    id: exports.generatorId,
    requires: [backend_core_1.backendGeneratorId, base_1.baseGeneratorId],
    register: (context) => {
        const moduleName = Generator.toBackendModuleName('s3');
        context.backend.packageJson.dependencies.push({ packageName: '@aws-sdk/client-s3', version: '3.400.0' });
        const module = {
            name: Generator.toClassName('S3Module'),
            location: Generator.toBackendModuleLocation(`@s3/s3.module`),
        };
        const nestJsModule = {
            name: moduleName,
            moduleClass: module,
            apiModuleRegistration: {
                code: Generator.ts(`S3Module.forRoot(config.aws)`),
            },
            envConfig: {
                dotEnvExample: `
          # If true, S3 will be used to seed the database
          AWS_USE_S3=false
          # If true, ACCESS_KEY and SECRET_ACCESS_KEY will be used to authenticate with AWS S3
          AWS_USE_CREDENTIALS=false
          AWS_ACCESS_KEY=""
          AWS_SECRET_ACCESS_KEY=""
          AWS_BUCKET=""
          AWS_FILE_PREFIX=""
          AWS_REGION="eu-central-1"`,
                decoder: Generator.ts(`
            AWS_USE_S3: zEnvBoolean.optional().default('true'),
            AWS_USE_CREDENTIALS: zEnvBoolean.optional().default('false'),
            AWS_ACCESS_KEY: z.string().optional(),
            AWS_SECRET_ACCESS_KEY: z.string().optional(),
            AWS_BUCKET: z.string(),
            AWS_FILE_PREFIX: z.string(),
            AWS_REGION: z.string(),
            `),
                transformer: Generator.ts(`
          aws: {
            useS3: val.AWS_USE_S3,
            useCredentials: val.AWS_USE_CREDENTIALS,
            accessKey: val.AWS_ACCESS_KEY,
            secretAccessKey: val.AWS_SECRET_ACCESS_KEY,
            bucket: val.AWS_BUCKET,
            filePrefix: val.AWS_FILE_PREFIX,
            region: val.AWS_REGION,
          }`),
            },
        };
        context.backend.modules.push(nestJsModule);
        const service = {
            name: Generator.toClassName('S3Service'),
            location: Generator.toBackendModuleLocation(`@s3/s3.service`),
        };
        const configLocation = Generator.toBackendModuleLocation(`@s3/s3.config`);
        const config = {
            name: Generator.toClassName('S3Config'),
            location: configLocation,
            type: { name: Generator.toTypeName('Config'), location: configLocation },
        };
        const s3 = {
            module,
            service,
            config,
        };
        return {
            ...context,
            s3,
        };
    },
    generate: (context) => {
        const vfsSrc = new Generator.VirtualFileSystem();
        vfsSrc.write(Generator.toLocalFile(context.s3.module), (0, s3_module_generator_1.generateS3Module)(context.s3));
        vfsSrc.write(Generator.toLocalFile(context.s3.service), (0, s3_service_generator_1.generateS3Service)(context.s3));
        vfsSrc.write(Generator.toLocalFile(context.s3.config), (0, s3_config_generator_1.generateS3Config)(context.s3));
        const vfs = new Generator.VirtualFileSystem();
        vfs.insertFromVfs({ targetPath: 'src', vfs: vfsSrc });
        vfs.write('/tsconfig.lib.json', Generator.generateTsConfig('s3'));
        context.vfs.insertFromVfs({ targetPath: 'backend/libs/s3', vfs });
        return context;
    },
};
//# sourceMappingURL=s3.generator.js.map