import { system, BlockPermutation } from "@minecraft/server";
const connectedFenceTypes = /* @__PURE__ */ new Set([
  "maca_probuilder:wooden_fence_corner",
  "maca_probuilder:wooden_fence_end",
  "maca_probuilder:wooden_fence_post",
  "maca_probuilder:wooden_fence_straight",
  "maca_probuilder:wooden_fence_straight_post",
  "maca_probuilder:wooden_fence_upwards",
  "maca_probuilder:iron_fence_corner",
  "maca_probuilder:iron_fence_end",
  "maca_probuilder:iron_fence_post",
  "maca_probuilder:iron_fence_straight",
  "maca_probuilder:iron_fence_diagonal",
  "maca_probuilder:iron_fence_upwards"
]);
const breakFenceComponent = {
  onPlayerDestroy(eventData) {
    const { block, player } = eventData;
    breakConnected(block);
    const inventory = player.getComponent("inventory");
    const selectedItem = inventory.container.getItem(player.selectedSlotIndex);
    if (selectedItem && (selectedItem.typeId.includes("shovel") || selectedItem.typeId.includes("hoe") || selectedItem.typeId.includes("_axe") || selectedItem.typeId.includes("_pickaxe"))) {
      system.runTimeout(() => {
        applyDurabilityDamage(
          player,
          selectedItem,
          inventory,
          player.selectedSlotIndex
        );
      }, 1);
    }
  }
};
function breakConnected(block) {
  const directions = ["north", "south", "east", "west"];
  directions.forEach((dir) => {
    const adjacentBlock = block[dir]();
    if (connectedFenceTypes.has(adjacentBlock.typeId)) {
      updateAdjacentBlock(adjacentBlock);
    }
  });
}
function updateAdjacentBlock(block) {
  const connections = getNeighborConnections(block);
  const connectionCount = Object.values(connections).filter(Boolean).length;
  let newType;
  let cardinalDirection;
  if (connectionCount === 0) {
    newType = resolveType(block, "_post");
  } else if (connectionCount === 1) {
    newType = resolveType(block, "_end");
    cardinalDirection = Object.keys(connections).find((dir) => connections[dir]);
  } else {
    return;
  }
  let blockPerm = BlockPermutation.resolve(newType);
  if (cardinalDirection) {
    blockPerm = blockPerm.withState("minecraft:cardinal_direction", cardinalDirection);
  }
  blockPerm = applyTexture(blockPerm, block, block);
  block.setPermutation(blockPerm);
}
function getNeighborConnections(block) {
  return {
    north: connectedFenceTypes.has(block.north().typeId),
    south: connectedFenceTypes.has(block.south().typeId),
    east: connectedFenceTypes.has(block.east().typeId),
    west: connectedFenceTypes.has(block.west().typeId),
    up: connectedFenceTypes.has(block.above().typeId),
    down: connectedFenceTypes.has(block.below().typeId)
  };
}
function resolveType(block, type) {
  const prefix = "maca_probuilder:";
  let blockPrefix;
  if (block.typeId.includes("wooden_fence")) {
    blockPrefix = "wooden_fence";
  } else if (block.typeId.includes("iron_fence")) {
    blockPrefix = "iron_fence";
  }
  const fullTypeId = `${prefix}${blockPrefix}${type}`;
  return fullTypeId;
}
function applyDurabilityDamage(player, item, inventory, slotIndex) {
  const durabilityComponent = item.getComponent("minecraft:durability");
  if (durabilityComponent) {
    const { unbreakingLevel } = getRelevantEnchantments(item);
    if (Math.random() < 1 / (unbreakingLevel + 1)) {
      const newDamage = durabilityComponent.damage + 1;
      if (newDamage >= durabilityComponent.maxDurability) {
        inventory.container.setItem(slotIndex, void 0);
        player.playSound("random.break");
      } else {
        durabilityComponent.damage = newDamage;
        inventory.container.setItem(slotIndex, item);
      }
    }
  }
}
function getRelevantEnchantments(item) {
  let unbreakingLevel = 0;
  try {
    const enchantableComponent = item.getComponent("minecraft:enchantable");
    if (enchantableComponent) {
      const enchantments = enchantableComponent.getEnchantments();
      for (const enchant of enchantments) {
        if (enchant.type.id === "unbreaking") {
          unbreakingLevel = enchant.level;
        }
      }
    }
  } catch (error) {
  }
  return { unbreakingLevel };
}
function applyTexture(blockPerm, block, item) {
  const currentTextureBit0 = block.permutation.getState("maca_probuilder:texture_bit_0") || 0;
  const currentTextureBit1 = block.permutation.getState("maca_probuilder:texture_bit_1") || 0;
  blockPerm = blockPerm.withState(
    "maca_probuilder:texture_bit_0",
    currentTextureBit0
  );
  if (!item.typeId.includes("wooden")) {
    blockPerm = blockPerm.withState(
      "maca_probuilder:texture_bit_1",
      currentTextureBit1
    );
  }
  return blockPerm;
}
export {
  breakFenceComponent
};
