"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 path = __importStar(require("node:path"));
const Generator = __importStar(require("@postxl/generator"));
const generator_1 = require("@postxl/generator");
const backend_actions_1 = require("../backend-actions");
const backend_core_1 = require("../backend-core");
const backend_s3_1 = require("../backend-s3");
const backend_view_1 = require("../backend-view");
const types_1 = require("../types");
exports.generatorId = Generator.toGeneratorInterfaceId('backend-upload');
exports.generator = {
    id: exports.generatorId,
    requires: [
        backend_core_1.backendGeneratorId,
        backend_actions_1.backendActionsGeneratorId,
        backend_view_1.backendViewGeneratorId,
        backend_s3_1.backendS3GeneratorId,
        types_1.typesGeneratorId,
    ],
    register: (context) => {
        const module = {
            name: Generator.toClassName('UploadModule'),
            location: Generator.toBackendModuleLocation('@upload/upload.module'),
        };
        const uploadModule = {
            name: Generator.toBackendModuleName('upload'),
            moduleClass: module,
            apiModuleRegistration: {
                code: (0, generator_1.ts)('UploadModule.forRoot(config.upload)'),
            },
            envConfig: {
                dotEnvExample: `
          # Upload storage mode: none | local | s3
          UPLOAD_STORAGE=none
          # Path for local uploads if UPLOAD_STORAGE=local
          UPLOAD_LOCAL_DIRECTORY="./tmp/uploads"
          # If true, upload binaries are not persisted to local disk/S3
          UPLOAD_DRY_RUN=true
          # Default max upload size in bytes (30 MB)
          UPLOAD_MAX_SIZE_BYTES=31457280`,
                decoder: (0, generator_1.ts)(`
          UPLOAD_STORAGE: z.enum(['none', 'local', 's3']).optional().default('none'),
          UPLOAD_LOCAL_DIRECTORY: z.string().optional().default('./tmp/uploads'),
          UPLOAD_DRY_RUN: zEnvBoolean.optional().default(true),
          UPLOAD_MAX_SIZE_BYTES: z.coerce.number().int().positive().optional().default(30 * 1024 * 1024),
        `),
                transformer: (0, generator_1.ts)(`
          upload: {
            storage: val.UPLOAD_STORAGE,
            localDirectory: val.UPLOAD_LOCAL_DIRECTORY,
            dryRun: val.UPLOAD_DRY_RUN,
            maxSizeBytes: val.UPLOAD_MAX_SIZE_BYTES,
          }`),
            },
        };
        context.backend.modules.push(uploadModule);
        context.backend.packageJson.dependencies.push({ packageName: '@fastify/multipart', version: '9.4.0' });
        return {
            ...context,
            upload: { module },
        };
    },
    generate: async (context) => {
        const vfs = new Generator.VirtualFileSystem();
        await vfs.loadFolder({
            diskPath: path.resolve(__dirname, './template'),
        });
        context.vfs.insertFromVfs({ targetPath: '/backend/libs/upload', vfs });
        return context;
    },
};
//# sourceMappingURL=upload.generator.js.map