"use strict";
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
Object.defineProperty(exports, "__esModule", { value: true });
exports.NpmPackageSettingAllowList = exports.PackageJsonDefault = exports.OverridesDefault = exports.DependenciesDefault = exports.ScriptsDefault = exports.DevDependenciesDefault = void 0;
const ste_events_1 = require("ste-events");
const StorageUtilities_1 = require("../storage/StorageUtilities");
const Log_1 = require("../core/Log");
exports.DevDependenciesDefault = {
    "@minecraft/core-build-tasks": "^5.2.0",
    "eslint-plugin-minecraft-linting": "^2.0.2",
    "source-map": "^0.7.4",
    "ts-node": "^10.9.1",
    typescript: "^5.5.4",
};
exports.ScriptsDefault = {
    lint: "just-scripts lint",
    build: "just-scripts build",
    clean: "just-scripts clean",
    "local-deploy": "just-scripts local-deploy",
    mcaddon: "just-scripts mcaddon",
    enablemcloopback: "CheckNetIsolation.exe LoopbackExempt -a -p=S-1-15-2-1958404141-86561845-1752920682-3514627264-368642714-62675701-733520436",
    enablemcpreviewloopback: "CheckNetIsolation.exe LoopbackExempt -a -p=S-1-15-2-424268864-5579737-879501358-346833251-474568803-887069379-4040235476",
    buildsnippets: "just-scripts buildSnippets",
};
exports.DependenciesDefault = {
    "@minecraft/math": "^2.2.7",
    "@minecraft/server": "^2.0.0",
    "@minecraft/server-editor": "^0.1.0-beta.1.21.30-preview.24",
    "@minecraft/server-ui": "^2.0.0",
    "@minecraft/vanilla-data": "^1.21.90",
};
exports.OverridesDefault = {
    "@minecraft/math": {
        "@minecraft/server": "$@minecraft/server",
    },
    "@minecraft/server-gametest": {
        "@minecraft/server": "$@minecraft/server",
    },
    "@minecraft/server-ui": {
        "@minecraft/server": "$@minecraft/server",
    },
};
exports.PackageJsonDefault = {
    name: "my-project",
    version: "0.1.0",
    description: "My Minecraft Addon Project",
    private: true,
    devDependencies: exports.DevDependenciesDefault,
    scripts: exports.ScriptsDefault,
    dependencies: exports.DependenciesDefault,
};
exports.NpmPackageSettingAllowList = [
    "name",
    "author",
    "dependencies",
    "devDependencies",
    "contributors",
    "license",
    "overrides",
    "version",
    "productName",
    "description",
    "private",
    "scripts",
];
class NpmPackageDefinition {
    _file;
    _id;
    _isLoaded = false;
    definition;
    _onLoaded = new ste_events_1.EventDispatcher();
    get isLoaded() {
        return this._isLoaded;
    }
    get file() {
        return this._file;
    }
    set file(newFile) {
        this._file = newFile;
    }
    get onLoaded() {
        return this._onLoaded.asEvent();
    }
    get description() {
        if (!this.definition) {
            return undefined;
        }
        return this.definition.description;
    }
    get name() {
        if (this.definition) {
            return this.definition.name;
        }
        return undefined;
    }
    get id() {
        return this._id;
    }
    set id(newId) {
        this._id = newId;
    }
    static async ensureOnFile(file, loadHandler) {
        let dt;
        if (file.manager === undefined) {
            dt = new NpmPackageDefinition();
            dt.file = file;
            file.manager = dt;
        }
        if (file.manager !== undefined && file.manager instanceof NpmPackageDefinition) {
            dt = file.manager;
            if (!dt.isLoaded && loadHandler) {
                dt.onLoaded.subscribe(loadHandler);
            }
            await dt.load();
            return dt;
        }
        return dt;
    }
    async persist() {
        if (this._file === undefined) {
            return;
        }
        const pjString = JSON.stringify(this.definition, null, 2);
        this._file.setContent(pjString);
    }
    async save() {
        if (this._file === undefined) {
            return;
        }
        this.persist();
        await this._file.saveContent(false);
    }
    async ensureMinContent(project) {
        await this.load();
        if (!this.definition) {
            this.definition = exports.PackageJsonDefault;
        }
        await this.updateContent(project);
        await this.ensureStandardContent();
    }
    async updateContent(project) {
        await this.load();
        if (!this.definition) {
            return;
        }
        this.definition.name = project.name;
        this.definition.description = project.description;
        this.definition.version = project.versionAsString;
    }
    async ensureStandardContent() {
        await this.load();
        if (!this.definition) {
            return;
        }
        if (this.definition.devDependencies) {
            let isValidDevDependencies = true;
            for (const devDepend in this.definition.devDependencies) {
                if (exports.DevDependenciesDefault[devDepend] === undefined) {
                    Log_1.default.debugAlert("Unexpected dev dependency '" + devDepend + "' found.");
                    isValidDevDependencies = false;
                }
            }
            if (!isValidDevDependencies) {
                this.definition.devDependencies = exports.DevDependenciesDefault;
            }
        }
        if (this.definition.dependencies) {
            let isValidDependencies = true;
            for (const dependencyName in this.definition.dependencies) {
                if (exports.DependenciesDefault[dependencyName] === undefined) {
                    Log_1.default.debugAlert("Unexpected dependency '" + dependencyName + "' found.");
                    isValidDependencies = false;
                }
            }
            if (!isValidDependencies) {
                this.definition.dependencies = exports.DependenciesDefault;
            }
        }
        if (this.definition.overrides) {
            const overrides = this.definition.overrides;
            let isOverrideValid = true;
            for (const overrideName in overrides) {
                if (exports.OverridesDefault[overrideName] === undefined) {
                    Log_1.default.debugAlert("Unexpected override '" + overrideName + "' found.");
                    isOverrideValid = false;
                }
                else if (JSON.stringify(exports.OverridesDefault[overrideName]) !== JSON.stringify(overrides[overrideName])) {
                    Log_1.default.debugAlert("Unexpected override '" +
                        overrideName +
                        "' is not in exepected form: " +
                        JSON.stringify(overrides[overrideName]));
                    isOverrideValid = false;
                }
            }
            if (!isOverrideValid) {
                this.definition.overrides = exports.OverridesDefault;
            }
        }
        if (this.definition.scripts) {
            let isValidScripts = true;
            for (const scriptName in this.definition.scripts) {
                if (exports.ScriptsDefault[scriptName] === undefined) {
                    Log_1.default.debugAlert("Unexpected script '" + scriptName + "' found.");
                    isValidScripts = false;
                }
                else if (this.definition.scripts[scriptName] !== exports.ScriptsDefault[scriptName]) {
                    Log_1.default.debugAlert("Script '" + scriptName + "' is in unexpected form: " + this.definition.scripts[scriptName]);
                    isValidScripts = false;
                }
            }
            if (!isValidScripts) {
                this.definition.scripts = exports.ScriptsDefault;
            }
        }
        for (const key in this.definition) {
            if (!exports.NpmPackageSettingAllowList.includes(key)) {
                this.definition[key] = undefined;
                Log_1.default.debugAlert("Unexpected setting '" + key + "' found in package.json.");
            }
        }
    }
    async load() {
        if (this._file === undefined || this._isLoaded) {
            return;
        }
        await this._file.loadContent();
        if (this._file.content === null || this._file.content instanceof Uint8Array) {
            return;
        }
        this.id = this._file.name;
        this.definition = StorageUtilities_1.default.getJsonObject(this._file);
        this._isLoaded = true;
    }
}
exports.default = NpmPackageDefinition;

//# sourceMappingURL=../maps/devproject/NpmPackageDefinition.js.map
