"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
var __generator = (this && this.__generator) || function (thisArg, body) {
    var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
    return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
    function verb(n) { return function (v) { return step([n, v]); }; }
    function step(op) {
        if (f) throw new TypeError("Generator is already executing.");
        while (g && (g = 0, op[0] && (_ = 0)), _) try {
            if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
            if (y = 0, t) op = [op[0] & 2, t.value];
            switch (op[0]) {
                case 0: case 1: t = op; break;
                case 4: _.label++; return { value: op[1], done: false };
                case 5: _.label++; y = op[1]; op = [0]; continue;
                case 7: op = _.ops.pop(); _.trys.pop(); continue;
                default:
                    if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
                    if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
                    if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
                    if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
                    if (t[2]) _.ops.pop();
                    _.trys.pop(); continue;
            }
            op = body.call(thisArg, _);
        } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
        if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
    }
};
Object.defineProperty(exports, "__esModule", { value: true });
var flag_1 = require("../tools/flag");
var generators_1 = require("../tools/generators");
var pretty_1 = require("../tools/pretty");
var SUB_DIR_DELIMITER = "/";
module.exports = {
    alias: ["g", "generator", "generators"],
    description: "Generates components and other features from templates",
    run: function (toolbox) { return __awaiter(void 0, void 0, void 0, function () {
        var generator;
        var _a;
        return __generator(this, function (_b) {
            generator = (_a = toolbox.parameters.first) === null || _a === void 0 ? void 0 : _a.toLowerCase();
            (0, generators_1.runGenerator)(toolbox, generate, generator);
            return [2 /*return*/];
        });
    }); },
};
function generate(toolbox) {
    return __awaiter(this, void 0, void 0, function () {
        var parameters, strings, generator, name, subdirectory, lastSlashIndex, pascalGenerator, pascalName, options, defaultOverwrite, overwrite, _a, written, overwritten, exists;
        return __generator(this, function (_b) {
            switch (_b.label) {
                case 0:
                    parameters = toolbox.parameters, strings = toolbox.strings;
                    generator = parameters.first.toLowerCase();
                    name = parameters.second;
                    if (!name) {
                        (0, pretty_1.warning)("\u26A0\uFE0F  Please specify a name for your ".concat(generator, ":"));
                        (0, pretty_1.p)();
                        (0, pretty_1.command)("ignite g ".concat(generator, " MyName"));
                        return [2 /*return*/];
                    }
                    subdirectory = "";
                    if (name.indexOf(SUB_DIR_DELIMITER) > -1) {
                        lastSlashIndex = name.lastIndexOf(SUB_DIR_DELIMITER);
                        subdirectory = name.substring(0, lastSlashIndex + 1);
                        name = name.substring(lastSlashIndex + 1);
                    }
                    pascalGenerator = strings.pascalCase(generator);
                    pascalName = strings.pascalCase(name);
                    if (pascalName.endsWith(pascalGenerator)) {
                        (0, pretty_1.p)("Stripping ".concat(pascalGenerator, " from end of name"));
                        (0, pretty_1.p)("Note that you don't need to add ".concat(pascalGenerator, " to the end of the name -- we'll do it for you!"));
                        pascalName = pascalName.slice(0, -1 * pascalGenerator.length);
                        (0, pretty_1.command)("ignite generate ".concat(generator, " ").concat(pascalName));
                    }
                    // okay, let's do it!
                    (0, pretty_1.p)();
                    options = parameters.options;
                    defaultOverwrite = false;
                    overwrite = !options.overwrite ? defaultOverwrite : (0, flag_1.boolFlag)(options.overwrite);
                    return [4 /*yield*/, (0, generators_1.generateFromTemplate)(generator, {
                            name: pascalName,
                            skipIndexFile: parameters.options.skipIndexFile,
                            overwrite: overwrite,
                            subdirectory: subdirectory,
                        })];
                case 1:
                    _a = _b.sent(), written = _a.written, overwritten = _a.overwritten, exists = _a.exists;
                    (0, pretty_1.heading)("Generated new files:");
                    if (exists.length > 0) {
                        if (written.length > 0) {
                            written.forEach(function (f) { return (0, pretty_1.p)(f); });
                        }
                        else
                            (0, pretty_1.p)("<none>");
                        (0, pretty_1.p)();
                        (0, pretty_1.heading)("Skipped these files because they already exist:");
                        exists.forEach(function (f) { return (0, pretty_1.p)(f); });
                        (0, pretty_1.p)();
                        (0, pretty_1.heading)("To overwrite these files, run the command again with the `--overwrite` flag");
                    }
                    else if (overwritten.length > 0) {
                        overwritten.forEach(function (f) { return (0, pretty_1.p)(f); });
                    }
                    else {
                        written.forEach(function (f) { return (0, pretty_1.p)(f); });
                    }
                    return [2 /*return*/];
            }
        });
    });
}
//# sourceMappingURL=generate.js.map