// scripts/bestiary/killTracker.js
import { world } from "@minecraft/server";
import { ENTRY_BY_ID } from "./entries.js";
import { markKilled, hasKilled } from "./storage.js";
import { checkFull, checkAdvanced } from "./checkAdvanced.js";

export function initKillTracker() {

  world.afterEvents.entityDie.subscribe((ev) => {

    const dead = ev.deadEntity;
    if (!dead) return;

    const entry = ENTRY_BY_ID.get(dead.typeId);
    if (!entry) return;

    const killer = ev.damageSource?.damagingEntity;
    if (!killer || killer.typeId !== "minecraft:player") return;

    if (!hasKilled(killer, entry.id)) {

      markKilled(killer, entry.id);
      checkFull(killer, entry);
      checkAdvanced(killer, entry);
    }
  });
}