"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
/*
 * Copyright (c) 2026, 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 crypto = require("node:crypto");
const path = require("path");
const utils_1 = require("../utils");
const baseGenerator_1 = require("./baseGenerator");
const i18n_1 = require("../i18n");
const TEMPLATE_ROOT = 'digitalexperiencesite';
class DigitalExperienceSiteGenerator extends baseGenerator_1.BaseGenerator {
    constructor(options) {
        super(options);
        this.uuidCache = {};
    }
    validateOptions() {
        utils_1.CreateUtil.checkInputs(this.options.template);
        // site name allows anything, but we will strip it down for dev name
        // url path prefix must be strictly alphanumeric, no "-" or "_", can start with a number
        // empty string is allowed (for sites without a prefix)
        const alphaRegExp = /^[a-zA-Z0-9]*$/;
        if (!alphaRegExp.test(this.options.urlpathprefix)) {
            throw new Error(i18n_1.nls.localize('AlphaNumericValidationError', 'url-path-prefix'));
        }
    }
    generate() {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            const { template, sitename, urlpathprefix, adminemail } = this.options;
            const siteDevName = this.toSiteDevName(sitename);
            const picassoSiteDevName = this.toPicassoSiteDevName(siteDevName);
            this.sourceRootWithPartialPath(path.join(TEMPLATE_ROOT, template));
            yield this.generateNetwork(sitename, siteDevName, picassoSiteDevName, urlpathprefix, adminemail);
            yield this.generateCustomSite(sitename, siteDevName, urlpathprefix);
            yield this.generateDigitalExperienceConfig(sitename, picassoSiteDevName, urlpathprefix);
            const bundlePath = path.join(this.outputdir, 'digitalExperiences', 'site', picassoSiteDevName);
            yield this.generateDEBMeta(bundlePath, picassoSiteDevName);
            yield this.generateDEBAppPage(bundlePath);
            yield this.generateDEBBrandingSet(bundlePath);
            yield this.generateDEBLanguageSettings(bundlePath);
            yield this.generateDEBMobilePublisherConfig(bundlePath);
            yield this.generateDEBRoutes(bundlePath);
            yield this.generateDEBSite(bundlePath, sitename, picassoSiteDevName);
            yield this.generateDEBTheme(bundlePath);
            yield this.generateDEBThemeLayouts(bundlePath);
            yield this.generateDEBViews(bundlePath);
        });
    }
    generateNetwork(siteName, siteDevName, picassoSiteDevName, urlPathPrefix, adminEmail) {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            const fileName = this.encodeForFileName(siteName);
            yield this.render(this.templatePath('_network.xml'), this.destinationPath(path.join(this.outputdir, 'networks', `${fileName}.network-meta.xml`)), { siteName, siteDevName, picassoSiteDevName, urlPathPrefix, adminEmail });
        });
    }
    generateCustomSite(siteName, siteDevName, urlPathPrefix) {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            yield this.render(this.templatePath('_customSite.xml'), this.destinationPath(path.join(this.outputdir, 'sites', `${siteDevName}.site-meta.xml`)), { siteName, siteDevName, urlPathPrefix });
        });
    }
    generateDigitalExperienceConfig(siteName, picassoSiteDevName, urlPathPrefix) {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            yield this.render(this.templatePath('_digitalExperienceConfig.xml'), this.destinationPath(path.join(this.outputdir, 'digitalExperienceConfigs', `${picassoSiteDevName}.digitalExperienceConfig-meta.xml`)), { siteName, picassoSiteDevName, urlPathPrefix });
        });
    }
    generateDEBMeta(bundlePath, picassoSiteDevName) {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            yield this.render(this.templatePath('_digitalExperience.xml'), this.destinationPath(path.join(bundlePath, `${picassoSiteDevName}.digitalExperience-meta.xml`)), { picassoSiteDevName });
        });
    }
    generateDEBAppPage(bundlePath) {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            const appPagePath = path.join(bundlePath, 'sfdc_cms__appPage', 'mainAppPage');
            yield this.render(this.templatePath('sfdc_cms__appPage', 'mainAppPage', 'content.json'), this.destinationPath(path.join(appPagePath, 'content.json')), {});
            yield this.render(this.templatePath('sfdc_cms__appPage', 'mainAppPage', '_meta.json'), this.destinationPath(path.join(appPagePath, '_meta.json')), {});
        });
    }
    generateDEBBrandingSet(bundlePath) {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            const brandingPath = path.join(bundlePath, 'sfdc_cms__brandingSet', 'Build_Your_Own_LWR');
            yield this.render(this.templatePath('sfdc_cms__brandingSet', 'Build_Your_Own_LWR', 'content.json'), this.destinationPath(path.join(brandingPath, 'content.json')), {});
            yield this.render(this.templatePath('sfdc_cms__brandingSet', 'Build_Your_Own_LWR', '_meta.json'), this.destinationPath(path.join(brandingPath, '_meta.json')), {});
        });
    }
    generateDEBLanguageSettings(bundlePath) {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            const langPath = path.join(bundlePath, 'sfdc_cms__languageSettings', 'languages');
            yield this.render(this.templatePath('sfdc_cms__languageSettings', 'languages', 'content.json'), this.destinationPath(path.join(langPath, 'content.json')), {});
            yield this.render(this.templatePath('sfdc_cms__languageSettings', 'languages', '_meta.json'), this.destinationPath(path.join(langPath, '_meta.json')), {});
        });
    }
    generateDEBMobilePublisherConfig(bundlePath) {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            const mobileConfigPath = path.join(bundlePath, 'sfdc_cms__mobilePublisherConfig', 'mobilePublisherConfig');
            yield this.render(this.templatePath('sfdc_cms__mobilePublisherConfig', 'mobilePublisherConfig', 'content.json'), this.destinationPath(path.join(mobileConfigPath, 'content.json')), {});
            yield this.render(this.templatePath('sfdc_cms__mobilePublisherConfig', 'mobilePublisherConfig', '_meta.json'), this.destinationPath(path.join(mobileConfigPath, '_meta.json')), {});
        });
    }
    generateDEBRoutes(bundlePath) {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            const routes = [
                'Check_Password',
                'Error',
                'Forgot_Password',
                'Home',
                'Login',
                'News_Detail__c',
                'Register',
                'Service_Not_Available',
                'Too_Many_Requests',
            ];
            for (const route of routes) {
                const routePath = path.join(bundlePath, 'sfdc_cms__route', route);
                yield this.render(this.templatePath('sfdc_cms__route', route, 'content.json'), this.destinationPath(path.join(routePath, 'content.json')), {});
                yield this.render(this.templatePath('sfdc_cms__route', route, '_meta.json'), this.destinationPath(path.join(routePath, '_meta.json')), {});
            }
        });
    }
    generateDEBSite(bundlePath, siteName, picassoSiteDevName) {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            const urlName = siteName
                .toLowerCase()
                .replace(/[^a-z0-9]+/g, '-') // keep only alphanumeric characters
                .replace(/-$/, ''); // remove trailing hyphen
            const sitePath = path.join(bundlePath, 'sfdc_cms__site', picassoSiteDevName);
            siteName = JSON.stringify(siteName) // escape special characters since this needs to be a JSON string
                .slice(1, -1); // remove quotes added by JSON.stringify
            yield this.render(this.templatePath('sfdc_cms__site', 'content.json'), this.destinationPath(path.join(sitePath, 'content.json')), { siteName, urlName });
            yield this.render(this.templatePath('sfdc_cms__site', '_meta.json'), this.destinationPath(path.join(sitePath, '_meta.json')), { picassoSiteDevName });
        });
    }
    generateDEBTheme(bundlePath) {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            const themePath = path.join(bundlePath, 'sfdc_cms__theme', 'Build_Your_Own_LWR');
            yield this.render(this.templatePath('sfdc_cms__theme', 'Build_Your_Own_LWR', 'content.json'), this.destinationPath(path.join(themePath, 'content.json')), {});
            yield this.render(this.templatePath('sfdc_cms__theme', 'Build_Your_Own_LWR', '_meta.json'), this.destinationPath(path.join(themePath, '_meta.json')), {});
        });
    }
    generateDEBThemeLayouts(bundlePath) {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            const layouts = ['scopedHeaderAndFooter', 'snaThemeLayout'];
            for (const layout of layouts) {
                const layoutPath = path.join(bundlePath, 'sfdc_cms__themeLayout', layout);
                yield this.render(this.templatePath('sfdc_cms__themeLayout', layout, 'content.json'), this.destinationPath(path.join(layoutPath, 'content.json')), { uuid: this.generateUUID.bind(this, `themeLayout_${layout}`) });
                yield this.render(this.templatePath('sfdc_cms__themeLayout', layout, '_meta.json'), this.destinationPath(path.join(layoutPath, '_meta.json')), {});
            }
        });
    }
    generateDEBViews(bundlePath) {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            const views = [
                'checkPasswordResetEmail',
                'error',
                'forgotPassword',
                'home',
                'login',
                'newsDetail',
                'register',
                'serviceNotAvailable',
                'tooManyRequests',
            ];
            for (const view of views) {
                const viewPath = path.join(bundlePath, 'sfdc_cms__view', view);
                yield this.render(this.templatePath('sfdc_cms__view', view, 'content.json'), this.destinationPath(path.join(viewPath, 'content.json')), { uuid: this.generateUUID.bind(this, `view_${view}`) });
                yield this.render(this.templatePath('sfdc_cms__view', view, '_meta.json'), this.destinationPath(path.join(viewPath, '_meta.json')), {});
            }
        });
    }
    generateUUID(namepsace, key) {
        if (!key) {
            return crypto.randomUUID();
        }
        key = `${namepsace}:${key}`;
        if (!this.uuidCache[key]) {
            this.uuidCache[key] = crypto.randomUUID();
        }
        return this.uuidCache[key];
    }
    toSiteDevName(sitename) {
        return sitename
            .replace(/[^a-zA-Z0-9]+/g, '_') // remove non-alphanumeric characters
            .replace(/_$/, '') // remove trailing underscore
            .replace(/^([0-9])/, 'X$1'); // prefix with X if it starts with a number
    }
    toPicassoSiteDevName(siteDevName) {
        return `${siteDevName}1`;
    }
    encodeForFileName(str) {
        const charMap = {
            '~': '%7E',
            '!': '%21',
            '.': '%2E',
            "'": '%27',
            '(': '%28',
            ')': '%29',
        };
        // encodeURIComponent is for URL, so we need additional steps to match filename
        // encoding from the server
        return encodeURIComponent(str)
            .replace(/%20/g, ' ')
            .replace(/[~!.'()]/g, (char) => charMap[char]);
    }
}
exports.default = DigitalExperienceSiteGenerator;
//# sourceMappingURL=digitalExperienceSiteGenerator.js.map