import * as mc from "@minecraft/server";
import * as utils from "./utils/index";
export function initializeGhastTargetting() {
    mc.world.afterEvents.entitySpawn.subscribe(onGhastSpawn);
    mc.world.afterEvents.entityRemove.subscribe(onGhastDespawn, { entityTypes: ['minecraft:ghast'] });
    utils.nether.getEntities({ type: 'minecraft:ghast' }).forEach(addGhastTarget);
    utils.waitForLoopEvent(10, () => {
        ghastTargetGroups.forEach(group => {
            if (group.ghast.isValid() && group.ghastTarget.isValid()) {
                group.ghastTarget.teleport(group.ghast.location);
            }
        });
        return false;
    });
}
const ghastTargetGroups = [];
function onGhastSpawn(event) {
    if (event.entity.typeId === 'minecraft:ghast') {
        utils.mclog('Added Ghast Target');
        addGhastTarget(event.entity);
    }
}
function addGhastTarget(ghast) {
    const ghastTarget = ghast.dimension.spawnEntity('ldz_crcomp:ghast_target', ghast.location);
    ghastTargetGroups.push({ ghast: ghast, ghastTarget });
}
function onGhastDespawn(event) {
    const ghastTargetGroup = ghastTargetGroups.find(group => group.ghast.id === event.removedEntityId);
    if (ghastTargetGroup) {
        utils.mclog('Removed Ghast Target');
        utils.removeFromArray(ghastTargetGroups, ghastTargetGroup);
        ghastTargetGroup.ghastTarget.remove();
    }
}
