"use strict";
/*
 * Copyright (c) 2019, 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
 */
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const kit_1 = require("@salesforce/kit");
const path = require("path");
const i18n_1 = require("../i18n");
const utils_1 = require("../utils");
const baseGenerator_1 = require("./baseGenerator");
class LightningComponentGenerator extends baseGenerator_1.BaseGenerator {
    validateOptions() {
        utils_1.CreateUtil.checkInputs(this.options.componentname);
        utils_1.CreateUtil.checkInputs(this.options.template);
        const fileparts = path.resolve(this.outputdir).split(path.sep);
        if (!this.options.internal) {
            if (this.options.type === 'lwc' && !fileparts.includes('lwc')) {
                throw new Error(i18n_1.nls.localize('MissingLWCDir'));
            }
            else if (!fileparts.includes('aura') && this.options.type === 'aura') {
                throw new Error(i18n_1.nls.localize('MissingAuraDir'));
            }
        }
        if (utils_1.CreateUtil.getCommandTemplatesInSubdirs('lightningcomponent', { subdir: this.options.type }, this._fs, this.templatesRootPath).indexOf(this.options.template) < 0) {
            throw new Error(i18n_1.nls.localize('MissingLightningComponentTemplate', [
                this.options.template,
                this.options.type,
            ]));
        }
    }
    generate() {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            const { template, componentname, type, internal } = this.options;
            if (type === 'aura') {
                this.sourceRootWithPartialPath(path.join('lightningcomponent', 'aura', template));
                if (!internal) {
                    yield this.render(this.templatePath(`${template}.cmp-meta.xml`), this.destinationPath(path.join(this.outputdir, componentname, `${componentname}.cmp-meta.xml`)), {
                        componentname,
                        description: i18n_1.nls.localize('LightningComponentBundle'),
                        apiVersion: this.apiversion,
                    });
                }
                yield this.render(this.templatePath(`${template}.auradoc`), this.destinationPath(path.join(this.outputdir, componentname, `${componentname}.auradoc`)), {});
                yield this.render(this.templatePath(`${template}.cmp`), this.destinationPath(path.join(this.outputdir, componentname, `${componentname}.cmp`)), {});
                yield this.render(this.templatePath(`${template}.css`), this.destinationPath(path.join(this.outputdir, componentname, `${componentname}.css`)), {});
                yield this.render(this.templatePath(`${template}.design`), this.destinationPath(path.join(this.outputdir, componentname, `${componentname}.design`)), {});
                yield this.render(this.templatePath(`${template}.svg`), this.destinationPath(path.join(this.outputdir, componentname, `${componentname}.svg`)), {});
                yield this.render(this.templatePath(`${template}Controller.js`), this.destinationPath(path.join(this.outputdir, componentname, `${componentname}Controller.js`)), {});
                yield this.render(this.templatePath(`${template}Helper.js`), this.destinationPath(path.join(this.outputdir, componentname, `${componentname}Helper.js`)), {});
                yield this.render(this.templatePath(`${template}Renderer.js`), this.destinationPath(path.join(this.outputdir, componentname, `${componentname}Renderer.js`)), {});
            }
            if (type === 'lwc') {
                const pascalCaseComponentName = `${componentname
                    .substring(0, 1)
                    .toUpperCase()}${componentname.substring(1)}`;
                const camelCaseComponentName = `${componentname
                    .substring(0, 1)
                    .toLowerCase()}${componentname.substring(1)}`;
                const kebabCaseComponentName = componentname
                    .match(/[A-Z]{2,}(?=[A-Z][a-z]+[0-9]*|\b)|[A-Z]?[a-z]+[0-9]*|[A-Z]|[0-9]+/g)
                    .join('-')
                    .toLowerCase();
                const ext = template === 'typeScript' ? 'ts' : 'js';
                this.sourceRootWithPartialPath(path.join('lightningcomponent', 'lwc', template));
                yield this.render(this.templatePath(`${template}.${ext}`), this.destinationPath(path.join(this.outputdir, camelCaseComponentName, `${camelCaseComponentName}.${ext}`)), { pascalCaseComponentName });
                yield this.render(this.templatePath(`${template}.html`), this.destinationPath(path.join(this.outputdir, camelCaseComponentName, `${camelCaseComponentName}.html`)), {});
                yield this.render(this.templatePath(path.join(`__tests__`, `${template}.test.${ext}`)), this.destinationPath(path.join(this.outputdir, camelCaseComponentName, `__tests__`, `${camelCaseComponentName}.test.${ext}`)), {
                    pascalCaseComponentName,
                    camelCaseComponentName,
                    kebabCaseComponentName,
                });
                if (template === 'typeScript') {
                    yield this.render(this.templatePath('gitignore'), this.destinationPath(path.join(this.outputdir, camelCaseComponentName, '.gitignore')), {});
                }
                if (!internal) {
                    const masterLabel = (0, kit_1.camelCaseToTitleCase)(componentname)
                        .replace(/</g, '&lt;')
                        .replace(/>/g, '&gt;');
                    yield this.render(this.templatePath(`${template}.js-meta.xml`), this.destinationPath(path.join(this.outputdir, camelCaseComponentName, `${camelCaseComponentName}.js-meta.xml`)), { apiVersion: this.apiversion, masterLabel });
                }
            }
        });
    }
}
exports.default = LightningComponentGenerator;
//# sourceMappingURL=lightningComponentGenerator.js.map