"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const ste_events_1 = require("ste-events");
const ActionSet_1 = require("../actions/ActionSet");
const StorageUtilities_1 = require("../storage/StorageUtilities");
const Project_1 = require("../app/Project");
const MinecraftUtilities_1 = require("../minecraft/MinecraftUtilities");
const ActionSetScriptGenerator_1 = require("./ActionSetScriptGenerator");
const ActionSetCommandGenerator_1 = require("./ActionSetCommandGenerator");
const IProjectItemData_1 = require("../app/IProjectItemData");
const ProjectUtilities_1 = require("../app/ProjectUtilities");
class ActionSetManager {
    _jsonFile;
    _jsFile;
    _tsFile;
    _functionFile;
    _isLoaded;
    _actionSet;
    _project;
    _actionSetData;
    get actionSet() {
        return this._actionSet;
    }
    _onLoaded = new ste_events_1.EventDispatcher();
    constructor(project, actionSet) {
        this._isLoaded = false;
        this._actionSet = actionSet;
        this._project = project;
    }
    get isLoaded() {
        return this._isLoaded;
    }
    get jsonFile() {
        return this._jsonFile;
    }
    get onLoaded() {
        return this._onLoaded.asEvent();
    }
    get name() {
        if (this._actionSetData === undefined) {
            return "";
        }
        return this._actionSetData.name;
    }
    set name(newName) {
        if (this._actionSetData === undefined) {
            return;
        }
        this._actionSetData.name = newName;
    }
    set jsonFile(newFile) {
        this._jsonFile = newFile;
    }
    async load() {
        if (this._jsonFile === undefined || this._isLoaded) {
            return;
        }
        await this._jsonFile.loadContent();
        if (this._jsonFile.content === null || this._jsonFile.content instanceof Uint8Array) {
            return;
        }
        this._actionSetData = StorageUtilities_1.default.getJsonObject(this._jsonFile);
        if (this._actionSetData) {
            this._actionSet = new ActionSet_1.default(this._actionSetData);
            this._actionSet.ensureLoaded();
            this._actionSet.name = StorageUtilities_1.default.getBaseFromName(this._jsonFile.name);
        }
        this._isLoaded = true;
        this._onLoaded.dispatch(this, this);
    }
    ensureJsFile() {
        if (this._jsonFile === undefined || this._jsFile !== undefined) {
            return;
        }
        const newFileName = this.getScriptBaseName() + ".js";
        this._jsFile = this._jsonFile.parentFolder.ensureFile(newFileName);
    }
    getScriptBaseName() {
        let name = "action";
        if (this._jsonFile) {
            name = this._jsonFile.name;
        }
        else if (this._actionSet && this._actionSet.name) {
            name = this._actionSet.name;
        }
        return MinecraftUtilities_1.default.makeNameScriptSafe(StorageUtilities_1.default.getBaseFromName(name));
    }
    async ensureTsFile() {
        if ((this._jsonFile === undefined && this._actionSet === undefined) || this._tsFile !== undefined) {
            return;
        }
        const name = this.getScriptBaseName();
        const genFolder = await this._project.ensureScriptGenFolder();
        const newFileName = name + ".ts";
        this._tsFile = genFolder.ensureFile(newFileName);
    }
    async ensureFunctionFile() {
        if (this._jsonFile === undefined || this._functionFile !== undefined) {
            return;
        }
        const newFileName = StorageUtilities_1.default.getBaseFromName(this._jsonFile.name) + ".mcfunction";
        let functionFolder = this._jsonFile.parentFolder;
        if (functionFolder.name === "scripts" && functionFolder.parentFolder) {
            functionFolder = functionFolder.parentFolder.ensureFolder("functions");
        }
        await functionFolder.ensureExists();
        this._functionFile = functionFolder.ensureFile(newFileName);
    }
    async persist(project) {
        if (this._jsonFile) {
            const bpString = JSON.stringify(this._actionSetData);
            this._jsonFile.setContent(bpString);
        }
        await this.ensureTsFile();
        await this.ensureFunctionFile();
        if (this.actionSet && this._jsFile) {
            this._project.ensureItemFromFile(this._jsFile, IProjectItemData_1.ProjectItemType.js, Project_1.FolderContext.behaviorPack, IProjectItemData_1.ProjectItemCreationType.generated);
            const jsString = ActionSetScriptGenerator_1.default.generateScript(this.actionSet, { typeScript: false });
            this._jsFile.setContent(jsString);
        }
        if (this.actionSet && this._tsFile) {
            this._project.ensureItemFromFile(this._tsFile, IProjectItemData_1.ProjectItemType.ts, Project_1.FolderContext.behaviorPack, IProjectItemData_1.ProjectItemCreationType.generated);
            const tsString = ActionSetScriptGenerator_1.default.generateScript(this.actionSet, { typeScript: true });
            this._tsFile.setContent(tsString);
            const baseName = StorageUtilities_1.default.getBaseFromName(this._tsFile.name);
            const scriptSafeName = MinecraftUtilities_1.default.makeNameScriptSafe(baseName);
            await ProjectUtilities_1.default.ensureContentInDefaultScriptFile(project, "import * as " + scriptSafeName, "import * as " + scriptSafeName + ' from "./_gen/' + baseName + '";\n' + scriptSafeName + ".init();\n", false);
        }
        if (this.actionSet && this._functionFile) {
            const functionContent = ActionSetCommandGenerator_1.default.generateMCFunction(this.actionSet);
            this._functionFile.setContent(functionContent);
        }
    }
    static async ensureOnFile(file, project, loadHandler) {
        let autos;
        if (file.manager === undefined) {
            autos = new ActionSetManager(project);
            autos.jsonFile = file;
            file.manager = autos;
        }
        if (file.manager !== undefined && file.manager instanceof ActionSetManager) {
            autos = file.manager;
            if (!autos.isLoaded) {
                if (loadHandler) {
                    autos.onLoaded.subscribe(loadHandler);
                }
                await autos.load();
            }
        }
        return autos;
    }
}
exports.default = ActionSetManager;

//# sourceMappingURL=../maps/script/ActionSetManager.js.map
