import { world, DynamicPropertiesDefinition } from "@minecraft/server";

/**
 * Registers the dynamic properties used by the PvP/PvE battle search flow.
 * This is safe to ship even if you don't use PvP (props are just reserved).
 */
// worldInitialize is exposed under afterEvents in the Script API.
// Using beforeEvents here will throw and can prevent the whole script module from loading.
world.afterEvents.worldInitialize.subscribe((ev) => {
  try {
    const def = new DynamicPropertiesDefinition();

    // mode: "pve" | "pvp"
    def.defineString("pr:battle_mode", 8);

    // pending challenge tracking
    def.defineString("pr:pvp_token", 32);
    def.defineString("pr:pvp_from", 64);
    def.defineString("pr:pvp_to", 64);
    def.defineString("pr:pvp_cpoke", 64);
    def.defineString("pr:pvp_dpoke", 64);

    // NOTE: pr:looking_for_battle already used by the pack; we don't redefine it here
    // to avoid conflicts if it is registered elsewhere.

    ev.propertyRegistry.registerPlayerDynamicProperties(def);
  } catch (e) {
    // If registration fails (older API), we silently ignore to avoid crashing.
  }
});
