// data/system_template_esbuild/subscripts/guide_book.ts
import {
  world,
  system,
  ItemStack,
  EntityComponentTypes,
  EquipmentSlot
} from "@minecraft/server";
var PROJECT_NAME = "cbcl_rm";
var LORE = "\xA7r\xA76Revive Add-On";
var VERSION_1_0 = `${PROJECT_NAME}_VERSION_1_0`;
world.afterEvents.itemStartUse.subscribe((event) => {
  let player = event.source;
  let itemStack = event.itemStack;
  if (!itemStack || itemStack.typeId !== `${PROJECT_NAME}:guide_book`)
    return;
  player.playSound("item.book.page_turn");
});
world.afterEvents.playerJoin.subscribe((event) => {
  let player = world.getEntity(event.playerId);
  if (!player)
    return;
  checkIfLoaded(player);
});
function checkIfLoaded(player) {
  let location = player.location;
  try {
    let blockToCheck = player.dimension?.getBlock(location);
    if (!blockToCheck) {
      system.runTimeout(() => {
        if (!player.isValid)
          return;
        checkIfLoaded(player);
      }, 10);
      return;
    } else {
      addonLoaded(player, location);
    }
  } catch (error) {
    system.runTimeout(() => {
      if (!player.isValid)
        return;
      checkIfLoaded(player);
    }, 10);
  }
}
function addonLoaded(player, originalLocation) {
  if (Math.abs(originalLocation.x - player.location.x) > 0.5 || Math.abs(originalLocation.z - player.location.z) > 0.5) {
    system.runTimeout(() => {
      if (!player.isValid)
        return;
      if (!player.hasTag(VERSION_1_0)) {
        player.playSound("note.bell");
        //player.sendMessage({ translate: `${PROJECT_NAME}.addon_applied` });
        player.addTag(VERSION_1_0);
        let guidebook = new ItemStack(`${PROJECT_NAME}:guide_book`, 1);
       // guidebook.setLore([`${LORE}`]);
        player.dimension.spawnItem(guidebook, player.location);
      }
    }, 5);
  } else {
    system.runTimeout(() => {
      if (!player.isValid)
        return;
      addonLoaded(player, originalLocation);
    }, 5);
    return;
  }
}
function getItemHeld(player) {
  const playerEquippableComp = player.getComponent(EntityComponentTypes.Equippable);
  if (!playerEquippableComp)
    return;
  let heldItem = playerEquippableComp.getEquipment(EquipmentSlot.Mainhand);
  return heldItem;
}
function displayHint(player) {
  if (!player)
    return;
  const holdingItem = getItemHeld(player);
  if (holdingItem == void 0)
    return;
  if (holdingItem.typeId.includes(`${PROJECT_NAME}:guide_book`)) {
    player.runCommand(`/titleraw @s actionbar {"rawtext":[{"translate":"${PROJECT_NAME}.guide_book.actionbar_hint"}]}`);
  }
}
function addLore(player) {
  let itemStack = getItemHeld(player);
  if (itemStack === void 0)
    return;
  if (itemStack?.typeId.includes(`${PROJECT_NAME}:guide_book`) && itemStack.getLore().length === 0) {
    itemStack.setLore([`${LORE}`]);
    const playerEquippableComp = player.getComponent(EntityComponentTypes.Equippable);
    playerEquippableComp.setEquipment(EquipmentSlot.Mainhand, itemStack);
  }
}
system.runInterval(() => {
  world.getAllPlayers().forEach((player) => {
    displayHint(player);
    //addLore(player);
  });
}, 20);
