import { InputPermissionCategory, system, EntityComponentTypes } from '@minecraft/server';
import { ActionFormData, ModalFormData } from '@minecraft/server-ui';
import { settingsGUI } from './pages/settings.js';

const actionHandlers = {
    settingsGUI,
};
// Utility: Toggle player input
function setInputPermission(player, category, enabled) {
    player.inputPermissions.setPermissionCategory(category, enabled);
}
const MENU_CONFIG = {
    bookMain: {
        searchable: false,
        title: "ui.ftb_sieves.book_welcome_to_sieves_and_things.title",
        body: "ui.ftb_sieves.book_welcome_to_sieves_and_things.body",
        buttons: [
            {
                label: "ui.ftb_sieves.book_getting_started.title",
                icon: "textures/ui/icon_spring",
                action: "gettingStarted",
            },
            {
                label: "ui.ftb_sieves.book_automation.title",
                icon: "textures/items/hopper",
                action: "automation",
            },
            {
                label: "ui.ftb_sieves.book_main.button.search",
                icon: "textures/ui/magnifyingGlass",
                action: "search",
            },
            {
                label: "ui.ftb_sieves.book_main.button.settings",
                icon: "textures/ui/settings_glyph_color_2x",
                action: "settingsGUI",
            },
            {
                label: "ui.ftb_sieves.book_main.button.our_stuff",
                icon: "textures/ui/Add-Ons_Nav_Icon36x36",
                action: "ourStuff",
            },
            {
                label: "ui.ftb_sieves.book_main.button.support",
                icon: "textures/ftb/sieves/book/recipes/ftb_logo",
                action: "support",
            },
            {
                label: "ui.ftb_sieves.book_main.button.exit",
                icon: "textures/ui/redX1",
                action: "close",
            }
        ],
    },
    gettingStarted: {
        searchable: false,
        title: "ui.ftb_sieves.book_getting_started.title",
        body: "ui.ftb_sieves.book_getting_started.body",
        buttons: [
            {
                label: "ui.ftb_sieves.book_rocks.title",
                icon: "textures/ftb/sieves/items/rock/stone",
                action: "rocks",
            },
            {
                label: "ui.ftb_sieves.book_stone_crafting_table.title",
                icon: "textures/ftb/sieves/book/recipes/stone_table",
                action: "stoneCraftingTable",
            },
            {
                label: "ui.ftb_sieves.book_crooks.title",
                icon: "textures/ftb/sieves/items/crooks/wood_crook",
                action: "crooks",
            },
            {
                label: "ui.ftb_sieves.book_leaf_worms.title",
                icon: "textures/ftb/sieves/items/worm/leaf_worm",
                action: "leafWorms",
            },
            {
                label: "ui.ftb_sieves.book_mesh.title",
                icon: "textures/ftb/sieves/items/mesh/mesh_string",
                action: "mesh",
            },
            {
                label: "ui.ftb_sieves.book_sieves.title",
                icon: "textures/ftb/sieves/book/recipes/sieve",
                action: "sieves",
            },
            {
                label: "ui.ftb_sieves.book_hammer.title",
                icon: "textures/ftb/sieves/items/hammer/stone",
                action: "hammer",
            },
            {
                label: "ui.ftb_sieves.book_trowels.title",
                icon: "textures/ftb/sieves/items/trowel/stone",
                action: "trowels",
            },
            {
                label: "ui.ftb_sieves.book_sickle.title",
                icon: "textures/ftb/sieves/items/sickle/stone",
                action: "sickles",
            },
            {
                label: "ui.ftb_sieves.book_sneak_to_grow.title",
                icon: "textures/ui/icon_new",
                action: "sneakToGrow",
            },
            {
                label: "ui.ftb_sieves.book_main.button.back",
                icon: "textures/ui/recap_glyph_color_2x",
                action: "bookMain",
            }
        ],
    },
    automation: {
        searchable: false,
        title: "ui.ftb_sieves.book_automation.title",
        body: "ui.ftb_sieves.book_automation.body",
        buttons: [
            {
                label: "ui.ftb_sieves.book_botany_pots.title",
                icon: "textures/ftb/sieves/book/recipes/botany-pot",
                action: "botanyPots",
            },
            {
                label: "ui.ftb_sieves.book_compost.title",
                icon: "textures/ftb/sieves/book/recipes/compost",
                action: "compost",
            },
            {
                label: "ui.ftb_sieves.book_wooden_crucible.title",
                icon: "textures/ftb/sieves/book/recipes/wood-crucible",
                action: "woodenCrucible",
            },
            {
                label: "ui.ftb_sieves.book_clay.title",
                icon: "textures/ftb/sieves/book/recipes/clay",
                action: "clay",
            },
            {
                label: "ui.ftb_sieves.book_clay_crucible.title",
                icon: "textures/ftb/sieves/book/recipes/clay-crucible",
                action: "clayCrucible",
            },
            {
                label: "ui.ftb_sieves.book_cobblestone_generators.title",
                icon: "textures/ftb/sieves/book/recipes/cobble-gen",
                action: "cobblestoneGenerators",
            },
            {
                label: "ui.ftb_sieves.book_basalt_generators.title",
                icon: "textures/ftb/sieves/book/recipes/basalt-gen",
                action: "basaltGenerators",
            },
            {
                label: "ui.ftb_sieves.book_auto_hammer.title",
                icon: "textures/ftb/sieves/book/recipes/auto-hammer",
                action: "autoHammer",
            },
            {
                label: "ui.ftb_sieves.book_main.button.back",
                icon: "textures/ui/recap_glyph_color_2x",
                action: "bookMain",
            }
        ],
    },
    rocks: {
        searchable: true,
        title: "ui.ftb_sieves.book_rocks.title",
        body: "ui.ftb_sieves.book_rocks.body",
        buttons: [
            {
                label: "ui.ftb_sieves.book_main.button.back",
                icon: "textures/ui/recap_glyph_color_2x",
                action: "gettingStarted",
            }
        ],
    },
    stoneCraftingTable: {
        searchable: true,
        title: "ui.ftb_sieves.book_stone_crafting_table.title",
        body: "ui.ftb_sieves.book_stone_crafting_table.body",
        buttons: [
            {
                label: "ui.ftb_sieves.book_main.button.back",
                icon: "textures/ui/recap_glyph_color_2x",
                action: "gettingStarted",
            }
        ],
    },
    trowels: {
        searchable: true,
        title: "ui.ftb_sieves.book_trowels.title",
        body: "ui.ftb_sieves.book_trowels.body",
        buttons: [
            {
                label: "ui.ftb_sieves.book_main.button.back",
                icon: "textures/ui/recap_glyph_color_2x",
                action: "gettingStarted",
            }
        ],
    },
    sickles: {
        searchable: true,
        title: "ui.ftb_sieves.book_sickle.title",
        body: "ui.ftb_sieves.book_sickle.body",
        buttons: [
            {
                label: "ui.ftb_sieves.book_main.button.back",
                icon: "textures/ui/recap_glyph_color_2x",
                action: "gettingStarted",
            }
        ],
    },
    crooks: {
        searchable: true,
        title: "ui.ftb_sieves.book_crooks.title",
        body: "ui.ftb_sieves.book_crooks.body",
        buttons: [
            {
                label: "ui.ftb_sieves.book_main.button.back",
                icon: "textures/ui/recap_glyph_color_2x",
                action: "gettingStarted",
            }
        ],
    },
    leafWorms: {
        searchable: true,
        title: "ui.ftb_sieves.book_leaf_worms.title",
        body: "ui.ftb_sieves.book_leaf_worms.body",
        buttons: [
            {
                label: "ui.ftb_sieves.book_main.button.back",
                icon: "textures/ui/recap_glyph_color_2x",
                action: "gettingStarted",
            }
        ],
    },
    mesh: {
        searchable: true,
        title: "ui.ftb_sieves.book_mesh.title",
        body: "ui.ftb_sieves.book_mesh.body",
        buttons: [
            {
                label: "ui.ftb_sieves.book_main.button.back",
                icon: "textures/ui/recap_glyph_color_2x",
                action: "gettingStarted",
            }
        ],
    },
    sieves: {
        searchable: true,
        title: "ui.ftb_sieves.book_sieves.title",
        body: "ui.ftb_sieves.book_sieves.body",
        buttons: [
            {
                label: "ui.ftb_sieves.book_sievable_blocks.title",
                icon: "textures/ftb/sieves/book/recipes/gravel",
                action: "sievableBlocks",
            },
            {
                label: "ui.ftb_sieves.book_main.button.back",
                icon: "textures/ui/recap_glyph_color_2x",
                action: "gettingStarted",
            }
        ],
    },
    hammer: {
        searchable: true,
        title: "ui.ftb_sieves.book_hammer.title",
        body: "ui.ftb_sieves.book_hammer.body",
        buttons: [
            {
                label: "ui.ftb_sieves.book_main.button.back",
                icon: "textures/ui/recap_glyph_color_2x",
                action: "gettingStarted",
            }
        ],
    },
    sneakToGrow: {
        searchable: true,
        title: "ui.ftb_sieves.book_sneak_to_grow.title",
        body: "ui.ftb_sieves.book_sneak_to_grow.body",
        buttons: [
            {
                label: "ui.ftb_sieves.book_main.button.back",
                icon: "textures/ui/recap_glyph_color_2x",
                action: "gettingStarted",
            }
        ],
    },
    ourStuff: {
        searchable: false,
        title: "ui.ftb_sieves.book_our_stuff.title",
        body: "ui.ftb_sieves.book_our_stuff.body",
        buttons: [
            {
                label: "ui.ftb_sieves.book_tinkers_construct.title",
                icon: "textures/ftb/sieves/book/recipes/tinkers",
                action: "tinkersConstruct",
            },
            {
                label: "ui.ftb_sieves.book_storage_drawers.title",
                icon: "textures/ftb/sieves/book/recipes/drawers",
                action: "storageDrawers",
            },
            {
                label: "ui.ftb_sieves.book_feed_the_bees.title",
                icon: "textures/ftb/sieves/book/recipes/bees",
                action: "feedTheBees",
            },
            {
                label: "ui.ftb_sieves.book_mining_and_building_gadgets.title",
                icon: "textures/ftb/sieves/book/recipes/gadgets",
                action: "miningAndBuildingGadgets",
            },
            {
                label: "ui.ftb_sieves.book_main.button.back",
                icon: "textures/ui/recap_glyph_color_2x",
                action: "bookMain",
            }
        ],
    },
    tinkersConstruct: {
        searchable: false,
        title: "ui.ftb_sieves.book_tinkers_construct.title",
        body: "ui.ftb_sieves.book_tinkers_construct.body",
        buttons: [
            {
                label: "ui.ftb_sieves.book_main.button.back",
                icon: "textures/ui/recap_glyph_color_2x",
                action: "ourStuff",
            }
        ],
    },
    storageDrawers: {
        searchable: false,
        title: "ui.ftb_sieves.book_storage_drawers.title",
        body: "ui.ftb_sieves.book_storage_drawers.body",
        buttons: [
            {
                label: "ui.ftb_sieves.book_main.button.back",
                icon: "textures/ui/recap_glyph_color_2x",
                action: "ourStuff",
            }
        ],
    },
    feedTheBees: {
        searchable: false,
        title: "ui.ftb_sieves.book_feed_the_bees.title",
        body: "ui.ftb_sieves.book_feed_the_bees.body",
        buttons: [
            {
                label: "ui.ftb_sieves.book_main.button.back",
                icon: "textures/ui/recap_glyph_color_2x",
                action: "ourStuff",
            }
        ],
    },
    miningAndBuildingGadgets: {
        searchable: false,
        title: "ui.ftb_sieves.book_mining_and_building_gadgets.title",
        body: "ui.ftb_sieves.book_mining_and_building_gadgets.body",
        buttons: [
            {
                label: "ui.ftb_sieves.book_main.button.back",
                icon: "textures/ui/recap_glyph_color_2x",
                action: "ourStuff",
            }
        ],
    },
    support: {
        searchable: false,
        title: "ui.ftb_sieves.book_support.title",
        body: "ui.ftb_sieves.book_support.body",
        buttons: [
            {
                label: "ui.ftb_sieves.book_main.button.back",
                icon: "textures/ui/recap_glyph_color_2x",
                action: "bookMain",
            }
        ],
    },
    string: {
        searchable: true,
        title: "ui.ftb_sieves.book_string.title",
        body: "ui.ftb_sieves.book_string.body",
        buttons: [
            {
                label: "ui.ftb_sieves.book_main.button.back",
                icon: "textures/ui/recap_glyph_color_2x",
                action: "bookMain",
            }
        ],
    },
    sievableBlocks: {
        searchable: false,
        title: "ui.ftb_sieves.book_sievable_blocks.title",
        body: "ui.ftb_sieves.book_sievable_blocks.body",
        buttons: [
            {
                label: "ui.ftb_sieves.book_dirt.title",
                icon: "textures/ftb/sieves/book/recipes/dirt",
                action: "dirt",
            },
            {
                label: "ui.ftb_sieves.book_dust.title",
                icon: "textures/ftb/sieves/book/recipes/dust",
                action: "dust",
            },
            {
                label: "ui.ftb_sieves.book_gravel.title",
                icon: "textures/ftb/sieves/book/recipes/gravel",
                action: "gravel",
            },
            {
                label: "ui.ftb_sieves.book_sand.title",
                icon: "textures/ftb/sieves/book/recipes/sand",
                action: "sand",
            },
            {
                label: "ui.ftb_sieves.book_red_sand.title",
                icon: "textures/ftb/sieves/book/recipes/red_sand",
                action: "redSand",
            },
            {
                label: "ui.ftb_sieves.book_soul_sand.title",
                icon: "textures/ftb/sieves/book/recipes/soul_sand",
                action: "soulSand",
            },
            {
                label: "ui.ftb_sieves.book_crushed_netherrack.title",
                icon: "textures/ftb/sieves/book/recipes/crushed_netherrack",
                action: "crushedNetherrack",
            },
            {
                label: "ui.ftb_sieves.book_crushed_endstone.title",
                icon: "textures/ftb/sieves/book/recipes/crushed_endstone",
                action: "crushedEndstone",
            },
            {
                label: "ui.ftb_sieves.book_crushed_basalt.title",
                icon: "textures/ftb/sieves/book/recipes/crushed_basalt",
                action: "crushedBasalt",
            }
        ],
    },
    dirt: {
        searchable: true,
        title: "ui.ftb_sieves.book_dirt.title",
        body: "ui.ftb_sieves.book_dirt.body",
        buttons: [
            {
                label: "ui.ftb_sieves.book_main.button.back",
                icon: "textures/ui/recap_glyph_color_2x",
                action: "sievableBlocks",
            }
        ],
    },
    dust: {
        searchable: true,
        title: "ui.ftb_sieves.book_dust.title",
        body: "ui.ftb_sieves.book_dust.body",
        buttons: [
            {
                label: "ui.ftb_sieves.book_main.button.back",
                icon: "textures/ui/recap_glyph_color_2x",
                action: "sievableBlocks",
            }
        ],
    },
    gravel: {
        searchable: true,
        title: "ui.ftb_sieves.book_gravel.title",
        body: "ui.ftb_sieves.book_gravel.body",
        buttons: [
            {
                label: "ui.ftb_sieves.book_main.button.back",
                icon: "textures/ui/recap_glyph_color_2x",
                action: "sievableBlocks",
            }
        ],
    },
    sand: {
        searchable: true,
        title: "ui.ftb_sieves.book_sand.title",
        body: "ui.ftb_sieves.book_sand.body",
        buttons: [
            {
                label: "ui.ftb_sieves.book_main.button.back",
                icon: "textures/ui/recap_glyph_color_2x",
                action: "sievableBlocks",
            }
        ],
    },
    redSand: {
        searchable: true,
        title: "ui.ftb_sieves.book_red_sand.title",
        body: "ui.ftb_sieves.book_red_sand.body",
        buttons: [
            {
                label: "ui.ftb_sieves.book_main.button.back",
                icon: "textures/ui/recap_glyph_color_2x",
                action: "sievableBlocks",
            }
        ],
    },
    soulSand: {
        searchable: true,
        title: "ui.ftb_sieves.book_soul_sand.title",
        body: "ui.ftb_sieves.book_soul_sand.body",
        buttons: [
            {
                label: "ui.ftb_sieves.book_main.button.back",
                icon: "textures/ui/recap_glyph_color_2x",
                action: "sievableBlocks",
            }
        ],
    },
    crushedNetherrack: {
        searchable: true,
        title: "ui.ftb_sieves.book_crushed_netherrack.title",
        body: "ui.ftb_sieves.book_crushed_netherrack.body",
        buttons: [
            {
                label: "ui.ftb_sieves.book_main.button.back",
                icon: "textures/ui/recap_glyph_color_2x",
                action: "sievableBlocks",
            }
        ],
    },
    crushedEndstone: {
        searchable: true,
        title: "ui.ftb_sieves.book_crushed_endstone.title",
        body: "ui.ftb_sieves.book_crushed_endstone.body",
        buttons: [
            {
                label: "ui.ftb_sieves.book_main.button.back",
                icon: "textures/ui/recap_glyph_color_2x",
                action: "sievableBlocks",
            }
        ],
    },
    crushedBasalt: {
        searchable: true,
        title: "ui.ftb_sieves.book_crushed_basalt.title",
        body: "ui.ftb_sieves.book_crushed_basalt.body",
        buttons: [
            {
                label: "ui.ftb_sieves.book_main.button.back",
                icon: "textures/ui/recap_glyph_color_2x",
                action: "sievableBlocks",
            }
        ],
    },
    botanyPots: {
        searchable: true,
        title: "ui.ftb_sieves.book_botany_pots.title",
        body: "ui.ftb_sieves.book_botany_pots.body",
        buttons: [
            {
                label: "ui.ftb_sieves.book_compost.title",
                icon: "textures/ftb/sieves/book/recipes/compost",
                action: "compost",
            },
            {
                label: "ui.ftb_sieves.book_main.button.back",
                icon: "textures/ui/recap_glyph_color_2x",
                action: "automation",
            }
        ],
    },
    compost: {
        searchable: true,
        title: "ui.ftb_sieves.book_compost.title",
        body: "ui.ftb_sieves.book_compost.body",
        buttons: [
            {
                label: "ui.ftb_sieves.book_main.button.back",
                icon: "textures/ui/recap_glyph_color_2x",
                action: "automation",
            }
        ],
    },
    woodenCrucible: {
        searchable: true,
        title: "ui.ftb_sieves.book_wooden_crucible.title",
        body: "ui.ftb_sieves.book_wooden_crucible.body",
        buttons: [
            {
                label: "ui.ftb_sieves.book_clay.title",
                icon: "textures/ftb/sieves/book/recipes/clay",
                action: "clay",
            },
            {
                label: "ui.ftb_sieves.book_main.button.back",
                icon: "textures/ui/recap_glyph_color_2x",
                action: "automation",
            }
        ],
    },
    clay: {
        searchable: true,
        title: "ui.ftb_sieves.book_clay.title",
        body: "ui.ftb_sieves.book_clay.body",
        buttons: [
            {
                label: "ui.ftb_sieves.book_main.button.back",
                icon: "textures/ui/recap_glyph_color_2x",
                action: "automation",
            }
        ],
    },
    clayCrucible: {
        searchable: true,
        title: "ui.ftb_sieves.book_clay_crucible.title",
        body: "ui.ftb_sieves.book_clay_crucible.body",
        buttons: [
            {
                label: "ui.ftb_sieves.book_main.button.back",
                icon: "textures/ui/recap_glyph_color_2x",
                action: "automation",
            }
        ],
    },
    cobblestoneGenerators: {
        searchable: true,
        title: "ui.ftb_sieves.book_cobblestone_generators.title",
        body: "ui.ftb_sieves.book_cobblestone_generators.body",
        buttons: [
            {
                label: "ui.ftb_sieves.book_generator_tiers.title",
                icon: "textures/ftb/sieves/book/recipes/gold_cobble_gen",
                action: "generatorTiers",
            },
            {
                label: "ui.ftb_sieves.book_main.button.back",
                icon: "textures/ui/recap_glyph_color_2x",
                action: "automation",
            }
        ],
    },
    generatorTiers: {
        searchable: true,
        title: "ui.ftb_sieves.book_generator_tiers.title",
        body: "ui.ftb_sieves.book_generator_tiers.body",
        buttons: [
            {
                label: "ui.ftb_sieves.book_main.button.back",
                icon: "textures/ui/recap_glyph_color_2x",
                action: "automation",
            }
        ],
    },
    basaltGenerators: {
        searchable: true,
        title: "ui.ftb_sieves.book_basalt_generators.title",
        body: "ui.ftb_sieves.book_basalt_generators.body",
        buttons: [
            {
                label: "ui.ftb_sieves.book_generator_tiers.title",
                icon: "textures/ftb/sieves/book/recipes/diamond_basalt_gen",
                action: "generatorTiers",
            },
            {
                label: "ui.ftb_sieves.book_main.button.back",
                icon: "textures/ui/recap_glyph_color_2x",
                action: "automation",
            }
        ],
    },
    compressedBlocks: {
        searchable: true,
        title: "ui.ftb_sieves.book_compressed_blocks.title",
        body: "ui.ftb_sieves.book_compressed_blocks.body",
        buttons: [
            {
                label: "ui.ftb_sieves.book_main.button.back",
                icon: "textures/ui/recap_glyph_color_2x",
                action: "automation",
            }
        ],
    },
    autoHammer: {
        searchable: true,
        title: "ui.ftb_sieves.book_auto_hammer.title",
        body: "ui.ftb_sieves.book_auto_hammer.body",
        buttons: [
            {
                label: "ui.ftb_sieves.book_main.button.back",
                icon: "textures/ui/recap_glyph_color_2x",
                action: "automation",
            }
        ],
    },
};
// Show a page of the book UI
function showMenu(player, menuKey, previous) {
    const menu = MENU_CONFIG[menuKey];
    const form = new ActionFormData()
        .title({ translate: menu.title, with: ["\n"] })
        .body({ translate: menu.body, with: ["\n"] });
    menu.buttons.forEach((button) => form.button({ translate: button.label, with: ["\n"] }, button.icon));
    form.show(player).then((response) => {
        if (response.canceled || response.selection === undefined) {
            if (previous?.length) {
                const bottomPrevious = previous.pop();
                if (bottomPrevious) {
                    showMenu(player, bottomPrevious, previous);
                    return;
                }
            }
            else {
                closeBook(player);
            }
            return;
        }
        const button = menu.buttons[response.selection];
        const { action: buttonAction, commands: buttonCommands, } = button;
        // Run commands (if any)
        if (buttonCommands) {
            for (const cmd of buttonCommands) {
                player.runCommand(cmd);
            }
        }
        //Handle navigation and custom actions
        if (buttonAction === "search") {
            showSearchMenu(player);
        }
        else if (buttonAction === "close") {
            closeBook(player);
        }
        else if (typeof buttonAction === "string") {
            if (actionHandlers[buttonAction]) {
                player.playSound("random.click");
                actionHandlers[buttonAction](player);
            }
            else if (MENU_CONFIG[buttonAction]) {
                player.playSound("item.book.page_turn");
                const previousMenus = previous ?? [];
                if (button.label !== "ui.ftb_sieves.book_main.button.back" &&
                    button.label !== "ui.ftb_sieves.book_main.button.exit") {
                    previousMenus.push(menuKey);
                }
                else {
                    previousMenus.pop();
                }
                showMenu(player, buttonAction, previousMenus);
            }
            else {
                console.warn(`Unknown action: ${buttonAction}`);
            }
        }
    });
}
// Search UI
function showSearchMenu(player) {
    const form = new ModalFormData()
        .title("Search")
        .textField("Enter a keyword to search:", "Keyword...");
    form.show(player).then((response) => {
        if (response.canceled || !response.formValues) {
            openBook(player, "bookMain");
            return;
        }
        const keyword = typeof response.formValues[0] === "string"
            ? response.formValues[0].toLowerCase()
            : "";
        if (!keyword) {
            player.sendMessage("§cPlease enter a valid keyword!");
            closeBook(player);
            return;
        }
        const results = searchMenus(keyword);
        if (results.length > 0) {
            showSearchResults(player, results);
        }
        else {
            player.sendMessage("§cNo results found for the keyword: " + keyword);
            closeBook(player);
        }
    });
}
function searchMenus(keyword) {
    const results = [];
    for (const [menuKey, menu] of Object.entries(MENU_CONFIG)) {
        if (!menu.searchable)
            continue;
        if (menu.title.toLowerCase().includes(keyword)) {
            results.push({ label: menu.title, menuKey });
        }
        for (const button of menu.buttons) {
            if (button.action === null ||
                button.action === "bookMain" ||
                button.label.toLowerCase().includes("back") ||
                button.label.toLowerCase().includes("exit")) {
                continue;
            }
            if (button.label.toLowerCase().includes(keyword)) {
                results.push({ label: button.label, menuKey });
            }
        }
    }
    return results;
}
function showSearchResults(player, results) {
    const form = new ActionFormData()
        .title("Search Results")
        .body("Select a result to open:");
    results.forEach((result) => {
        form.button({ translate: result.label, with: ["\n"] });
    });
    form.show(player).then((response) => {
        if (response.canceled || response.selection === undefined) {
            closeBook(player);
            return;
        }
        const selectedResult = results[response.selection];
        if (!selectedResult)
            return;
        const menuKey = selectedResult.menuKey;
        showMenu(player, menuKey);
    });
}
const playerLightWatcher = new Map();
function openBook(player, page) {
    player.playSound("item.book.page_turn");
    setInputPermission(player, InputPermissionCategory.Camera, false);
    showMenu(player, page);
    if (!playerLightWatcher.has(player.name)) {
        playerLightWatcher.set(player.name, new PlayerLightController(player));
    }
    playerLightWatcher.get(player.name).createLight();
}
function closeBook(player) {
    let bookSlotIndex = -1;
    const container = player.getComponent(EntityComponentTypes.Inventory);
    if (container && container.container) {
        for (let i = 0; i < container.container.size; i++) {
            const item = container.container.getItem(i);
            if (item &&
                item.typeId.startsWith("ftb_sieves:book_") &&
                item.typeId !== "ftb_sieves:book_main") {
                bookSlotIndex = i;
                break;
            }
        }
    }
    if (bookSlotIndex !== -1) {
        player.runCommand(`/replaceitem entity @s ${bookSlotIndex < 9 ? "slot.hotbar" : "slot.inventory"} ${bookSlotIndex} ftb_sieves:book_main`);
    }
    player.playSound("item.book.page_turn");
    setInputPermission(player, InputPermissionCategory.Camera, true);
    if (playerLightWatcher.has(player.name)) {
        playerLightWatcher.get(player.name).removeLight();
    }
}
class PlayerLightController {
    constructor(player) {
        this.player = player;
        this.watcher = null;
        this.removeLeftOverLight();
    }
    createLight() {
        this.removeLeftOverLight();
        const playerLocation = this.player.location;
        this.writeLocation(playerLocation);
        const block = this.player.dimension.getBlock(playerLocation);
        if (!block)
            return;
        block.setType("minecraft:light_block_15");
        this.watcher = system.runInterval(this.lightUpdater.bind(this), 20);
    }
    removeLight() {
        const location = this.readLocation();
        if (!location)
            return;
        const block = this.player.dimension.getBlock(location);
        if (block) {
            block.setType("minecraft:air");
        }
        this.clearWatcher();
    }
    removeLeftOverLight() {
        this.removeLight();
        this.clearWatcher();
    }
    lightUpdater() {
        const currentLocation = this.readLocation();
        if (!currentLocation)
            return;
        const { x, y, z } = this.player.location;
        const playerLocation = { x, y, z };
        if (playerLocation.x !== currentLocation.x ||
            playerLocation.y !== currentLocation.y ||
            playerLocation.z !== currentLocation.z) {
            this.writeLocation(playerLocation);
            const block = this.player.dimension.getBlock(currentLocation);
            if (block)
                block.setType("minecraft:air");
            const newBlock = this.player.dimension.getBlock(playerLocation);
            if (newBlock)
                newBlock.setType("minecraft:light_block_15");
        }
    }
    clearWatcher() {
        if (this.watcher !== null) {
            system.clearRun(this.watcher);
            this.watcher = null;
        }
    }
    writeLocation(location) {
        this.player.setDynamicProperty("ftb_sieves_light_location", `${location.x} ${location.y} ${location.z}`);
    }
    readLocation() {
        const property = this.player.getDynamicProperty("ftb_sieves_light_location");
        if (!property || typeof property !== "string")
            return null;
        const parts = property.split(" ");
        if (parts.length !== 3)
            return null;
        const [x, y, z] = parts.map(Number);
        return { x, y, z };
    }
}

export { MENU_CONFIG, closeBook, openBook };
