import { world } from "@minecraft/server";
import { StructureDetector } from "./structureDetector.js";

const SKULL_BLOCK_TAG = "hb_hr:banned_wither_skull"
const ONLY_SPAWN_ON_ARENA = true; // Should wither only spawn on the arena?

// 2D pattern
const detector = new StructureDetector(
	{
		"*": undefined,
		"a": "minecraft:air",
		"B": "hb_hr:banned_wither_block",
		"C": "hb_hr:banned_wither_block_head",
		"#": SKULL_BLOCK_TAG // skulls block tag - leave it last. Proper skull variants checking done in structureDetector script
	},
	[
		["aBa"],
		["CCC"],
		["###"]
	],
	(dimension, pos) => {
		// banned wither spawn trigger command
		dimension.runCommand(`/execute positioned ${pos.x} ${pos.y} ${pos.z} as @e[type=hb_hr:banned_wither_marker,r=16,c=1] run scriptevent hb_hr:banned_wither spawn`);
	}
);

export function banned_wither_structure_detector() {
	world.afterEvents.playerPlaceBlock.subscribe(({ block, dimension }) => {
		if (block?.permutation?.hasTag(SKULL_BLOCK_TAG)) {

			// Check if its on arena
			if (ONLY_SPAWN_ON_ARENA) {
				const markers = dimension.getEntities({
					"type": "hb_hr:banned_wither_marker",
					"closest": true,
					"location": block?.location,
					"maxDistance": 6
				});

				if (markers.length == 0) return;
			}

			detector.detectStructure(dimension, block);
		}
	});
}
