#!/usr/bin/env node
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.OutputType = exports.TaskType = void 0;
const CartoApp_1 = require("../app/CartoApp");
const Project_1 = require("../app/Project");
const Status_1 = require("../app/Status");
const Log_1 = require("../core/Log");
const LocalEnvironment_1 = require("../local/LocalEnvironment");
const NodeStorage_1 = require("../local/NodeStorage");
const Database_1 = require("../minecraft/Database");
const StorageUtilities_1 = require("../storage/StorageUtilities");
var TaskType;
(function (TaskType) {
    TaskType[TaskType["noCommand"] = 0] = "noCommand";
    TaskType[TaskType["info"] = 7] = "info";
    TaskType[TaskType["add"] = 8] = "add";
    TaskType[TaskType["create"] = 9] = "create";
    TaskType[TaskType["validate"] = 10] = "validate";
    TaskType[TaskType["version"] = 12] = "version";
    TaskType[TaskType["world"] = 18] = "world";
    TaskType[TaskType["aggregateReports"] = 22] = "aggregateReports";
})(TaskType = exports.TaskType || (exports.TaskType = {}));
var OutputType;
(function (OutputType) {
    OutputType[OutputType["normal"] = 0] = "normal";
    OutputType[OutputType["noReports"] = 1] = "noReports";
})(OutputType = exports.OutputType || (exports.OutputType = {}));
class ClUtils {
    static createProject(carto, startInfo) {
        const proj = new Project_1.default(carto, startInfo.ctorProjectName, null);
        if (startInfo.localFilePath) {
            proj.localFilePath = startInfo.localFilePath;
        }
        if (startInfo.localFolderPath) {
            proj.localFolderPath = startInfo.localFolderPath;
        }
        if (startInfo.accessoryFiles) {
            proj.accessoryFilePaths = startInfo.accessoryFiles;
        }
        proj.autoDeploymentMode = Project_1.ProjectAutoDeploymentMode.noAutoDeployment;
        return proj;
    }
    static handleStatusAdded(carto, status) {
        let message = status.message;
        if (status.type === Status_1.StatusType.operationStarted) {
            message =
                LocalEnvironment_1.OperationColors[(status.operationId ? status.operationId : 0) % LocalEnvironment_1.OperationColors.length] +
                    "[[ START: " +
                    message +
                    LocalEnvironment_1.consoleText_reset;
        }
        else if (status.type === Status_1.StatusType.operationEndedComplete) {
            message =
                LocalEnvironment_1.OperationColors[(status.operationId ? status.operationId : 0) % LocalEnvironment_1.OperationColors.length] +
                    "          " +
                    message +
                    " :END ]]" +
                    LocalEnvironment_1.consoleText_reset;
        }
        else if (status.type === Status_1.StatusType.operationEndedErrors) {
            message =
                LocalEnvironment_1.OperationColors[(status.operationId ? status.operationId : 0) % LocalEnvironment_1.OperationColors.length] +
                    "          " +
                    message +
                    " :END - ERRORS ]]" +
                    LocalEnvironment_1.consoleText_reset;
        }
        Log_1.default.verbose(message);
    }
    static async localFolderExists(path) {
        const ls = new NodeStorage_1.default(path, "");
        return await ls.rootFolder.exists();
    }
    static async localFileExists(path) {
        const folderPath = StorageUtilities_1.default.getFolderPath(path);
        const fileName = StorageUtilities_1.default.getLeafName(path);
        if (!fileName || fileName.length < 2 || !folderPath || folderPath.length < 2) {
            throw new Error("Could not process file with path: `" + path + "`");
        }
        const ls = new NodeStorage_1.default(folderPath, "");
        const file = ls.rootFolder.ensureFile(fileName);
        return await file.exists();
    }
    static ensureLocalFolder(path) {
        const ls = new NodeStorage_1.default(path, "");
        return ls.rootFolder;
    }
    static getIsWriteCommand(taskType) {
        return taskType === TaskType.world || taskType === TaskType.create || taskType === TaskType.add;
    }
    static async getMainWorkFolder(taskType, inputFolder, outputFolder) {
        // console.log("Using local path: '" + inputFolder + "' from '" + __dirname + "'");
        let workFolder;
        if (!inputFolder && outputFolder && ClUtils.getIsWriteCommand(taskType)) {
            const outputStorage = new NodeStorage_1.default(outputFolder, "");
            workFolder = outputStorage.rootFolder;
            await workFolder.ensureExists();
        }
        if (!workFolder) {
            if (!inputFolder) {
                inputFolder = process.cwd();
            }
            const inputStorage = new NodeStorage_1.default(inputFolder, "");
            workFolder = inputStorage.rootFolder;
        }
        const exists = await workFolder.exists();
        if (!exists) {
            throw new Error("Specified folder path '" + workFolder.fullPath + "' does not exist within '" + __dirname + "'.");
        }
        await workFolder.load();
        return workFolder;
    }
    static getCarto(localEnv) {
        CartoApp_1.default.localFolderExists = ClUtils.localFolderExists;
        CartoApp_1.default.localFileExists = ClUtils.localFileExists;
        CartoApp_1.default.ensureLocalFolder = ClUtils.ensureLocalFolder;
        CartoApp_1.default.prefsStorage = new NodeStorage_1.default(localEnv.utilities.cliWorkingPath + "prefs" + NodeStorage_1.default.platformFolderDelimiter, "");
        CartoApp_1.default.projectsStorage = new NodeStorage_1.default(localEnv.utilities.cliWorkingPath + "projects" + NodeStorage_1.default.platformFolderDelimiter, "");
        CartoApp_1.default.packStorage = new NodeStorage_1.default(localEnv.utilities.cliWorkingPath + "packs" + NodeStorage_1.default.platformFolderDelimiter, "");
        CartoApp_1.default.deploymentStorage = new NodeStorage_1.default(localEnv.utilities.cliWorkingPath + "deployment" + NodeStorage_1.default.platformFolderDelimiter, "");
        CartoApp_1.default.workingStorage = new NodeStorage_1.default(localEnv.utilities.cliWorkingPath + "working" + NodeStorage_1.default.platformFolderDelimiter, "");
        const coreStorage = new NodeStorage_1.default(__dirname + "/../data/content/", "");
        Database_1.default.contentFolder = coreStorage.rootFolder;
        Database_1.default.local = localEnv.utilities;
        CartoApp_1.default.init();
        if (CartoApp_1.default.carto) {
            CartoApp_1.default.carto.local = localEnv.utilities;
        }
        return CartoApp_1.default.carto;
    }
}
exports.default = ClUtils;

//# sourceMappingURL=../maps/cli/ClUtils.js.map
