import { world, system, GameMode } from "@minecraft/server";

const insmiblocks = [
  "minecraft:allium",
  "minecraft:acacia_sapling",
  "minecraft:azalea",
  "minecraft:beetroot",
  "minecraft:birch_sapling",
  "minecraft:blue_orchid",
  "minecraft:brain_coral",
  "minecraft:brain_coral_fan",
  "minecraft:brown_mushroom",
  "minecraft:bubble_coral",
  "minecraft:bubble_coral_fan",
  "minecraft:cave_vines_body_with_berries",
  "minecraft:cave_vines_head_with_berries",
  "minecraft:carrots",
  "minecraft:cherry_sapling",
  "minecraft:cornflower",
  "minecraft:crimson_fungus",
  "minecraft:crimson_roots",
  "minecraft:darkoak_sapling",
  "minecraft:dead_brain_coral",
  "minecraft:dead_brain_coral_fan",
  "minecraft:dead_bubble_coral",
  "minecraft:dead_bubble_coral_fan",
  "minecraft:dead_fire_coral",
  "minecraft:dead_fire_coral_fan",
  "minecraft:dead_horn_coral",
  "minecraft:dead_horn_coral_fan",
  "minecraft:dead_tube_coral",
  "minecraft:dead_tube_coral_fan",
  "minecraft:deadbush",
  "minecraft:decorated_pot",
  "minecraft:end_rod",
  "minecraft:fern",
  "minecraft:fire_coral",
  "minecraft:fire_coral_fan",
  "minecraft:flower_pot",
  "minecraft:flowering_azalea",
  "minecraft:frog_spawn",
  "minecraft:hanging_roots",
  "minecraft:honey_block",
  "minecraft:horn_coral",
  "minecraft:horn_coral_fan",
  "minecraft:jungle_sapling",
  "minecraft:large_fern",
  "minecraft:lilac",
  "minecraft:lily_of_the_valley",
  "minecraft:melon_stem",
  "minecraft:nether_wart",
  "minecraft:oak_sapling",
  "minecraft:orange_tulip",
  "minecraft:oxeye_daisy",
  "minecraft:peony",
  "minecraft:pink_petals",
  "minecraft:pink_tulip",
  "minecraft:pitcher_plant",
  "minecraft:poppy",
  "minecraft:potatoes",
  "minecraft:powered_comparator",
  "minecraft:powered_repeater",
  "minecraft:reeds",
  "minecraft:red_mushroom",
  "minecraft:red_tulip",
  "minecraft:redstone_torch",
  "minecraft:redstone_wire",
  "minecraft:rose_bush",
  "minecraft:scaffolding",
  "minecraft:seagrass",
  "minecraft:short_grass",
  "minecraft:slime",
  "minecraft:small_dripleaf",
  "minecraft:soul_torch",
  "minecraft:spruce_sapling",
  "minecraft:spore_blossom",
  "minecraft:sunflower",
  "minecraft:sweet_berry_bush",
  "minecraft:tall_grass",
  "minecraft:tnt",
  "minecraft:torch",
  "minecraft:torchflower",
  "minecraft:trip_wire",
  "minecraft:tripwire_hook",
  "minecraft:tube_coral",
  "minecraft:tube_coral_fan",
  "minecraft:twisting_vines",
  "minecraft:unpowered_comparator",
  "minecraft:unpowered_repeater",
  "minecraft:warped_fungus",
  "minecraft:warped_roots",
  "minecraft:waterlily",
  "minecraft:weeping_vines",
  "minecraft:wheat",
  "minecraft:white_tulip",
  "minecraft:wither_rose",
  "minecraft:yellow_flower",
];

function damageitem(unbreakingLevel) {
  return 1 / (1 + (unbreakingLevel ?? 0)) >= Math.random();
}

function instantmine(blockIdentifier) {
  return insmiblocks.includes(blockIdentifier);
}

function redura(entity, itemStack, damageAmount) {
  const durability = itemStack.getComponent("minecraft:durability");
  const inventory = entity.getComponent("minecraft:inventory").container;

  if (durability.damage + damageAmount >= durability.maxDurability) {
    inventory.setItem(entity.selectedSlotIndex, undefined);
    entity.playSound("random.break");
  } else {
    durability.damage += damageAmount;
    inventory.setItem(entity.selectedSlotIndex, itemStack);
  }
}

  system.beforeEvents.startup.subscribe((initEvent) => {
  // DURABILIDAD PERSONALIZADA
  initEvent.itemComponentRegistry.registerCustomComponent("dungeon:durability", {
    onMineBlock: (e) => {
      if (e.source.matches({ gameMode: GameMode.Creative })) return;

      const itemEnchantable = e.itemStack.getComponent("enchantable");
      const unbreaking = itemEnchantable?.getEnchantment("unbreaking");
      const unbreakingLevel = unbreaking?.level || 0;
      const silkTouch = itemEnchantable?.getEnchantment("silk_touch");

      if (silkTouch || (!unbreaking && !instantmine(e.minedBlockPermutation.type.id))) {
        redura(e.source, e.itemStack, 1);
      } else if (unbreaking && damageitem(unbreakingLevel)) {
        redura(e.source, e.itemStack, 1);
      }
    },
  });

  initEvent.itemComponentRegistry.registerCustomComponent("dungeon:durability_use", {
    onUse: (e) => {
      if (e.source.matches({ gameMode: GameMode.Creative })) return;
      redura(e.source, e.itemStack, 5);
    },
  });

  initEvent.itemComponentRegistry.registerCustomComponent("dungeon:durability_use2", {
    onUse: (e) => {
      if (e.source.matches({ gameMode: GameMode.Creative })) return;
      redura(e.source, e.itemStack, 100);
    },
  });
})
