import { electricGun, tranquilizingSniper } from "./constants";
import { getOffsetRelativeToEntityRotation, isDinosaur, damageItem } from "./utilities";
import { NAMESPACE } from "./anvilConstants";
function tranquilizingSniperHit(entity) {
    if (isDinosaur(entity) && entity.getProperty(`${NAMESPACE}:can_be_tranquilized`) === true) {
        entity.triggerEvent("is_tranquilized");
    }
    else {
        entity.addEffect("poison", 10 * 20, { amplifier: 1 });
        entity.addEffect("nausea", 10 * 20, { amplifier: 1 });
    }
}
function tranquilizingSniperShoot(player) {
    const projectile = player.dimension.spawnEntity(`${NAMESPACE}:dart_projectile`, getOffsetRelativeToEntityRotation(player, {
        x: 0,
        y: 1.4,
        z: 1,
    }));
    const view = player.getViewDirection();
    view.x *= 5;
    view.y *= 5;
    view.z *= 5;
    projectile.applyImpulse(view);
}
export function tranquilizingSniperShootEvent(eventData) {
    const player = eventData.source;
    const itemStack = eventData.itemStack;
    if (itemStack && itemStack.typeId === tranquilizingSniper) {
        const inventory = player.getComponent('inventory');
        if (!inventory || !inventory.container) return;
        const { container } = inventory;
        for (let i = 0; i <= inventory.inventorySize - 1; i++) {
            const itemSearch = container.getItem(i);
            if (itemSearch !== undefined && itemSearch.typeId === `${NAMESPACE}:dart_projectile`) {
                tranquilizingSniperShoot(player);
                damageItem(player);
                player.runCommand(`clear @s[m=!c] ${NAMESPACE}:dart_projectile 0 1`);
                return;
            }
        }
        player.runCommand(`titleraw @s actionbar ${JSON.stringify({ rawtext: [{ translate: `${NAMESPACE}.dart_required` }] })}`);
        return;
    }
}
export function tranquilizingSniperHitEvent(eventData) {
    const entity = eventData.getEntityHit().entity;
    const projectile = eventData.projectile;
    if (entity && projectile.typeId === `${NAMESPACE}:dart_projectile`) {
        tranquilizingSniperHit(entity);
    }
}
function electricGunShoot(player) {
    const projectile = player.dimension.spawnEntity(`${NAMESPACE}:electric_projectile`, getOffsetRelativeToEntityRotation(player, {
        x: 0,
        y: 1.4,
        z: 1,
    }));
    const view = player.getViewDirection();
    view.x *= 5;
    view.y *= 5;
    view.z *= 5;
    projectile.applyImpulse(view);
}
export function electricGunShootEvent(eventData) {
    const player = eventData.source;
    const itemStack = eventData.itemStack;
    if (itemStack && itemStack.typeId === electricGun) {
        electricGunShoot(player);
        damageItem(player);
    }
}