import {
  system,
  BlockPermutation,
  EquipmentSlot
} from "@minecraft/server";
import { getBlockTextures } from "../main.js";
import { guiTextureSelect } from "../gui/index.js";
import { getOppositeDirection } from "../connected/connected.js";
const connectedSunshades = /* @__PURE__ */ new Set([
  "maca_probuilder:sunshade_round_side_left",
  "maca_probuilder:sunshade_round_side_right",
  "maca_probuilder:sunshade_round_middle",
  "maca_probuilder:sunshade_round_singular"
]);
const breakSunshade = {
  onPlayerDestroy(event) {
    const { block, dimension, destroyedBlockPermutation } = event;
    const cardinalDirection = destroyedBlockPermutation.getState("minecraft:cardinal_direction");
    breakConnected(dimension, block, cardinalDirection);
  }
};
const connectSunshade = {
  beforeOnPlayerPlace(event) {
    const { block, dimension, player, face } = event;
    event.cancel = true;
    let cardinalDirection = getOppositeDirection(face.toLowerCase());
    system.runTimeout(() => {
      const { blockLeft, blockRight } = getAdjacentBlocks(dimension, cardinalDirection, block);
      const hasBlockLeft = isConnected(blockLeft);
      const hasBlockRight = isConnected(blockRight);
      const newType = getSunshadeType(hasBlockLeft, hasBlockRight, cardinalDirection);
      const equipment = player.getComponent("equippable");
      const selectedItem = equipment.getEquipment("Mainhand");
      let storedLore;
      if (selectedItem && selectedItem !== void 0) {
        storedLore = selectedItem.getLore()[0];
      } else {
        storedLore = player.getDynamicProperty("maca_probuilder:stored_lore");
      }
      if (player.getGameMode() !== "creative") {
        if (selectedItem.amount > 1) {
          selectedItem.amount -= 1;
          equipment.setEquipment(EquipmentSlot.Mainhand, selectedItem);
        } else {
          equipment.setEquipment(EquipmentSlot.Mainhand, void 0);
        }
      }
      let blockPerm = BlockPermutation.resolve(selectedItem.typeId);
      blockPerm = blockPerm.withState("minecraft:cardinal_direction", cardinalDirection);
      let textureBit0 = 0;
      let textureBit1 = 0;
      if (storedLore !== void 0) {
        const textures = getBlockTextures(BlockPermutation.resolve(selectedItem.typeId));
        let index = textures.findIndex(
          (x) => x.name === storedLore || x.item === storedLore
        );
        if (index !== -1) {
          textureBit0 = index % 16;
          if (textures.length > 16) {
            textureBit1 = Math.floor(index / 16);
          }
        }
      }
      let newPermutation = BlockPermutation.resolve(newType);
      newPermutation = newPermutation.withState(
        "minecraft:cardinal_direction",
        cardinalDirection
      );
      newPermutation = newPermutation.withState(
        "maca_probuilder:texture_bit_0",
        textureBit0
      );
      newPermutation = newPermutation.withState(
        "maca_probuilder:texture_bit_1",
        textureBit1
      );
      dimension.getBlock(block.location).setPermutation(newPermutation);
      updateAdjacentBlock(block, dimension, cardinalDirection);
      if (player.isSneaking) {
        const placedBlock = dimension.getBlock(block.location);
        const validTextures = getBlockTextures(placedBlock).map((x) => x.name);
        guiTextureSelect(
          player,
          placedBlock,
          validTextures,
          "",
          validTextures.findIndex((x) => x === storedLore)
        );
      }
    });
  }
};
function isConnected(blockAdj) {
  return connectedSunshades.has(blockAdj.typeId);
}
function getAdjacentBlocks(dimension, cardinalDirection, block) {
  switch (cardinalDirection.toLowerCase()) {
    case "north":
      return {
        blockLeft: dimension.getBlock(block.west()),
        blockRight: dimension.getBlock(block.east())
      };
    case "south":
      return {
        blockLeft: dimension.getBlock(block.east()),
        blockRight: dimension.getBlock(block.west())
      };
    case "east":
      return {
        blockLeft: dimension.getBlock(block.north()),
        blockRight: dimension.getBlock(block.south())
      };
    case "west":
      return {
        blockLeft: dimension.getBlock(block.south()),
        blockRight: dimension.getBlock(block.north())
      };
  }
}
function getSunshadeType(hasLeft, hasRight) {
  if (hasLeft && hasRight)
    return "maca_probuilder:sunshade_round_middle";
  if (hasLeft)
    return "maca_probuilder:sunshade_round_side_right";
  if (hasRight)
    return "maca_probuilder:sunshade_round_side_left";
  return "maca_probuilder:sunshade_round_singular";
}
function updateAdjacentBlock(block, dimension, cardinalDirection) {
  const { blockLeft, blockRight } = getAdjacentBlocks(dimension, cardinalDirection, block);
  const blocksToUpdate = [block, blockLeft, blockRight];
  blocksToUpdate.forEach((sunshadeBlock) => {
    if (!connectedSunshades.has(sunshadeBlock.typeId))
      return;
    const { blockLeft: leftBlock, blockRight: rightBlock } = getAdjacentBlocks(dimension, cardinalDirection, sunshadeBlock);
    const hasBlockLeft = isConnected(leftBlock);
    const hasBlockRight = isConnected(rightBlock);
    const newType = getSunshadeType(hasBlockLeft, hasBlockRight, cardinalDirection);
    if (sunshadeBlock.typeId !== newType) {
      let newPermutation = BlockPermutation.resolve(newType);
      newPermutation = newPermutation.withState(
        "minecraft:cardinal_direction",
        sunshadeBlock.permutation.getState("minecraft:cardinal_direction")
      );
      newPermutation = newPermutation.withState(
        "maca_probuilder:texture_bit_0",
        sunshadeBlock.permutation.getState("maca_probuilder:texture_bit_0")
      );
      newPermutation = newPermutation.withState(
        "maca_probuilder:texture_bit_1",
        sunshadeBlock.permutation.getState("maca_probuilder:texture_bit_1")
      );
      dimension.getBlock(sunshadeBlock.location).setPermutation(newPermutation);
    }
  });
}
function breakConnected(dimension, block, cardinalDirection) {
  const { blockLeft, blockRight } = getAdjacentBlocks(dimension, cardinalDirection, block);
  const hasBlockLeft = isConnected(blockLeft);
  const hasBlockRight = isConnected(blockRight);
  if (!hasBlockLeft && !hasBlockRight)
    return;
  system.runTimeout(() => {
    updateAdjacentBlock(blockLeft, dimension, cardinalDirection);
    updateAdjacentBlock(blockRight, dimension, cardinalDirection);
  }, 1);
}
export {
  breakSunshade,
  connectSunshade
};
