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

// Core systems
import "./allanz/main.js";

// NOTE:
// "@minecraft/server-admin" is not available in many environments (single-player, realms, some hosts).
// Importing it will hard-crash the entire script module.
// If you need server-transfer again later, we can re-add it as an optional *separate* add-on.
// import "./allanz/transfer-server.js";

// Features
import "./vendor_interact.js";
import "./fishing_pack/main.js";
import "./plushie_place_entities.js";
import "./trainer_menu.js";
import "./fishing.js";

import "./mega_evolution.js";
const DEBUG = true;

const __safeHasTag = (ent, tag) => {
  try {
    if (!ent) return false;
    // Avoid calling hasTag on invalid entities (this throws InvalidEntityError)
    if (typeof ent.isValid === "function") {
      let ok = false;
      try { ok = ent.isValid(); } catch { return false; }
      if (!ok) return false;
    }
    if (typeof ent.hasTag !== "function") return false;
    try { return ent.hasTag(tag) === true; } catch { return false; }
  } catch {
    return false;
  }
};

const ENTITY_COMMANDS = [
	{ id: "pr:pokemall_oak", command: "structure load mystructure:PokeMall_Oak ~ ~ ~" },
	{ id: "pr:acacia_village", command: "structure load mystructure:Acacia_village ~ ~-2 ~" },
	{ id: "pr:cherry_village", command: "structure load mystructure:cherry_village ~ ~-2 ~" },
	{ id: "pr:nether_village", command: "structure load mystructure:nether_village ~ ~-2 ~" },
	{ id: "pr:oak_village", command: "structure load mystructure:oak_village ~ ~-2 ~" },
	{ id: "pr:pokecenter_plans", command: "structure load mystructure:Pokecenter_plans ~ ~-2 ~" },
	{ id: "pr:pokemall_cave", command: "structure load mystructure:PokeMall_cave ~ ~ ~" }
];

world.afterEvents.entitySpawn.subscribe(event => {
	const entity = event.entity;

	// First: only run this handler for our structure-marker entities.
	let typeId = "";
	try { typeId = entity?.typeId ?? ""; } catch { return; }
	const data = ENTITY_COMMANDS.find(e => e.id === typeId);
	if (!data) return;

	// Entities can be removed the same tick they "spawn" (failed spawn, immediate despawn, etc.)
	// Guard every entity call to avoid InvalidEntityError.
	try {
		if (!entity) return;
		if (typeof entity.isValid === "function" && !entity.isValid()) return;
		if (__safeHasTag(entity, "structure_loaded")) return;
	} catch {
		return;
	}
let dimension;
	let x, y, z;
	try {
		dimension = entity.dimension;
		({ x, y, z } = entity.location);
	} catch {
		return;
	}

	const cmd = `execute positioned ${x} ${y} ${z} run ${data.command}`;

	try {
		if (typeof dimension.runCommandAsync === "function") dimension.runCommandAsync(cmd);
		else dimension.runCommand(cmd);
	} catch (e) {
		if (DEBUG) console.warn(`[PokeReviva] Structure load failed: ${cmd}`, e);
	}

	try {
		if (typeof entity.isValid === "function" && !entity.isValid()) return;
		entity.addTag("structure_loaded");
		entity.triggerEvent("minecraft:despawn");
	} catch {
		// ignore
	}
});

// Bikes
import "./bikes.js";
