import { EquipmentSlot, world } from "@minecraft/server";
import ScriptingModule from "./scriptingModule";
import { getItemInSlot } from "./helper/utils";
import { namespace } from "../config/_config";
import { summonEntitiesInRange } from "./helper/summonFromLocation";
export class Hunt {
    constructor({ item, stages }) {
        this.item = item;
        this.stages = stages;
    }
    static register(hunt) {
        Hunt.hunts.push(hunt);
    }
    static initialize() {
        world.beforeEvents.worldInitialize.subscribe((event) => {
            event.blockComponentRegistry.registerCustomComponent(`${namespace}:hunt_interaction`, {
                onPlayerInteract: (event) => {
                    const player = event.player;
                    const block = player.getBlockFromViewDirection({ maxDistance: 5 })?.block;
                    if (!player || !block)
                        return;
                    const handItem = getItemInSlot(player, EquipmentSlot.Mainhand);
                    Hunt.hunts.forEach((hunt) => {
                        if (hunt.item !== handItem?.typeId)
                            return;
                        hunt.stages.forEach((stage) => {
                            const trailBlock = stage.trailBlock;
                            if (block.typeId !== trailBlock.id ||
                                block.permutation.getState(`${namespace}:hunt_stage`) !== trailBlock.state) {
                                return;
                            }
                            block.setType("air");
                            if (stage.onSearch) {
                                if (stage.onSearch.message) {
                                    player.sendMessage(`${stage.onSearch.message}`);
                                }
                                if (stage.onSearch.triggerStage) {
                                    summonEntitiesInRange(player.location, player.dimension, [
                                        {
                                            entity: "cc:hint",
                                            horizontalRadius: stage.onSearch.triggerStage.radius,
                                            tag: `${namespace}:${stage.onSearch.triggerStage.id}`,
                                            amount: {
                                                min: 1,
                                                max: 1,
                                            },
                                        },
                                    ]);
                                }
                                if (stage.onSearch.spawnMobs) {
                                    summonEntitiesInRange(player.location, player.dimension, stage.onSearch.spawnMobs);
                                }
                            }
                        });
                    });
                },
            });
        });
    }
}
Hunt.hunts = [];
export default class HuntModule extends ScriptingModule {
    constructor() {
        super("HuntModule");
    }
    onInitialize() {
        Hunt.register({
            item: "cc:search_glass",
            stages: [
                {
                    id: "big_foot_1",
                    trailBlock: {
                        id: "cc:big_foot_hint_1",
                        state: 1,
                    },
                    onSearch: {
                        triggerStage: {
                            id: "big_foot_2",
                            radius: 30,
                        },
                    },
                },
                {
                    id: "big_foot_2",
                    trailBlock: {
                        id: "cc:big_foot_hint_1",
                        state: 2,
                    },
                    onSearch: {
                        triggerStage: {
                            id: "big_foot_3",
                            radius: 30,
                        },
                    },
                },
                {
                    id: "big_foot_3",
                    trailBlock: {
                        id: "cc:big_foot_hint_1",
                        state: 3,
                    },
                    onSearch: {
                        spawnMobs: [
                            {
                                entity: "minecraft:enderman",
                                horizontalRadius: 10,
                                verticalHeight: 3,
                                amount: {
                                    min: 1,
                                    max: 1,
                                },
                            },
                        ],
                    },
                },
            ],
        });
        Hunt.initialize();
    }
}
