"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ActionSetCatalog = exports.GroupActions = void 0;
const Utilities_1 = require("../core/Utilities");
const DataFormFile_1 = require("../dataform/DataFormFile");
const Database_1 = require("../minecraft/Database");
const StorageUtilities_1 = require("../storage/StorageUtilities");
const ActionGroup_1 = require("./ActionGroup");
const BlockBreakAction_1 = require("./BlockBreakAction");
const BlockExplodeAction_1 = require("./BlockExplodeAction");
const BlockSetAction_1 = require("./BlockSetAction");
const EntityDieAction_1 = require("./EntityDieAction");
const EntitySpawnAction_1 = require("./EntitySpawnAction");
const GeneralAction_1 = require("./GeneralAction");
const IAction_1 = require("./IAction");
const IActionSetData_1 = require("./IActionSetData");
const ItemSpawnAction_1 = require("./ItemSpawnAction");
const ParticleSpawnAction_1 = require("./ParticleSpawnAction");
const PlayerJoinAction_1 = require("./PlayerJoinAction");
const RandomizeGroupAction_1 = require("./RandomizeGroupAction");
const SequenceGroupAction_1 = require("./SequenceGroupAction");
const SoundPlayAction_1 = require("./SoundPlayAction");
const TestIdleAction_1 = require("./TestIdleAction");
const TestSimulatedPlayerMoveAction_1 = require("./TestSimulatedPlayerMoveAction");
const TestSimulatedPlayerSpawnAction_1 = require("./TestSimulatedPlayerSpawnAction");
const TickGroupAction_1 = require("./TickGroupAction");
const WorldSendMessageAction_1 = require("./WorldSendMessageAction");
const EntitySpawnActionType = {
    id: "entity_spawn",
    title: "Entity Spawn",
    requiredContextType: IAction_1.ActionContextType.dimensionLocation,
};
const BlockBreakActionType = {
    id: "block_break",
    title: "Block Break",
    requiredContextType: IAction_1.ActionContextType.dimensionLocation,
};
exports.GroupActions = ["tick", "entity_die", "block_explode", "player_join", "randomize", "sequence"];
class ActionSetCatalog {
    static async getEntityLegacyFiltersSet() {
        const legacyFilterTypes = [];
        await ActionSetCatalog.addTypesFromFormFolder(legacyFilterTypes, "entityfilters", IActionSetData_1.ActionSetTarget.entityEvent);
        return legacyFilterTypes;
    }
    static async getScriptSet() {
        const scriptFilterTypes = [];
        await ActionSetCatalog.addTypesFromFormFolder(scriptFilterTypes, "scriptfilters", IActionSetData_1.ActionSetTarget.script);
        return scriptFilterTypes;
    }
    static async addTypesFromFormFolder(actionList, folderName, actionSetTarget) {
        const formsFolder = await Database_1.default.getFormsFolder(folderName);
        if (!formsFolder) {
            return;
        }
        await formsFolder.load();
        for (const fileName in formsFolder.files) {
            const file = formsFolder.files[fileName];
            if (file) {
                if (!file.isContentLoaded) {
                    await file.loadContent();
                }
                const form = await DataFormFile_1.default.ensureOnFile(file);
                if (form && form.formDefinition) {
                    let shouldAddItem = true;
                    if (form.formDefinition.tags) {
                        if (actionSetTarget === IActionSetData_1.ActionSetTarget.entityEvent && !form.formDefinition.tags.includes("entityevent")) {
                            shouldAddItem = false;
                        }
                        if (actionSetTarget === IActionSetData_1.ActionSetTarget.script && !form.formDefinition.tags.includes("script")) {
                            shouldAddItem = false;
                        }
                        if (actionSetTarget === IActionSetData_1.ActionSetTarget.mcfunction && !form.formDefinition.tags.includes("commands")) {
                            shouldAddItem = false;
                        }
                        if (actionSetTarget === IActionSetData_1.ActionSetTarget.gameTest && !form.formDefinition.tags.includes("test")) {
                            shouldAddItem = false;
                        }
                    }
                    else {
                        shouldAddItem = false;
                    }
                    if (shouldAddItem) {
                        actionList.push({
                            id: StorageUtilities_1.default.getCoreBaseFromName(fileName),
                            title: form.title
                                ? form.title
                                : Utilities_1.default.humanify(form.id ? form.id : StorageUtilities_1.default.getCoreBaseFromName(fileName)),
                            form: form.formDefinition,
                        });
                    }
                }
            }
        }
    }
    getDefaultContextForTarget(actionSetTarget) {
        if (actionSetTarget === IActionSetData_1.ActionSetTarget.entityEvent) {
            return IAction_1.ActionContextType.entity;
        }
        return IAction_1.ActionContextType.general;
    }
    static async getActionCatalog(actionSetTarget) {
        const actionTypes = [];
        if (actionSetTarget === IActionSetData_1.ActionSetTarget.entityEvent) {
            await ActionSetCatalog.addTypesFromFormFolder(actionTypes, "entityevents", actionSetTarget);
        }
        else {
            await ActionSetCatalog.addTypesFromFormFolder(actionTypes, "action", actionSetTarget);
        }
        return actionTypes;
    }
    static createActionGroup(parent, data, id) {
        const ag = new ActionGroup_1.default(data
            ? data
            : {
                id: id ? id : Utilities_1.default.createRandomId(10),
                actions: [],
            }, parent.actionSet);
        return ag;
    }
    static createActionOrGroup(parent, type, data, id) {
        if (exports.GroupActions.includes(type)) {
            const ag = this.createActionGroup(parent, data, id);
            ag.groupActionType = type;
            return ag;
        }
        return this.createAction(parent, type, data, id);
    }
    static createAction(parent, type, data, id) {
        let action = undefined;
        if (!data) {
            data = {
                type: type,
            };
        }
        switch (type) {
            case EntitySpawnActionType.id:
                action = new EntitySpawnAction_1.default(parent, data);
                break;
            case BlockBreakActionType.id:
                action = new BlockBreakAction_1.default(parent, data);
                break;
            case "test_simulated_player_spawn":
                action = new TestSimulatedPlayerSpawnAction_1.default(parent, data);
                break;
            case "test_simulated_player_move":
                action = new TestSimulatedPlayerMoveAction_1.default(parent, data);
                break;
            case "test_idle":
                action = new TestIdleAction_1.default(parent, data);
                break;
            case "tick":
                action = new TickGroupAction_1.default(parent, data);
                break;
            case "randomize":
                action = new RandomizeGroupAction_1.default(parent, data);
                break;
            case "sequence":
                action = new SequenceGroupAction_1.default(parent, data);
                break;
            case "world_send_message":
                action = new WorldSendMessageAction_1.default(parent, data);
                break;
            case "entity_die":
                action = new EntityDieAction_1.default(parent, data);
                break;
            case "player_join":
                action = new PlayerJoinAction_1.default(parent, data);
                break;
            case "block_set":
                action = new BlockSetAction_1.default(parent, data);
                break;
            case "particle_spawn":
                action = new ParticleSpawnAction_1.default(parent, data);
                break;
            case "item_spawn":
                action = new ItemSpawnAction_1.default(parent, data);
                break;
            case "block_explode":
                action = new BlockExplodeAction_1.default(parent, data);
                break;
            case "sound_play":
                action = new SoundPlayAction_1.default(parent, data);
                break;
            default:
                action = new GeneralAction_1.default(parent, data);
                action.typeId = type;
                break;
        }
        if (id) {
            action.id = id;
        }
        return action;
    }
}
exports.ActionSetCatalog = ActionSetCatalog;

//# sourceMappingURL=../maps/actions/ActionSetCatalog.js.map
