import { world } from 'mojang-minecraft';

world.events.dataDrivenEntityTriggerEvent.subscribe(data => {
  const { entity, id } = data;

  if (entity.id == "minecraft:player") {
    if (id == "morph:post_morph") { remove_mob_items(entity); };

    if (id == "morph:skeleton") { add_mob_item(entity, "bow"); };
    if (id == "morph:enderman") { add_mob_item(entity, "morph:teleport"); };
    if (id == "morph:zombified_piglin") { add_mob_item(entity, "golden_sword"); };
    if (id == "morph:wither_skeleton") { add_mob_item(entity, "stone_sword"); };
    if (id == "morph:snow_golem") { add_mob_item(entity, "morph:snow_golem_ball"); };
    if (id == "morph:sheared_snow_golem") { add_mob_item(entity, "morph:snow_golem_ball"); };
    if (id == "morph:blaze") { add_mob_item(entity, "morph:blaze_fireball"); };
    if (id == "morph:piglin") { add_mob_item(entity, "golden_sword"); };
    if (id == "morph:stray") { add_mob_item(entity, "bow"); };
    if (id == "morph:vindicator") { add_mob_item(entity, "iron_axe"); };
    if (id == "morph:piglin_brute") { add_mob_item(entity, "golden_axe"); };
    if (id == "morph:creamy_llama") { add_mob_item(entity, "morph:llama_spit"); };
    if (id == "morph:baby_creamy_llama") { add_mob_item(entity, "morph:llama_spit"); };
    if (id == "morph:white_llama") { add_mob_item(entity, "morph:llama_spit"); };
    if (id == "morph:baby_white_llama") { add_mob_item(entity, "morph:llama_spit"); };
    if (id == "morph:brown_llama") { add_mob_item(entity, "morph:llama_spit"); };
    if (id == "morph:baby_brown_llama") { add_mob_item(entity, "morph:llama_spit"); };
    if (id == "morph:gray_llama") { add_mob_item(entity, "morph:llama_spit"); };
    if (id == "morph:baby_gray_llama") { add_mob_item(entity, "morph:llama_spit"); };
    if (id == "morph:wither") { add_mob_item(entity, "morph:wither_skull"); add_mob_item(entity, "morph:wither_skull_dangerous"); };
    if (id == "morph:pillager") { add_mob_item(entity, "crossbow"); };
    if (id == "morph:vex") { add_mob_item(entity, "iron_sword"); };
    if (id == "morph:witch") { add_mob_item(entity, "potion", 21); add_mob_item(entity, "splash_potion", 23); };
    if (id == "morph:ghast") { add_mob_item(entity, "morph:ghast_fireball"); };
    
    if (id == "morph:skeleton_convert_to_wither") { remove_mob_items(entity); add_mob_item(entity, "stone_sword"); };
    if (id == "morph:pig_become_zombie" && !entity.getComponent('minecraft:is_baby')) { remove_mob_items(entity); add_mob_item(entity, "golden_sword"); };
    if (id == "morph:villager_become_witch") { add_mob_item(entity, "potion", 21); add_mob_item(entity, "splash_potion", 23); };
  };
});

function add_mob_item(entity, item_id, aux = 0) {
  let slot = getEmptySlot(entity);
  let replaceitem_slot = slot < 9 ? `slot.hotbar ${slot}` : `slot.inventory ${slot - 9}`;
  entity.runCommand(`replaceitem entity @s ${replaceitem_slot} ${item_id} 1 ${aux} {"item_lock":{"mode": "lock_in_inventory"},"keep_on_death":{}}`);

  let item = entity.getComponent("minecraft:inventory").container.getItem(slot);
  item.setLore(["Morph Addon"]);
  entity.getComponent("minecraft:inventory").container.setItem(slot, item);
};

function getEmptySlot(entity) {
  for (let slot = 0; slot < entity.getComponent("minecraft:inventory").container.size; slot++) {
    let item = entity.getComponent("minecraft:inventory").container.getItem(slot);
    if (item == null) { return slot; };
  };
};

function remove_mob_items(entity) {
  for (let slot = 0; slot < entity.getComponent("minecraft:inventory").container.size; slot++) {
    let item = entity.getComponent("minecraft:inventory").container.getItem(slot);
    try {
      if (item.getLore().includes("Morph Addon")) {
        let replaceitem_slot = slot < 9 ? `slot.hotbar ${slot}` : `slot.inventory ${slot - 9}`;
        entity.runCommand(`replaceitem entity @s ${replaceitem_slot} air`);
      };
    } catch {};
  };
};

world.events.beforeItemUseOn.subscribe(data => {
  const { blockLocation, item, source } = data;
  if (source.dimension.getBlock(blockLocation).id == "minecraft:frame" && item.getLore().includes("Morph Addon")) { data.cancel = true; };
});