"use strict";
/*
 * Copyright (c) 2020, 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 });
exports.TemplateService = void 0;
exports.importGenerator = importGenerator;
const types_1 = require("../utils/types");
const i18n_1 = require("../i18n");
function importGenerator(templateType) {
    const generator = types_1.generators.get(templateType);
    if (!generator) {
        throw new Error(i18n_1.nls.localize('templateTypeNotFound'));
    }
    return generator;
}
/**
 * Template Service
 */
class TemplateService {
    constructor(cwd, context) {
        this._cwd = cwd !== null && cwd !== void 0 ? cwd : process.cwd();
        this._context = context;
    }
    /**
     * Get an instance of TemplateService
     * @param cwd cwd of current environment. CLI: don't need to set explicitly. VS Code: it's typically the root workspace path
     * @param context optional generator context with fs and templatesRootPath
     */
    static getInstance(cwd, context) {
        if (!TemplateService.instance) {
            TemplateService.instance = new TemplateService(cwd, context);
        }
        else {
            if (cwd) {
                TemplateService.instance.cwd = cwd;
            }
            if (context) {
                TemplateService.instance._context = context;
            }
        }
        return TemplateService.instance;
    }
    /**
     * Getting cwd of current environment
     */
    get cwd() {
        return this._cwd;
    }
    /**
     * Setting cwd of current environment
     * In VS Code, it's typically the root workspace path
     */
    set cwd(cwd) {
        this._cwd = cwd;
    }
    /**
     * Create using templates
     * @param templateType template type
     * @param templateOptions template options
     * @param customTemplatesRootPathOrGitRepo custom templates root path or git repo. If not specified, use built-in templates
     */
    create(templateType, templateOptions, customTemplatesRootPathOrGitRepo) {
        const runOptions = {
            cwd: this.cwd,
            customTemplatesRootPathOrGitRepo,
        };
        const Generator = importGenerator(templateType);
        const instance = new Generator(templateOptions, this._context, this._cwd);
        return instance.run(runOptions);
    }
}
exports.TemplateService = TemplateService;
//# sourceMappingURL=templateService.js.map