// Import necessary modules from Minecraft server API
import { world, system, BlockVolume } from "@minecraft/server";

const testBlocks = ["hb_hr:grim_stone_bricks_wall", "hb_hr:black_fence_gate", "hb_hr:cursed_fence_gate", "hb_hr:blight_fence_gate", "hb_hr:black_fence", "hb_hr:cursed_fence", "hb_hr:blight_fence", "hb_hr:blight_fence_inventory", "hb_hr:dead_bush", "hb_hr:dead_grass", "hb_hr:grim_stone_chunks", "hb_hr:cursed_bush", "hb_hr:cursed_grass", "hb_hr:cursed_tall_grass", "hb_hr:cursed_vine_carpet", "hb_hr:cursed_vine", "hb_hr:gravestone", "hb_hr:huge_bones", "hb_hr:small_bone_chunks", "hb_hr:black_grass", "hb_hr:black_moss_carpet", "hb_hr:black_rose", "hb_hr:black_tall_grass", "hb_hr:black_vine_carpet", "hb_hr:black_vine_carpet", "hb_hr:mushroom_litter"];

// export const denySnowComponent = {
//   onTick({ block }) {
//     if (!block.above()) return;
//     if (block.above().typeId.includes("minecraft:") && block.above().typeId.includes("snow")) {
//       world.sendMessage("replace snow");
//       block.dimension.runCommand("/setblock " + block.above().location.x + " " + block.above().location.y + " " + block.above().location.z + " air");
//     }
//   },
// };

// const customComponents = {
//   "hb_hr:deny_snow": denySnowComponent,
// };

// export function denySnow() {
//   // world.beforeEvents.worldInitialize.subscribe(({ blockComponentRegistry }) => {
//   //   for (const [componentName, componentImplementation] of Object.entries(customComponents)) {
//   //     blockComponentRegistry.registerCustomComponent(componentName, componentImplementation);
//   //   }
//   // });
// }

export function denySnowOnPlayer(player, curTick) {
  // world.afterEvents.weatherChange.subscribe((data) => {
  //   world.setDynamicProperty("hb_hr:weather_" + world.getDimension(data.dimension).id, data.newWeather);
  // });
  if (curTick % 5 != 0) return;
  // const currentWeather = world.getDynamicProperty("hb_hr:weather_" + player.dimension.id) ?? "Clear";
  // if (currentWeather != "Clear") {
  const volume = new BlockVolume({ x: player.location.x + 10, y: player.location.y + 2, z: player.location.z + 10 }, { x: player.location.x - 10, y: player.location.y - 2, z: player.location.z - 10 });

  const locations = volume.getBlockLocationIterator();

  for (const location of locations) {
    const block = player.dimension.getBlock(location);
    if (!block) continue;
    const blockType = block.typeId;
    if (testBlocks.includes(blockType)) {
      if (!block.above()) return;
      if (block.above().typeId.includes("minecraft:") && block.above().typeId.includes("snow")) {
        block.dimension.runCommand("/setblock " + block.above().location.x + " " + block.above().location.y + " " + block.above().location.z + " air");
      }
    }
    // }
  }
}
