import { world, system } from "@minecraft/server";
import { ActionFormData } from "@minecraft/server-ui";

const map_shop = "jam:npc_shop";

// Main Menu
function main(player, targetEntity) {
    const title = "§l§eEquipment Shop";
    const form = new ActionFormData()
        .title(title)
        .body(
            `§l§eWelcome to the Equipment Shop!\n\n§rGear up and be ready: Preparation is the key to success!\n\n§6🧯 Our prices are as hot as the fires you'll\nface but don't worry, our tools won't burn\nyour wallet!\n\n§aPro Tip: Don't let your gear go up in smoke double check your inventory before\nheading out!\n\n§e🤣 Joke: What do you call a funny firefighter? A real stand-up 'combustion'!`
        )
        .button('Exit\n§7[Exit menu]', 'textures/ui/crossout');

    form.show(player).then((r) => {
        // Exit is the only option, no handlers needed
    });
}

// Interaction Events
world.beforeEvents.playerInteractWithEntity.subscribe((data) => {
    const player = data.player;
    const targetEntity = data.target;

    if (targetEntity.typeId === map_shop) {
        system.run(() => main(player, targetEntity));
    }
});

world.afterEvents.entityHitEntity.subscribe((data) => {
    const player = data.damagingEntity;
    const targetEntity = data.hitEntity;

    if (player && targetEntity && targetEntity.typeId === map_shop) {
        system.run(() => main(player, targetEntity));
    }
});
