"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 constants_1 = require("../utils/constants");
const baseGenerator_1 = require("./baseGenerator");
class UIBundleGenerator extends baseGenerator_1.BaseGenerator {
    validateOptions() {
        utils_1.CreateUtil.checkInputs(this.options.bundlename);
        utils_1.CreateUtil.checkInputs(this.options.template);
        // Ensure output directory includes 'uiBundles' folder
        if (!this.options.internal) {
            const fileparts = path
                .resolve(this.outputdir)
                .split(path.sep)
                .filter(Boolean);
            const endsWithUiBundles = fileparts[fileparts.length - 1] === constants_1.UI_BUNDLES_DIR;
            if (!endsWithUiBundles) {
                this.outputdir = path.join(this.outputdir, constants_1.UI_BUNDLES_DIR);
            }
        }
    }
    generate() {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            const { bundlename } = this.options;
            const template = this.options.template.toLowerCase();
            const masterLabel = this.options.masterlabel || (0, kit_1.camelCaseToTitleCase)(bundlename);
            const bundleDir = path.join(this.outputdir, bundlename);
            switch (template) {
                case 'reactbasic':
                    yield this.generateReactBasic(bundleDir, bundlename, masterLabel);
                    break;
                default:
                    yield this.generateDefault(bundleDir, bundlename, masterLabel);
            }
        });
    }
    generateDefault(bundleDir, bundlename, masterLabel) {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            this.sourceRootWithPartialPath(path.join('uiBundles', 'webappbasic'));
            yield this.render(this.templatePath('_uibundle.uibundle-meta.xml'), this.destinationPath(path.join(bundleDir, `${bundlename}.uibundle-meta.xml`)), { apiVersion: this.apiversion, masterLabel });
            const templatePath = this.sourceRoot();
            yield this.copyDirectoryRecursive(templatePath, bundleDir, new Set(['_uibundle.uibundle-meta.xml']));
        });
    }
    generateReactBasic(bundleDir, bundlename, masterLabel) {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            this.sourceRootWithPartialPath(path.join('uiBundles', 'reactbasic'));
            yield this.render(this.templatePath('_uibundle.uibundle-meta.xml'), this.destinationPath(path.join(bundleDir, `${bundlename}.uibundle-meta.xml`)), { apiVersion: this.apiversion, masterLabel });
            yield this.render(this.templatePath('package.json'), this.destinationPath(path.join(bundleDir, 'package.json')), { bundlename });
            const templatePath = this.sourceRoot();
            yield this.copyDirectoryRecursive(templatePath, bundleDir, new Set(['_uibundle.uibundle-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 = UIBundleGenerator;
//# sourceMappingURL=uiBundleGenerator.js.map