import { world, system, ItemStack, Dimension } from "@minecraft/server";
function Element(projetil) {
  const family = projetil.getComponent("type_family").getTypeFamilies()
  const noFamilies = ["absorb", "anular"]
  const element = family.filter(f => !noFamilies.includes(f))
  return element[0]
}
  
  //Dweller licht
  system.beforeEvents.startup.subscribe((event) => {
    event.itemComponentRegistry.registerCustomComponent('yokai:dweller_licht', {
      onUse: use => {
        if (use.source.hasTag("no_skill")) return
          const projetil = use.source.dimension.getEntities({
            location: use.source.location,
            maxDistance: 15,
            families: ["absorb"],
          })[0]
          if (projetil) {
            const mana = use.source.getDynamicProperty("mana")
            if (mana >= 4) {
              use.source.setDynamicProperty("mana", mana - 4)
              const element = Element(projetil)
              const inventory = use.source.getComponent("inventory").container
              const sword = new ItemStack(`yokai:dweller_licht_${element}`, 1)
              sword.keepOnDeath = true
              sword.lockMode = "slot"
              inventory.setItem(7, sword)
              projetil.triggerEvent("kill")
              use.source.playSound("dweller_charge")
            }
          } else {
            const mana = use.source.getDynamicProperty("mana")
            if (mana >= 5) {
              use.source.setDynamicProperty("mana", mana - 5)
              const inventory = use.source.getComponent("inventory").container
              const sword = new ItemStack(`yokai:dweller_licht_light`, 1)
              sword.keepOnDeath = true
              sword.lockMode = "slot"
              inventory.setItem(7, sword)
              use.source.playSound("dweller_charge")
            }
          }
      }
    })
  });

    system.beforeEvents.startup.subscribe((event) => {
    event.itemComponentRegistry.registerCustomComponent('yokai:dweller_licht_mana', {
      onUse: use => {
        if (use.source.hasTag("no_skill")) return
        const player = use.source
        const inventory = player.getComponent("inventory").container
        const sword = new ItemStack(`yokai:dweller_licht`, 1)
        sword.keepOnDeath = true
        sword.lockMode = "slot"
        const item = use.itemStack.typeId.split("_")[2]
        function usar() {
          use.itemStack.getComponent("cooldown").startCooldown(player)
          player.triggerEvent("dweller_slash")
          player.playSound("shoot_dweller")
          player.triggerEvent(`dweller_${item}`)
          system.runTimeout(() => {
            player.triggerEvent("atk_none")
          }, 10)
        }
        usar()
        system.runTimeout(() => {
          usar()
        }, 20)
        system.runTimeout(() => {
          usar()
        }, 40)
        system.runTimeout(() => {
          inventory.setItem(7, sword)
        }, 50)
      }
    })
  });

  //Destroyer Licht 
  system.beforeEvents.startup.subscribe((event) => {
    event.itemComponentRegistry.registerCustomComponent('yokai:destroyer_licht', {
      onUse: use => {
        if (use.source.hasTag("no_skill")) return
          const mana = use.source.getDynamicProperty("mana")
          if (mana >= 9) {
            use.source.triggerEvent("destroyer_zone_licht")
            use.source.playSound("destroyer_zone")
            use.source.setDynamicProperty("mana", mana - 9)
            const mobs = use.source.dimension.getEntities({
              location: use.source.location,
              maxDistance: 15
            })
            for (const mob of mobs) {
              if (mob.id != use.source.id) {
                try {
                   mob.applyDamage(10, {
                     cause: "entityAttack",
                     damagingEntity: use.source,
                   })
                    const dx = mob.location.x - use.source.location.x;
                    const dz = mob.location.z - use.source.location.z;
                    const distance = Math.sqrt(dx * dx + dz * dz);
                    const knockbackX = distance === 0 ? 2.5 : dx / distance;
                    const knockbackZ = distance === 0 ? 2.5 : dz / distance;
                    mob.applyKnockback({ x: knockbackX * 6.5, z: knockbackZ * 6.5 }, 0.5)
                } catch {}
              }
            }
            system.runTimeout(() => {
              use.source.triggerEvent("atk_none")
            }, 10)
          }
      }
    })
  });
  
  