import {
    system,
    Player,
    Entity,
    EntityComponentTypes,
    EquipmentSlot,
    EntityDamageCause,
    ItemComponentTypes,
    EnchantmentTypes
  } from "@minecraft/server";
  import {
    Vector
  } from "../vector";
  import {
    playSound
  } from "../util";
  class BulletDamage {
    constructor(_0x286fc4, _0x26b821, _0xfffdd8) {
        this.gun = _0x286fc4;
        this.caliber = _0x26b821;
        this.damage = _0xfffdd8;
    }
  }
  class ArmorProtection {
    constructor(_0x279a8b, _0x144bbc) {
        this.type = _0x279a8b;
        this.value = _0x144bbc;
    }
  }
  const DAMAGE = {
    bullet_1: new BulletDamage("AK-12", "bullet_545x39", 1),
    bullet_2: new BulletDamage("AK-47", "bullet_762x39", 1),
    bullet_3: new BulletDamage("AWP", "bullet_762x51", 14),
    bullet_4: new BulletDamage(".50 Desert Eagle", "bullet_50ae", 2),
    bullet_5: new BulletDamage("FN FAL", "bullet_762x51", 1),
    bullet_6: new BulletDamage("Glock", "9x19pr", 1),
    bullet_7: new BulletDamage("M249", "bullet_556x45", 2),
    bullet_8: new BulletDamage("M4A1", "bullet_556x45", 1),
    bullet_9: new BulletDamage("Mossberg 500", "bullet_12gauge", 1),
    bullet_10: new BulletDamage("HK MP5A5", "9x19pr", 1)
  };
  const ARMOR_PROTECTION = {
    "minecraft:golden_helmet": new ArmorProtection("helmet", 0.08),
    "minecraft:iron_helmet": new ArmorProtection("helmet", 0.12),
    "minecraft:diamond_helmet": new ArmorProtection("helmet", 0.24),
    "minecraft:netherite_helmet": new ArmorProtection("helmet", 0.3),
    "gabrielaplok:helmet_6b47": new ArmorProtection("helmet", 0.24),
    "gabrielaplok:vest_6b3": new ArmorProtection("chest", 0.48),
    "minecraft:golden_chestplate": new ArmorProtection("chest", 0.12),
    "minecraft:iron_chestplate": new ArmorProtection("chest", 0.24),
    "minecraft:diamond_chestplate": new ArmorProtection("chest", 0.48),
    "minecraft:netherite_chestplate": new ArmorProtection("chest", 0.64),
    "gabrielaplok:juggernaut_helmet": new ArmorProtection("helmet", 0.24),
    "gabrielaplok:juggernaut_chestplate": new ArmorProtection("chest", 0.86)
  };
  const ENTITIES_SUPPORT_HEADSHOT = ["minecraft:blaze", "minecraft:bogged", "minecraft:breeze", "minecraft:drowned", "minecraft:evocation_illager", "minecraft:husk", "minecraft:npc", "minecraft:piglin", "minecraft:piglin_brute", "minecraft:pillager", "minecraft:player", "minecraft:skeleton", "minecraft:stray", "minecraft:villager", "minecraft:villager_v2", "minecraft:vindicator", "minecraft:wandering_trader", "minecraft:wither", "minecraft:wither_skeleton", "minecraft:zombie", "minecraft:iron_golem", "minecraft:zombie_pigman", "minecraft:zombie_villager", "minecraft:zombie_villager_v2", "converse:bot"];
  const getArmor = _0x6e6d56 => {
    const _0x5dcae6 = _0x6e6d56?.getComponent(EntityComponentTypes.Equippable);
    const _0x31768d = [];
    const _0xfeb882 = (_0x5dcae6?.getEquipment(EquipmentSlot.Mainhand)?.typeId === "minecraft:shield" || _0x5dcae6?.getEquipment(EquipmentSlot.Offhand)?.typeId === "minecraft:shield") && _0x6e6d56.isSneaking;
    if (_0x5dcae6?.getEquipment(EquipmentSlot.Chest)?.typeId) {
        _0x31768d.push(_0x5dcae6?.getEquipment(EquipmentSlot.Chest));
    }
    if (_0x5dcae6?.getEquipment(EquipmentSlot.Legs)?.typeId) {
        _0x31768d.push(_0x5dcae6?.getEquipment(EquipmentSlot.Legs));
    }
    if (_0x5dcae6?.getEquipment(EquipmentSlot.Feet)?.typeId) {
        _0x31768d.push(_0x5dcae6?.getEquipment(EquipmentSlot.Feet));
    }
    return {
        equipments: _0x31768d,
        zeroAttack: _0xfeb882
    };
  };
  const calcDamage = (_0x55f1f9, _0x1b9559, _0xafc8b6, _0x303d33, _0x2afa25) => {
    const _0x437b74 = getArmor(_0xafc8b6);
    const _0x275133 = [];
    let _0x3232a5 = 1;
    for (const _0xff385 of _0x437b74.equipments) {
        const _0x425300 = ARMOR_PROTECTION[_0xff385?.typeId];
        _0x275133.push(_0xff385?.typeId);
        if (_0x425300) {
            _0x3232a5 -= _0x425300.value;
        }
    }
    if (_0x275133.includes("gabrielaplok:juggernaut_chestplate")) {
        _0x55f1f9.dimension.spawnParticle("gabrielaplok:bullet_spark", _0x55f1f9.location);
        _0x55f1f9.dimension.spawnParticle("gabrielaplok:bullet_spark", _0x55f1f9.location);
        _0x55f1f9.dimension.spawnParticle("gabrielaplok:bullet_impact", _0x55f1f9.location);
        playSound(_0x55f1f9.dimension, "bullet.impact.metal", _0x55f1f9.location, 20, {
            volume: 0.25
        });
    }
    if (_0x1b9559.isSneaking) {
        _0x303d33 += 0.5;
    }
    let _0x104531 = _0x303d33 * _0x3232a5;
    let _0x2a087f = 1.5;
    let _0x384785 = new Vector(_0x55f1f9.location.x, _0x55f1f9.location.y, _0x55f1f9.location.z);
    let _0x256646 = new Vector(_0xafc8b6.location.x, _0xafc8b6.location.y, _0xafc8b6.location.z);
    let _0x1c18c1 = _0x256646.add(new Vector(0, _0x2a087f, 0));
    let _0x3eaff5 = _0x384785.subtract(_0x256646);
    let _0x208d91 = false;
    if (ENTITIES_SUPPORT_HEADSHOT.includes(_0xafc8b6.typeId) && _0x3eaff5.y > _0x2a087f) {
        const _0x3d1e05 = _0xafc8b6.getComponent(EntityComponentTypes.Equippable);
            _0x208d91 = true;
            playSound(_0xafc8b6.dimension, "bullet.impact.flesh_head", _0x1c18c1, 20);
            playSound(_0xafc8b6.dimension, "effect.player.splat", _0x1c18c1, 20);
            _0x104531 *= 2.5;
    } else {
        playSound(_0xafc8b6.dimension, "bullet.impact.flesh", _0xafc8b6.location, 20, {
            volume: 0.25
        });
    }
    if (_0x104531 < 1) {
        _0x104531 = 1;
    }
    return {
        damage: _0x437b74.zeroAttack ? 0 : _0x104531,
        headshot: _0x208d91
    };
  };
  export const bulletEntityImpact = async _0x73a840 => {
    system.run(() => {
        const _0x3da6c4 = _0x73a840.source;
        const _0x57625c = _0x73a840.projectile;
        if (!_0x57625c || !_0x57625c.isValid()) {
            return;
        }
        if (_0x57625c.typeId != "gabrielaplok2:bullet") {
            return;
        }
        const _0x3ab718 = _0x73a840.getEntityHit();
        if (!_0x3ab718) {
            return;
        }
        const _0x366dbc = _0x3ab718.entity;
        if (!_0x366dbc || !_0x366dbc.isValid() || !_0x366dbc.typeId) {
            return;
        }
        const _0x467ed5 = _0x57625c.getComponent(EntityComponentTypes.Variant)?.value;
        if (!_0x467ed5) {
            return;
        }
        const _0x12035f = DAMAGE["bullet_" + _0x467ed5];
        if (!_0x12035f) {
            return;
        }
        let _0x4d0f23 = _0x366dbc;
        let _0x286a7c = calcDamage(_0x57625c, _0x3da6c4, _0x4d0f23, _0x12035f.damage, _0x73a840.location);
        if (_0x4d0f23.applyDamage(_0x286a7c.damage, {
                cause: _0x3da6c4 instanceof Player ? EntityDamageCause.override : EntityDamageCause.entityAttack,
                damagingEntity: _0x3da6c4,
                damagingProjectile: _0x57625c
            })) {
            const _0x490923 = _0x4d0f23.getComponent(EntityComponentTypes.Health)?.currentValue;
            _0x4d0f23.runCommand((_0x286a7c.headshot ? "particle player:headshot" : "particle player:shot"));
            if (_0x490923 <= 0 && _0x4d0f23 instanceof Player && _0x3da6c4 instanceof Player) {
                _0x3da6c4.runCommand("tellraw @a {\"rawtext\": [{\"translate\": \"" + (_0x286a7c.headshot ? "death.attack.bullet.head" : "death.attack.bullet") + "\", \"with\": {\"rawtext\": [{\"text\": \"" + _0x3da6c4.nameTag + "\"}, {\"text\": \"" + _0x4d0f23.nameTag + "\"}, {\"text\": \"" + _0x12035f.gun + "\"}]}}]}");
                _0x3da6c4.runCommand((_0x286a7c.headshot ? "playsound gun.headshot @s" : "playsound gun.gun @s"));
            }
            else{
                if (_0x490923 <= 0 && _0x4d0f23.typeId == "converse:bot" && _0x4d0f23 instanceof Entity && _0x3da6c4 instanceof Entity) {
                    _0x3da6c4.runCommand((_0x286a7c.headshot ? "playsound gun.headshot @s" : "playsound gun.gun @s"));
                    _0x3da6c4.runCommand("scoreboard players add @s trainingkills 1");
                    _0x3da6c4.runCommand("scoreboard players add @s "+(_0x286a7c.headshot ? "trainingheadshot" : "trainingkill")+" 1");
                    _0x3da6c4.runCommand("tellraw @a[tag=training] {\"rawtext\": [{\"selector\":\"@s\"},{\"text\": \""+(_0x286a7c.headshot ? " " : " ")+"\"}]}");
                }
            }
        }
        if (_0x3da6c4 && _0x3da6c4 instanceof Player) {
            _0x3da6c4.triggerEvent(_0x286a7c.headshot ? "gabrielaplok:add_hitmark_headshot" : "gabrielaplok:add_hitmark");
        }
    });
  };