"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.AUTOGENERATED_WHOLEFILE_SCRIPT_SEPARATOR = void 0;
const Action_1 = require("../actions/Action");
const ActionGroup_1 = require("../actions/ActionGroup");
const IAction_1 = require("../actions/IAction");
const IScriptGenerationContext_1 = require("../actions/IScriptGenerationContext");
const Utilities_1 = require("../core/Utilities");
exports.AUTOGENERATED_WHOLEFILE_SCRIPT_SEPARATOR = "// ===== AUTOGENERATED FILE ===== CONTENT WITHIN THIS FILE WILL BE WIPED AND UPDATED WHEN USED IN TOOLING";
class ActionSetScriptGenerator {
    static generateScript(actionSet, options) {
        const lines = [];
        if (!options) {
            options = {
                typeScript: true,
            };
        }
        this.addScriptLines(lines, actionSet, options);
        return lines.join("\n");
    }
    static generateGameTestJavaScript(actionSet, groupName, nameSuffix, structureName) {
        const lines = [];
        let name = actionSet.name;
        if (nameSuffix) {
            name += nameSuffix;
        }
        name = Utilities_1.default.getSimpleString(name);
        groupName = Utilities_1.default.getSimpleString(groupName);
        lines.push("async function gt_" + name + "(test) {");
        this.addScriptGroupLines(lines, actionSet, {
            isFunction: true,
            addTestIdlePause: true,
            useGameTestApis: true,
            typeScript: false,
        }, { indent: 2 }, IScriptGenerationContext_1.ScriptGenerationPlacement.inSequence);
        lines.push("}");
        lines.push('registerAsync("' +
            groupName +
            '", "' +
            name +
            '", gt_' +
            name +
            ').maxTicks(4000).structureName("gametest:' +
            structureName +
            'empty");');
        return lines.join("\n");
    }
    static addScriptGroupLinesForFunction(lines, actionGroup, options, context) {
        this.addScriptGroupLines(lines, actionGroup, options, { indent: 2 }, IScriptGenerationContext_1.ScriptGenerationPlacement.beforeBlockStart);
        if (actionGroup.groupAction) {
            actionGroup.groupAction.addScriptLines(lines, options, { indent: 2 }, IScriptGenerationContext_1.ScriptGenerationPlacement.blockStart);
        }
        else {
            let functionLine = "export function " + actionGroup.name + "(";
            let args = "";
            if (actionGroup.expectedContext === IAction_1.ActionContextType.dimensionLocation) {
                args += "location: mc.DimensionLocation";
            }
            functionLine += args + ") {";
            options.isFunction = true;
            lines.push(functionLine);
        }
        this.addScriptGroupLines(lines, actionGroup, options, { indent: 2 }, IScriptGenerationContext_1.ScriptGenerationPlacement.afterBlockStart);
        this.addScriptGroupLines(lines, actionGroup, options, { indent: 2 }, IScriptGenerationContext_1.ScriptGenerationPlacement.inSequence);
        this.addScriptGroupLines(lines, actionGroup, options, { indent: 2 }, IScriptGenerationContext_1.ScriptGenerationPlacement.beforeBlockEnd);
        if (actionGroup.groupAction) {
            actionGroup.groupAction.addScriptLines(lines, options, { indent: 2 }, IScriptGenerationContext_1.ScriptGenerationPlacement.blockEnd);
        }
        else {
            lines.push("}");
        }
        this.addScriptGroupLines(lines, actionGroup, options, { indent: 2 }, IScriptGenerationContext_1.ScriptGenerationPlacement.afterBlockEnd);
    }
    static addInitGroupLines(lines, actionSet, options, context) {
        lines.push("export function init() {");
        this.addScriptGroupLines(lines, actionSet, options, { indent: 2 }, IScriptGenerationContext_1.ScriptGenerationPlacement.initInSequence);
        lines.push("}");
    }
    static addScriptGroupLines(lines, actionGroup, scriptGenOptions, context, placement) {
        const req = {};
        for (const action of actionGroup.actions) {
            if (action instanceof Action_1.default) {
                ActionGroup_1.default.mergeScriptRequirements(req, action.getScriptRequirements(scriptGenOptions));
            }
        }
        if (req.needsLocalOverworld && placement === IScriptGenerationContext_1.ScriptGenerationPlacement.inSequence) {
            ActionGroup_1.default.addLine(lines, context.indent, 'const overworld = mc.world.getDimension("overworld");');
        }
        if (actionGroup.groupAction &&
            (placement === IScriptGenerationContext_1.ScriptGenerationPlacement.initInSequence || placement === IScriptGenerationContext_1.ScriptGenerationPlacement.inSequence)) {
            actionGroup.groupAction.addScriptLines(lines, scriptGenOptions, context, placement);
        }
        for (const action of actionGroup.actions) {
            if (action instanceof ActionGroup_1.default) {
                this.addScriptGroupLines(lines, action, scriptGenOptions, context, placement);
            }
            else if (action instanceof Action_1.default) {
                action.addScriptLines(lines, scriptGenOptions, context, placement);
            }
            if (scriptGenOptions.addTestIdlePause) {
                ActionGroup_1.default.addLine(lines, context.indent, "await test.idle(15);");
            }
        }
    }
    static addScriptLines(lines, actionSet, options) {
        lines.push(exports.AUTOGENERATED_WHOLEFILE_SCRIPT_SEPARATOR);
        lines.push('import * as mc from "@minecraft/server";');
        let subGroupCount = 0;
        let subActionCount = 0;
        for (const childAction of actionSet.actions) {
            if (childAction instanceof ActionGroup_1.default) {
                subGroupCount++;
            }
            else if (childAction instanceof Action_1.default) {
                subActionCount++;
            }
        }
        let actionGroupCount = 0;
        if (subGroupCount > 0 && subActionCount === 0) {
            for (const childAction of actionSet.actions) {
                if (childAction instanceof ActionGroup_1.default) {
                    if (!childAction.name) {
                        childAction.name = actionSet.name + "_" + String(actionGroupCount + 1);
                    }
                    this.addScriptGroupLinesForFunction(lines, childAction, options, { indent: 2 });
                    actionGroupCount++;
                }
            }
        }
        else {
            this.addScriptGroupLinesForFunction(lines, actionSet, options, { indent: 2 });
        }
        this.addInitGroupLines(lines, actionSet, options, { indent: 2 });
    }
}
exports.default = ActionSetScriptGenerator;

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