"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.configureDevopsGenerator = exports.generatorId = void 0;
const Path = __importStar(require("node:path"));
const Generator = __importStar(require("@postxl/generator"));
const backend_build_sh_generator_1 = require("./generators/backend-build-sh.generator");
const backend_dockerfile_generator_1 = require("./generators/backend-dockerfile.generator");
const backend_start_api_sh_generator_1 = require("./generators/backend-start-api-sh.generator");
const bitbucket_pipelines_yml_generator_1 = require("./generators/bitbucket-pipelines-yml.generator");
const configure_keycloak_sh_generator_1 = require("./generators/configure-keycloak-sh.generator");
const docker_compose_yml_generator_1 = require("./generators/docker-compose-yml.generator");
const e2e_yml_generator_1 = require("./generators/e2e-yml.generator");
const frontend_build_sh_generator_1 = require("./generators/frontend-build-sh.generator");
const frontend_dockerfile_generator_1 = require("./generators/frontend-dockerfile.generator");
const jenkinsfile_generator_1 = require("./generators/jenkinsfile.generator");
const root_dotenv_example_generator_1 = require("./generators/root-dotenv-example.generator");
exports.generatorId = Generator.toGeneratorInterfaceId('devops');
const configureDevopsGenerator = (config) => ({
    id: exports.generatorId,
    register: (context) => {
        return {
            ...context,
            devOps: {
                database: config?.useDatabase
                    ? {
                        connectionString: `postgresql://postgres:postgres@localhost:5432/${context.schema.slug}`,
                        username: 'postgres',
                        password: 'postgres', // NOSONAR: only for local development
                        database: `${context.schema.slug}`,
                    }
                    : undefined,
                keycloak: config?.keycloakComponents
                    ? {
                        components: config.keycloakComponents,
                        host: 'http://localhost:8080',
                        pathPrefix: '/auth',
                        clientId: `${context.schema.slug}-client`,
                        clientSecret: 'secret',
                        realm: `${context.schema.slug}`,
                    }
                    : undefined,
                useBitbucket: config?.useBitbucket ?? false,
                useS3: config?.useS3 ?? false,
            },
        };
    },
    generate: async (context) => {
        const templatePath = Path.join(__dirname, './template');
        const copyToRelativeLocation = (path) => {
            const diskPath = Path.join(templatePath, path);
            const targetPath = Path.join('', path);
            return { diskPath, targetPath };
        };
        await context.vfs.loadFolder(copyToRelativeLocation('.github'));
        await context.vfs.loadFolder(copyToRelativeLocation('docker/frontend'));
        if (context.devOps.keycloak) {
            await context.vfs.loadFile(copyToRelativeLocation('docker/keycloak/Dockerfile'));
            if (context.devOps.keycloak.components.includes('userAttributes')) {
                await context.vfs.loadFile(copyToRelativeLocation('docker/keycloak/user-attributes.json'));
                await context.vfs.loadFile(copyToRelativeLocation('docker/keycloak/protocol-mappers.json'));
            }
            if (context.devOps.keycloak.components.includes('toc')) {
                await context.vfs.loadFile(copyToRelativeLocation('docker/keycloak/localization.en.json'));
            }
            context.vfs.write('/docker/keycloak/configure-keycloak.sh', (0, configure_keycloak_sh_generator_1.generateConfigureKeycloakSh)(context.devOps));
        }
        if (context.devOps.database) {
            await context.vfs.loadFile(copyToRelativeLocation('docker/backend/migrate.sh'));
        }
        // write dynamic files
        if (context.devOps.keycloak || context.devOps.database) {
            context.vfs.write('docker-compose.yml', (0, docker_compose_yml_generator_1.generateDockerComposeYaml)(context.devOps));
        }
        context.vfs.write('/.github/workflows/e2e.yml', (0, e2e_yml_generator_1.generateE2EYaml)(context));
        context.vfs.write('/docker/backend/Dockerfile', (0, backend_dockerfile_generator_1.generateBackendDockerfile)(context));
        context.vfs.write('/docker/backend/start.api.sh', (0, backend_start_api_sh_generator_1.generateBackendStartApiSh)(context));
        context.vfs.write('/docker/backend/build.sh', (0, backend_build_sh_generator_1.generateBackendBuildSh)(context));
        context.vfs.write('/docker/frontend/Dockerfile', (0, frontend_dockerfile_generator_1.generateFrontendDockerfile)(context));
        context.vfs.write('/docker/frontend/build.sh', (0, frontend_build_sh_generator_1.generateFrontendBuildSh)(context));
        if (context.devOps.useBitbucket) {
            context.vfs.write('bitbucket-pipelines.yml', (0, bitbucket_pipelines_yml_generator_1.generateBitbucketPipelinesYml)(context));
        }
        context.vfs.write('Jenkinsfile', (0, jenkinsfile_generator_1.generateJenkinsfile)(context));
        const env = (0, root_dotenv_example_generator_1.generateDotEnvExample)(context.devOps);
        context.vfs.write('.env.example', env);
        // We write the .env file so that the project can get started without the need for the developer to
        // copy the .env.example file. The .env file is not committed to the repository as it is part of the
        // .gitignore file.
        context.vfs.write('.env', env);
        return context;
    },
});
exports.configureDevopsGenerator = configureDevopsGenerator;
//# sourceMappingURL=devops.generator.js.map