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

/**
 * Registers dynamic properties needed for multi-Pokémon "sent out" support.
 * Safe to load even if a property was registered elsewhere (wrapped in try/catch).
 */
world.afterEvents.worldInitialize.subscribe((ev) => {
  try {
    const def = new DynamicPropertiesDefinition();

    // Existing single-active properties (ensure reserved)
    try { def.defineBoolean("pr:sended_pokemon"); } catch {}
    try { def.defineString("pr:sended_pokemon_id", 128); } catch {}
    try { def.defineNumber("pr:sended_pokemon_slot"); } catch {}

    // Per-slot "out" tracking (slots are 1..6 in UI)
    for (let i = 1; i <= 6; i++) {
      try { def.defineBoolean(`pr:out_slot_${i}`); } catch {}
      try { def.defineString(`pr:out_id_${i}`, 128); } catch {}
    }

    // Apply to player entities
    ev.propertyRegistry.registerEntityTypeDynamicProperties(def, "minecraft:player");
  } catch (e) {
    // Don't crash if API is unavailable or registration fails on older versions.
    try { console.warn("pokemon_out_dynamic_properties registration skipped:", e); } catch {}
  }
});
