import { world } from "@minecraft/server";
import { ActionFormData, ModalFormData } from "@minecraft/server-ui";
import { namespace } from "../../config/_config";
export var Behavior;
(function (Behavior) {
    Behavior["FRIENDLY"] = "friendly";
    Behavior["NEUTRAL"] = "neutral";
    Behavior["HOSTILE"] = "hostile";
})(Behavior || (Behavior = {}));
export class ActionForm {
    constructor(id, options, isMainForm = false) {
        this.id = id;
        this.options = options;
        this._isMainForm = isMainForm;
    }
    translateString(str) {
        return { translate: str };
    }
    translateStrings(strings) {
        return strings.map((str) => ({ translate: str }));
    }
    getTitle() {
        return this.translateString(this.options.title || `item.${namespace}:${this.id}`);
    }
    get isMainForm() {
        return this._isMainForm;
    }
    compile(referencedForms, referencesMap) {
        const form = new ActionFormData();
        form.title(this.getTitle());
        const body = [];
        const newlines = { text: "\n\n" };
        const singleNewline = { text: "\n" };
        if (this.options.description) {
            this.translateStrings(this.options.description).forEach((desc) => {
                body.push(singleNewline, desc);
            });
        }
        const wikiInfo = this.options.wikiInfo;
        if (wikiInfo) {
            if (wikiInfo) {
                if (wikiInfo.biomes) {
                    if (wikiInfo.biomes.length === 0) {
                        body.push(newlines, { translate: `${namespace}.form.no_biomes` });
                    }
                    else {
                        body.push(newlines, { translate: `${namespace}.form.biomes` }, { text: " " });
                        for (const biome of wikiInfo.biomes) {
                            body.push({ translate: `${namespace}.form.biome.${biome}` }, { text: ", " });
                        }
                        body.pop(); // Remove trailing comma
                    }
                }
                if (wikiInfo.isRidable !== undefined) {
                    body.push(newlines, {
                        translate: wikiInfo.isRidable
                            ? `${namespace}.form.rideable`
                            : `${namespace}.form.not_rideable`,
                    });
                }
                if (wikiInfo.isBreedable !== undefined) {
                    body.push(newlines, {
                        translate: wikiInfo.isBreedable
                            ? `${namespace}.form.breedable`
                            : `${namespace}.form.not_breedable`,
                    });
                }
                if (wikiInfo.isTameable !== undefined) {
                    body.push(newlines, {
                        translate: wikiInfo.isTameable
                            ? `${namespace}.form.tameable`
                            : `${namespace}.form.not_tameable`,
                    });
                }
                if (wikiInfo.behavior) {
                    body.push(newlines, {
                        translate: `${namespace}.form.behavior.${wikiInfo.behavior}`,
                    });
                }
                if (wikiInfo.food) {
                    if (wikiInfo.food.length === 0) {
                        body.push(newlines, { translate: `${namespace}.form.no_food` });
                    }
                    else {
                        body.push(newlines, { translate: `${namespace}.form.food` }, { text: " " });
                        for (const item of wikiInfo.food) {
                            body.push({ translate: item }, { text: ", " });
                        }
                        body.pop(); // Remove trailing comma
                    }
                }
                if (wikiInfo.drops) {
                    if (wikiInfo.drops.length === 0) {
                        body.push(newlines, { translate: `${namespace}.form.no_drops` });
                    }
                    else {
                        body.push(newlines, { translate: `${namespace}.form.drops` }, { text: " " });
                        for (const drop of wikiInfo.drops) {
                            body.push({ translate: drop }, { text: ", " });
                        }
                        body.pop(); // Remove trailing comma
                    }
                }
                if (wikiInfo.hasCraftingTableRecipe !== undefined) {
                    body.push(newlines, {
                        translate: wikiInfo.hasCraftingTableRecipe
                            ? `${namespace}.form.recipe`
                            : `${namespace}.form.no_recipe`,
                    });
                }
                if (wikiInfo.hasCustomTableRecipe !== undefined) {
                    if (wikiInfo.hasCustomTableRecipe.recipe !== undefined) {
                        body.push(newlines, {
                            translate: wikiInfo.hasCustomTableRecipe.recipe
                                ? `${namespace}.form.custom.recipe`
                                : `${namespace}.form.custom.no_recipe`,
                        }, { translate: `tile.${wikiInfo.hasCustomTableRecipe.blockID}.name` });
                    }
                }
                if (wikiInfo.isShearable !== undefined) {
                    body.push(newlines, {
                        translate: wikiInfo.isShearable
                            ? `${namespace}.form.shearable`
                            : `${namespace}.form.no_shearable`,
                    });
                }
            }
        }
        let buttonCount = 0;
        let backButtonIndex;
        // Add buttons for references, including fake buttons for ModalForms
        const references = referencesMap.get(this.id);
        if (references) {
            references.forEach((refId) => {
                const refForm = referencedForms.get(refId);
                if (refForm) {
                    const title = refForm instanceof ActionForm ? refForm.getTitle() : { text: refForm.getTitle() };
                    const iconPath = refForm.options.iconPath || "";
                    form.button(title, iconPath);
                    buttonCount++;
                }
            });
        }
        // Add back button for sub-pages
        if (!this.isMainForm) {
            form.button({ translate: `${namespace}.form.back` });
            backButtonIndex = buttonCount;
            buttonCount++;
        }
        body.push(newlines); // Add newlines before the back button
        form.body({ rawtext: body });
        return { form, buttonCount, backButtonIndex };
    }
}
export class ModalForm {
    constructor(id, options, _isMainForm = false) {
        this.id = id;
        this.options = options;
        this._isMainForm = _isMainForm;
    }
    getTitle() {
        return this.options.title;
    }
    get isMainForm() {
        return this._isMainForm;
    }
    compile() {
        const form = new ModalFormData().title(this.options.title);
        this.options.controls.forEach((control) => {
            let value = control.defaultValue;
            if (control.dynamicProperty) {
                const dynamicValue = world.getDynamicProperty(control.dynamicProperty);
                if (dynamicValue !== undefined) {
                    value = dynamicValue;
                }
            }
            switch (control.type) {
                case "toggle":
                    form.toggle({ translate: control.label }, value);
                    break;
                case "slider":
                    form.slider({ translate: control.label }, control.options.min, control.options.max, control.options.step, value);
                    break;
                case "dropdown":
                    form.dropdown({ translate: control.label }, control.options, value);
                    break;
                case "textField":
                    form.textField({ translate: control.label }, control.options.placeholder, value);
                    break;
            }
        });
        return form;
    }
}
