"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
/*
 * Copyright (c) 2025, salesforce.com, inc.
 * All rights reserved.
 * Licensed under the BSD 3-Clause license.
 * For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
 */
const kit_1 = require("@salesforce/kit");
const path = require("path");
const utils_1 = require("../utils");
const baseGenerator_1 = require("./baseGenerator");
class WebApplicationGenerator extends baseGenerator_1.BaseGenerator {
    validateOptions() {
        utils_1.CreateUtil.checkInputs(this.options.webappname);
        utils_1.CreateUtil.checkInputs(this.options.template);
        // Ensure output directory includes 'webapplications' folder
        if (!this.options.internal) {
            const fileparts = path
                .resolve(this.outputdir)
                .split(path.sep)
                .filter(Boolean);
            const endsWithWebApplications = fileparts[fileparts.length - 1] === 'webapplications';
            if (!endsWithWebApplications) {
                this.outputdir = path.join(this.outputdir, 'webapplications');
            }
        }
    }
    generate() {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            const { webappname } = this.options;
            const template = this.options.template.toLowerCase();
            const masterLabel = this.options.masterlabel || (0, kit_1.camelCaseToTitleCase)(webappname);
            const webappDir = path.join(this.outputdir, webappname);
            switch (template) {
                case 'reactbasic':
                    yield this.generateReactBasic(webappDir, webappname, masterLabel);
                    break;
                default:
                    yield this.generateDefault(webappDir, webappname, masterLabel);
            }
        });
    }
    generateDefault(webappDir, webappname, masterLabel) {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            this.sourceRootWithPartialPath(path.join('webapplication', 'webappbasic'));
            yield this.render(this.templatePath('_webapplication.webapplication-meta.xml'), this.destinationPath(path.join(webappDir, `${webappname}.webapplication-meta.xml`)), { apiVersion: this.apiversion, masterLabel });
            const templatePath = this.sourceRoot();
            yield this.copyDirectoryRecursive(templatePath, webappDir, new Set(['_webapplication.webapplication-meta.xml']));
        });
    }
    generateReactBasic(webappDir, webappname, masterLabel) {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            this.sourceRootWithPartialPath(path.join('webapplication', 'reactbasic'));
            yield this.render(this.templatePath('_webapplication.webapplication-meta.xml'), this.destinationPath(path.join(webappDir, `${webappname}.webapplication-meta.xml`)), { apiVersion: this.apiversion, masterLabel });
            yield this.render(this.templatePath('package.json'), this.destinationPath(path.join(webappDir, 'package.json')), { webappname });
            const templatePath = this.sourceRoot();
            yield this.copyDirectoryRecursive(templatePath, webappDir, new Set(['_webapplication.webapplication-meta.xml', 'package.json']));
        });
    }
    copyDirectoryRecursive(sourceDir_1, destDir_1) {
        return tslib_1.__awaiter(this, arguments, void 0, function* (sourceDir, destDir, excludeFiles = new Set()) {
            if (!this._fs.existsSync(sourceDir)) {
                return;
            }
            yield this._fs.promises.mkdir(destDir, { recursive: true });
            const entries = this._fs.readdirSync(sourceDir, { withFileTypes: true });
            for (const entry of entries) {
                const sourcePath = path.join(sourceDir, entry.name);
                const destPath = path.join(destDir, entry.name);
                // Skip template files that were rendered
                if (excludeFiles.has(entry.name)) {
                    continue;
                }
                if (entry.isDirectory()) {
                    yield this.copyDirectoryRecursive(sourcePath, destPath, excludeFiles);
                }
                else if (!this._fs.existsSync(destPath)) {
                    // Copy file and track it
                    const content = yield this._fs.promises.readFile(sourcePath);
                    yield this._fs.promises.mkdir(path.dirname(destPath), {
                        recursive: true,
                    });
                    yield this._fs.promises.writeFile(destPath, content);
                    // Register the created file
                    const relativePath = path.relative(this._cwd, destPath);
                    this.changes.created.push(relativePath);
                }
            }
        });
    }
}
exports.default = WebApplicationGenerator;
//# sourceMappingURL=webApplicationGenerator.js.map