"use strict";
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
Object.defineProperty(exports, "__esModule", { value: true });
const ProjectInfoItem_1 = require("../info/ProjectInfoItem");
const IProjectItemData_1 = require("../app/IProjectItemData");
const IInfoItemData_1 = require("../info/IInfoItemData");
const ProjectUpdateResult_1 = require("../updates/ProjectUpdateResult");
const VsCodeTasksDefinition_1 = require("../devproject/VsCodeTasksDefinition");
const IUpdateResult_1 = require("../updates/IUpdateResult");
const VsCodeLaunchDefinition_1 = require("../devproject/VsCodeLaunchDefinition");
class VsCodeFileManager {
    id = "VSCODEFILE";
    title = "VSCode Files";
    getTopicData(topicId) {
        return {
            title: topicId.toString(),
        };
    }
    getUpdaterData(updateId) {
        return {
            title: updateId.toString(),
        };
    }
    summarize(info, infoSet) { }
    async generate(project, contentIndex) {
        const infoItems = [];
        const itemsCopy = project.getItemsCopy();
        for (const pi of itemsCopy) {
            if (pi.itemType === IProjectItemData_1.ProjectItemType.vsCodeTasksJson && pi.storageType === IProjectItemData_1.ProjectItemStorageType.singleFile) {
                await pi.ensureFileStorage();
                if (pi.primaryFile) {
                    const vscodeTasksJson = await VsCodeTasksDefinition_1.default.ensureOnFile(pi.primaryFile);
                    if (vscodeTasksJson) {
                        const hasMinecraftTasks = await vscodeTasksJson.hasMinContent();
                        if (!hasMinecraftTasks) {
                            infoItems.push(new ProjectInfoItem_1.default(IInfoItemData_1.InfoItemType.info, this.id, 100, "Project has a VSCode tasks file, but no minecraft deploy tasks.", pi, undefined, pi.primaryFile.storageRelativePath));
                        }
                    }
                }
            }
            else if (pi.itemType === IProjectItemData_1.ProjectItemType.vsCodeLaunchJson &&
                pi.storageType === IProjectItemData_1.ProjectItemStorageType.singleFile) {
                await pi.ensureFileStorage();
                if (pi.primaryFile) {
                    const vscodeLaunchJson = await VsCodeLaunchDefinition_1.default.ensureOnFile(pi.primaryFile);
                    if (vscodeLaunchJson) {
                        vscodeLaunchJson.project = project;
                        const hasMinecraftDebugConfig = await vscodeLaunchJson.hasMinContent({ isServer: true });
                        if (!hasMinecraftDebugConfig) {
                            infoItems.push(new ProjectInfoItem_1.default(IInfoItemData_1.InfoItemType.info, this.id, 101, "Project has a VSCode launch file, but is not configured for Minecraft server launch.", pi, undefined, pi.primaryFile.storageRelativePath));
                        }
                    }
                }
            }
        }
        return infoItems;
    }
    async update(project, updateId) {
        const results = [];
        switch (updateId) {
            case 1:
                results.push(...(await this.ensureMinecraftLaunchTasks(project)));
                break;
            case 2:
                results.push(...(await this.ensureMinecraftDebugConfig(project)));
                break;
        }
        return results;
    }
    getUpdateIds() {
        return [1, 2];
    }
    async ensureMinecraftLaunchTasks(project) {
        const results = [];
        const itemsCopy = project.getItemsCopy();
        for (const pi of itemsCopy) {
            if (pi.itemType === IProjectItemData_1.ProjectItemType.vsCodeTasksJson && pi.storageType === IProjectItemData_1.ProjectItemStorageType.singleFile) {
                await pi.ensureFileStorage();
                if (pi.primaryFile) {
                    const vscodeTasksJson = await VsCodeTasksDefinition_1.default.ensureOnFile(pi.primaryFile);
                    if (vscodeTasksJson) {
                        const hasTasks = await vscodeTasksJson.hasMinContent();
                        if (!hasTasks) {
                            const result = await vscodeTasksJson.ensureMinContent();
                            if (result) {
                                await vscodeTasksJson.save();
                                results.push(new ProjectUpdateResult_1.default(IUpdateResult_1.UpdateResultType.updatedFile, this.id, 1, "Updated Minecraft Tasks", pi));
                            }
                        }
                    }
                }
            }
        }
        return results;
    }
    async ensureMinecraftDebugConfig(project) {
        const results = [];
        const itemsCopy = project.getItemsCopy();
        for (const pi of itemsCopy) {
            if (pi.itemType === IProjectItemData_1.ProjectItemType.vsCodeLaunchJson && pi.storageType === IProjectItemData_1.ProjectItemStorageType.singleFile) {
                await pi.ensureFileStorage();
                if (pi.primaryFile) {
                    const vscodeLaunchJson = await VsCodeLaunchDefinition_1.default.ensureOnFile(pi.primaryFile);
                    const pack = await project.getDefaultBehaviorPack();
                    const debugSettings = { isServer: true };
                    if (pack && pack.folder) {
                        debugSettings.behaviorPackFolderName = pack.folder.name;
                    }
                    if (vscodeLaunchJson) {
                        const hasConfig = await vscodeLaunchJson.hasMinContent(debugSettings);
                        if (!hasConfig) {
                            const result = await vscodeLaunchJson.ensureMinContent(debugSettings);
                            if (result) {
                                await vscodeLaunchJson.save();
                                results.push(new ProjectUpdateResult_1.default(IUpdateResult_1.UpdateResultType.updatedFile, this.id, 2, "Updated Minecraft Launch JSON", pi));
                            }
                        }
                    }
                }
            }
        }
        return results;
    }
}
exports.default = VsCodeFileManager;

//# sourceMappingURL=../maps/manager/VsCodeFileManager.js.map
