import { world, system } from "@minecraft/server";
import { particleBlocks } from "../main";
world.afterEvents.blockExplode.subscribe((event) => {
  const { explodedBlockPermutation: perm, dimension, block } = event;
  if (!perm.hasTag("maca_probuilder:is_connected_vertical"))
    return;
  const loc = block.location;
  system.runTimeout(() => {
    handleBlockExplosion(perm, dimension, loc);
  }, 1);
});
world.beforeEvents.worldInitialize.subscribe((eventData) => {
  eventData.blockComponentRegistry.registerCustomComponent("maca_probuilder:vertical_connected_place", {
    onPlace(event) {
      const block = event.block;
      const dimension = event.dimension;
      if (block.hasTag("maca_probuilder:is_connected_vertical")) {
        placeConnected(dimension, block, false);
      }
    },
    onPlayerDestroy(event) {
      const { block, dimension, destroyedBlockPermutation } = event;
      if (destroyedBlockPermutation.hasTag("maca_probuilder:is_connected_vertical")) {
        breakConnected(dimension, block, destroyedBlockPermutation.type.id);
      }
    },
    onTick(event) {
      const { block } = event;
      const loc = block?.location;
      const blockLocationString = `${loc.x},${loc.y},${loc.z}`;
      if (particleBlocks.has(blockLocationString) || block?.typeId === "maca_probuilder:chimney") {
        const loc2 = block.center();
        block.dimension.spawnParticle("maca_probuilder:chimney_smoke_particle", { x: loc2.x, y: loc2.y + 0.5, z: loc2.z });
        block.dimension.playSound("block.campfire.crackle", block.center());
      }
    }
  });
});
function handleBlockExplosion(perm, dimension, loc) {
  const blockAbove = dimension.getBlock({ x: loc.x, y: loc.y + 1, z: loc.z });
  const blockBelow = dimension.getBlock({ x: loc.x, y: loc.y - 1, z: loc.z });
  const block = dimension.getBlock(loc);
  if (blockBelow.hasTag("maca_probuilder:is_connected_vertical")) {
    breakConnected(dimension, block, perm.type.id);
  } else if (blockAbove.hasTag("maca_probuilder:is_connected_vertical")) {
    breakConnected(dimension, block, perm.type.id);
  }
}
function isConnected(blockAdj, block) {
  if (blockAdj === block) {
    return true;
  } else {
    return false;
  }
}
function placeConnected(dimension, block, connectAll) {
  const blockAbove = dimension.getBlock(block.above(1));
  const blockBelow = dimension.getBlock(block.below(1));
  const blockType = block.typeId;
  const blockBType = blockBelow.typeId;
  const blockAType = blockAbove.typeId;
  const hasBlockAbove = connectAll ? !(blockAbove.isAir || blockAbove.isLiquid || ignoreList.has(blockAType)) : isConnected(blockAType, blockType);
  const hasBlockBelow = connectAll ? !(blockBelow.isAir || blockBelow.isLiquid || ignoreList.has(blockBType)) : isConnected(blockBType, blockType);
  if (!hasBlockAbove && !hasBlockBelow) {
    return;
  }
  if (hasBlockAbove && !connectAll) {
    blockAbove.setPermutation(blockAbove.permutation.withState("maca_probuilder:connect_down", false));
  }
  if (hasBlockBelow && !connectAll) {
    blockBelow.setPermutation(blockBelow.permutation.withState("maca_probuilder:connect_up", false));
  }
  if (hasBlockAbove && hasBlockBelow) {
    block.setPermutation(
      block.permutation.withState("maca_probuilder:connect_up", false).withState("maca_probuilder:connect_down", false)
    );
  } else if (hasBlockAbove && !hasBlockBelow) {
    block.setPermutation(
      block.permutation.withState("maca_probuilder:connect_up", false).withState("maca_probuilder:connect_down", true)
    );
  } else if (!hasBlockAbove && hasBlockBelow) {
    block.setPermutation(
      block.permutation.withState("maca_probuilder:connect_up", true).withState("maca_probuilder:connect_down", false)
    );
  }
}
function breakConnected(dimension, block, type) {
  const blockAbove = dimension.getBlock(block.above(1));
  const blockBelow = dimension.getBlock(block.below(1));
  const blockBType = blockBelow.typeId;
  const blockAType = blockAbove.typeId;
  const hasBlockAbove = isConnected(blockAType, type);
  const hasBlockBelow = isConnected(blockBType, type);
  if (!hasBlockAbove && !hasBlockBelow)
    return;
  system.runTimeout(() => {
    if (hasBlockAbove) {
      blockAbove.setPermutation(blockAbove.permutation.withState("maca_probuilder:connect_down", true));
    }
    if (hasBlockBelow) {
      blockBelow.setPermutation(blockBelow.permutation.withState("maca_probuilder:connect_up", true));
    }
  }, 1);
}
const ignoreList = /* @__PURE__ */ new Set([
  "minecraft:acacia_button",
  "minecraft:acacia_door",
  "minecraft:acacia_fence",
  "minecraft:acacia_pressure_plate",
  "minecraft:acacia_slab",
  "minecraft:acacia_hanging_sign",
  "minecraft:acacia_trapdoor",
  "minecraft:acacia_sapling",
  "minecraft:oak_button",
  "minecraft:oak_door",
  "minecraft:oak_fence",
  "minecraft:oak_pressure_plate",
  "minecraft:oak_slab",
  "minecraft:oak_hanging_sign",
  "minecraft:oak_trapdoor",
  "minecraft:oak_sapling",
  "minecraft:dark_oak_button",
  "minecraft:dark_oak_door",
  "minecraft:dark_oak_fence",
  "minecraft:dark_oak_pressure_plate",
  "minecraft:dark_oak_slab",
  "minecraft:dark_oak_hanging_sign",
  "minecraft:dark_oak_trapdoor",
  "minecraft:dark_oak_sapling",
  "minecraft:birch_button",
  "minecraft:birch_door",
  "minecraft:birch_fence",
  "minecraft:birch_pressure_plate",
  "minecraft:birch_slab",
  "minecraft:birch_hanging_sign",
  "minecraft:birch_trapdoor",
  "minecraft:birch_sapling",
  "minecraft:cherry_button",
  "minecraft:cherry_door",
  "minecraft:cherry_fence",
  "minecraft:cherry_pressure_plate",
  "minecraft:cherry_slab",
  "minecraft:cherry_hanging_sign",
  "minecraft:cherry_trapdoor",
  "minecraft:cherry_sapling",
  "minecraft:jungle_button",
  "minecraft:jungle_door",
  "minecraft:jungle_fence",
  "minecraft:jungle_pressure_plate",
  "minecraft:jungle_slab",
  "minecraft:jungle_hanging_sign",
  "minecraft:jungle_trapdoor",
  "minecraft:jungle_sapling",
  "minecraft:spruce_button",
  "minecraft:spruce_door",
  "minecraft:spruce_fence",
  "minecraft:spruce_pressure_plate",
  "minecraft:spruce_slab",
  "minecraft:spruce_hanging_sign",
  "minecraft:spruce_trapdoor",
  "minecraft:spruce_sapling",
  "minecraft:pale_oak_button",
  "minecraft:pale_oak_door",
  "minecraft:pale_oak_fence",
  "minecraft:pale_oak_pressure_plate",
  "minecraft:pale_oak_slab",
  "minecraft:pale_oak_hanging_sign",
  "minecraft:pale_oak_trapdoor",
  "minecraft:pale_oak_sapling",
  "minecraft:mangrove_button",
  "minecraft:mangrove_door",
  "minecraft:mangrove_fence",
  "minecraft:mangrove_pressure_plate",
  "minecraft:mangrove_slab",
  "minecraft:mangrove_hanging_sign",
  "minecraft:mangrove_trapdoor",
  "minecraft:mangrove_sapling",
  "minecraft:bamboo_button",
  "minecraft:bamboo_door",
  "minecraft:bamboo_fence",
  "minecraft:bamboo_pressure_plate",
  "minecraft:bamboo_slab",
  "minecraft:bamboo_mosaic_slab",
  "minecraft:bamboo_hanging_sign",
  "minecraft:bamboo_trapdoor",
  "minecraft:bamboo_sapling",
  // nether wood
  "minecraft:warped_button",
  "minecraft:warped_door",
  "minecraft:warped_fence",
  "minecraft:warped_pressure_plate",
  "minecraft:warped_slab",
  "minecraft:warped_hanging_sign",
  "minecraft:warped_trapdoor",
  "minecraft:warped_sapling",
  "minecraft:crimson_button",
  "minecraft:crimson_door",
  "minecraft:crimson_fence",
  "minecraft:crimson_pressure_plate",
  "minecraft:crimson_slab",
  "minecraft:crimson_hanging_sign",
  "minecraft:crimson_trapdoor",
  "minecraft:crimson_sapling",
  // chests
  "minecraft:chest",
  "minecraft:trapped_chest",
  "minecraft:ender_chest",
  // shulker boxes
  "minecraft:white_shulker_box",
  "minecraft:orange_shulker_box",
  "minecraft:magenta_shulker_box",
  "minecraft:light_blue_shulker_box",
  "minecraft:yellow_shulker_box",
  "minecraft:lime_shulker_box",
  "minecraft:pink_shulker_box",
  "minecraft:gray_shulker_box",
  "minecraft:silver_shulker_box",
  "minecraft:cyan_shulker_box",
  "minecraft:undyed_shulker_box",
  "minecraft:blue_shulker_box",
  "minecraft:brown_shulker_box",
  "minecraft:green_shulker_box",
  "minecraft:red_shulker_box",
  "minecraft:black_shulker_box",
  // plants
  "minecraft:dandelion",
  "minecraft:poppy",
  "minecraft:blue_orchid",
  "minecraft:allium",
  "minecraft:azure_bluet",
  "minecraft:red_tulip",
  "minecraft:orange_tulip",
  "minecraft:white_tulip",
  "minecraft:pink_tulip",
  "minecraft:oxeye_daisy",
  "minecraft:eyeblossom",
  "minecraft:sunflower",
  "minecraft:lilac",
  "minecraft:rose_bush",
  "minecraft:peony",
  "minecraft:cornflower",
  "minecraft:lily_of_the_valley",
  "minecraft:wither_rose",
  "minecraft:torchflower",
  "minecraft:pitcher_plant",
  "minecraft:grass",
  "minecraft:tall_grass",
  "minecraft:tall_dry_grass",
  "minecraft:short_dry_grass",
  "minecraft:firefly_bush",
  "minecraft:fern",
  "minecraft:large_fern",
  "minecraft:deadbush",
  "minecraft:short_grass",
  "minecraft:sweet_berry_bush",
  "minecraft:seagrass",
  "minecraft:kelp",
  "minecraft:tall_seagrass",
  "minecraft:azalea",
  "minecraft:flowering_azalea",
  "minecraft:cocoa",
  "minecraft:reeds",
  // carpets
  "minecraft:white_carpet",
  "minecraft:orange_carpet",
  "minecraft:magenta_carpet",
  "minecraft:light_blue_carpet",
  "minecraft:yellow_carpet",
  "minecraft:lime_carpet",
  "minecraft:pink_carpet",
  "minecraft:gray_carpet",
  "minecraft:light_gray_carpet",
  "minecraft:cyan_carpet",
  "minecraft:purple_carpet",
  "minecraft:blue_carpet",
  "minecraft:brown_carpet",
  "minecraft:green_carpet",
  "minecraft:red_carpet",
  "minecraft:black_carpet",
  // Regular Copper
  "minecraft:copper_slab",
  "minecraft:copper_trapdoor",
  "minecraft:copper_door",
  "minecraft:copper_button",
  // Weathered Copper
  "minecraft:weathered_copper_slab",
  "minecraft:weathered_copper_trapdoor",
  "minecraft:weathered_copper_door",
  "minecraft:weathered_copper_button",
  // Oxidized Copper
  "minecraft:oxidized_copper_slab",
  "minecraft:oxidized_copper_trapdoor",
  "minecraft:oxidized_copper_door",
  "minecraft:oxidized_copper_button",
  // Exposed Copper 
  "minecraft:exposed_copper_slab",
  "minecraft:exposed_copper_trapdoor",
  "minecraft:exposed_copper_door",
  "minecraft:exposed_copper_button",
  // Waxed Copper Variants
  "minecraft:waxed_copper_slab",
  "minecraft:waxed_copper_trapdoor",
  "minecraft:waxed_copper_door",
  "minecraft:waxed_copper_button",
  "minecraft:waxed_weathered_copper_slab",
  "minecraft:waxed_weathered_copper_trapdoor",
  "minecraft:waxed_weathered_copper_door",
  "minecraft:waxed_weathered_copper_button",
  "minecraft:waxed_oxidized_copper_slab",
  "minecraft:waxed_oxidized_copper_trapdoor",
  "minecraft:waxed_oxidized_copper_door",
  "minecraft:waxed_oxidized_copper_button",
  "minecraft:waxed_exposed_copper_slab",
  "minecraft:waxed_exposed_copper_trapdoor",
  "minecraft:waxed_exposed_copper_door",
  "minecraft:waxed_exposed_copper_button",
  // beds      
  "minecraft:white_bed",
  "minecraft:orange_bed",
  "minecraft:magenta_bed",
  "minecraft:light_blue_bed",
  "minecraft:yellow_bed",
  "minecraft:lime_bed",
  "minecraft:pink_bed",
  "minecraft:gray_bed",
  "minecraft:light_gray_bed",
  "minecraft:cyan_bed",
  "minecraft:purple_bed",
  "minecraft:blue_bed",
  "minecraft:brown_bed",
  "minecraft:green_bed",
  "minecraft:red_bed",
  "minecraft:black_bed",
  // rest of slab types
  "minecraft:stone_slab",
  "minecraft:smooth_stone_slab",
  "minecraft:cobblestone_slab",
  "minecraft:smooth_sandstone_slab",
  "minecraft:sandstone_slab",
  "minecraft:cut_sandstone_slab",
  "minecraft:granite_slab",
  "minecraft:polished_granite_slab",
  "minecraft:diorite_slab",
  "minecraft:polished_diorite_slab",
  "minecraft:andesite_slab",
  "minecraft:polished_andesite_slab",
  "minecraft:red_sandstone_slab",
  "minecraft:cut_red_sandstone_slab",
  "minecraft:mossy_cobblestone_slab",
  "minecraft:mossy_stone_brick_slab",
  "minecraft:stone_brick_slab",
  "minecraft:nether_brick_slab",
  "minecraft:red_nether_brick_slab",
  "minecraft:blackstone_slab",
  "minecraft:polished_blackstone_slab",
  "minecraft:polished_blackstone_brick_slab",
  "minecraft:end_stone_brick_slab",
  "minecraft:quartz_slab",
  "minecraft:smooth_quartz_slab",
  "minecraft:prismarine_slab",
  "minecraft:prismarine_brick_slab",
  "minecraft:dark_prismarine_slab",
  "minecraft:deepslate_slab",
  "minecraft:polished_deepslate_slab",
  "minecraft:deepslate_brick_slab",
  "minecraft:deepslate_tile_slab",
  // nether plants, ladder, rails
  "minecraft:nether_wart",
  "minecraft:crimson_roots",
  "minecraft:warped_roots",
  "minecraft:nether_sprouts",
  "minecraft:weeping_vines",
  "minecraft:twisting_vines",
  "minecraft:crimson_fungus",
  "minecraft:warped_fungus",
  "minecraft:ladder",
  "minecraft:vines",
  "minecraft:weeping_vines",
  "minecraft:twisting_vines",
  "minecraft:rail",
  "minecraft:powered_rail",
  "minecraft:detector_rail",
  "minecraft:activator_rail",
  // light sources
  "minecraft:torch",
  "minecraft:soul_torch",
  "minecraft:redstone_torch",
  "minecraft:campfire",
  "minecraft:soul_campfire",
  "minecraft:fire",
  "minecraft:lantern",
  "minecraft:soul_lantern",
  "minecraft:white_candle",
  "minecraft:orange_candle",
  "minecraft:magenta_candle",
  "minecraft:light_blue_candle",
  "minecraft:yellow_candle",
  "minecraft:lime_candle",
  "minecraft:pink_candle",
  "minecraft:gray_candle",
  "minecraft:light_gray_candle",
  "minecraft:cyan_candle",
  "minecraft:purple_candle",
  "minecraft:blue_candle",
  "minecraft:brown_candle",
  "minecraft:green_candle",
  "minecraft:red_candle",
  "minecraft:black_candle",
  // rest of pressure plates
  "minecraft:stone_pressure_plate",
  "minecraft:polished_blackstone_pressure_plate",
  "minecraft:light_weighted_pressure_plate",
  "minecraft:heavy_weighted_pressure_plate",
  // other stuff in items category
  "minecraft:scaffolding",
  "minecraft:anvil",
  "minecraft:grindstone",
  "minecraft:lectern",
  "minecraft:enchanting_table",
  "minecraft:brewing_stand",
  "minecraft:composter",
  "minecraft:flower_pot",
  "minecraft:cobweb",
  "minecraft:bell",
  "minecraft:sea_pickle",
  "minecraft:glow_frame",
  "minecraft:frame",
  "minecraft:snow_layer"
]);
export {
  placeConnected
};
