import {
    Entity,
    EntityDamageCause,
    EquipmentSlot,
    ItemComponentTypes,
    system,
    world,
} from "@minecraft/server";
export default class Hedgehog {
    static ENTITY_OR_ITEM_ID = "sf_nba:hedgehog";
    static usedHedgehogTick = {};
    static blockInteractions = new Set();
    constructor() {
        this.initEvents();
    }
    initEvents() {
        world.beforeEvents.itemUseOn.subscribe(this.handleUseOnBlock.bind(this));
        world.afterEvents.itemReleaseUse.subscribe(this.handleRelease.bind(this));
        world.afterEvents.projectileHitEntity.subscribe(this.handleEntityHit.bind(this));
        system.afterEvents.scriptEventReceive.subscribe(this.handleHit.bind(this));
    }
    static extractHedgehogProperties(itemId) {
        const variant = itemId.replace(`${Hedgehog.ENTITY_OR_ITEM_ID}_`, "")[0];
        const isBaby = itemId.indexOf("baby") !== -1;
        return { variant, isBaby };
    }
    static applyHedgehogProperties(hedgehog, variant, isBaby, owner = null) {
        hedgehog.triggerEvent("sf_nba:spawn_variant_" + variant);
        if (isBaby) {
            hedgehog.triggerEvent("sf_nba:spawn_baby");
        }
        if (owner) {
            const tameable = hedgehog.getComponent("minecraft:tameable");
            if (tameable) {
                tameable.tame(owner);
            }
        }
    }
    handleUseOnBlock(eventData) {
        const stack = eventData.itemStack;
        if (!stack) return;
        const itemId = stack.typeId;
        if (itemId === "sf_nba:hedgehog_spawn_egg") return;
        if (!itemId.startsWith(Hedgehog.ENTITY_OR_ITEM_ID)) return;
        const player = eventData.source;
        const blockLocation = eventData.block.location;
        const blockFace = eventData.blockFace;
        Hedgehog.blockInteractions.add(player.id);
        eventData.cancel = true;
        const spawnLocation = this.calculateSpawnLocation(blockLocation, blockFace);
        const { variant, isBaby } = Hedgehog.extractHedgehogProperties(itemId);
        system.run(() => {
            const entity = player.dimension.spawnEntity(Hedgehog.ENTITY_OR_ITEM_ID, spawnLocation);
            const playerDirection = player.getViewDirection();
            entity.setRotation({x: -playerDirection.x, y: -playerDirection.y});
            const equipment = player.getComponent("minecraft:equippable");
            if (equipment) {
                if (stack.amount > 1) {
                    const newStack = stack.clone();
                    newStack.amount = stack.amount - 1;
                    equipment.setEquipment(EquipmentSlot.Mainhand, newStack);
                } else {
                    equipment.setEquipment(EquipmentSlot.Mainhand, undefined);
                }
            }
            Hedgehog.applyHedgehogProperties(entity, variant, isBaby, player);
        });
    }
    calculateSpawnLocation(blockLocation, blockFace) {
        const spawnLocation = { 
            x: blockLocation.x + 0.5, 
            y: blockLocation.y + 0.5, 
            z: blockLocation.z + 0.5 
        };
        switch (blockFace) {
            case 'Up':
                spawnLocation.y += 0.5;
                break;
            case 'Down':
                spawnLocation.y -= 0.5;
                break;
            case 'North':
                spawnLocation.z -= 0.5;
                break;
            case 'South':
                spawnLocation.z += 0.5;
                break;
            case 'West':
                spawnLocation.x -= 0.5;
                break;
            case 'East':
                spawnLocation.x += 0.5;
                break;
        }
        return spawnLocation;
    }
    handleRelease(eventData) {
        const stack = eventData.itemStack;
        if (!stack) return;
        const player = eventData.source;
        if (!player) return;
        if (!stack.type || !stack.type.id) return;
        const itemId = stack.type.id;
        if (!itemId.startsWith(Hedgehog.ENTITY_OR_ITEM_ID)) return;
        if (Hedgehog.usedHedgehogTick[player.id] === system.currentTick) return;
        if (Hedgehog.blockInteractions.has(player.id)) {
            Hedgehog.blockInteractions.delete(player.id);
            return;
        }
        const strength = 2;
        try {
            const cooldownComponent = stack.getComponent(ItemComponentTypes.Cooldown);
            if (cooldownComponent && cooldownComponent.getCooldownTicksRemaining(player) > 0) return;
            Hedgehog.usedHedgehogTick[player.id] = system.currentTick;
            const { variant, isBaby } = Hedgehog.extractHedgehogProperties(itemId);
            const hedgehog = Hedgehog.spawnAndThrow(player, strength, variant, isBaby);
            const equipment = player.getComponent("minecraft:equippable");
            if (!equipment) return;
            if (cooldownComponent) {
                cooldownComponent.startCooldown(player);
            }
            if (stack.amount > 1) {
                const newStack = stack.clone();
                newStack.amount = stack.amount - 1;
                equipment.setEquipment(EquipmentSlot.Mainhand, newStack);
            } else {
                equipment.setEquipment(EquipmentSlot.Mainhand, undefined);
            }
        } catch (error) {
            console.warn("Error in handleRelease:", error);
        }
    }
    handleHit(eventData) {
        const id = eventData.id;
        const entity = eventData.sourceEntity;
        if (!entity) return;
        const identifier = entity.typeId;
        if (identifier !== Hedgehog.ENTITY_OR_ITEM_ID || id !== "sf_nba:hedgehog_hit") return;
        system.runTimeout(() => {
            entity.triggerEvent("sf_nba:stop_rolling");
            entity.setProperty("sf_nba:hit", false);
        }, 2.2);
    }
    handleEntityHit(eventData) {
        const player = eventData.source;
        const entity = eventData.projectile;
        const hitEntityResult = eventData.getEntityHit();
        if (!hitEntityResult) return;
        const target = hitEntityResult.entity;
        if (!entity || !target) return;
        const identifier = entity.typeId;
        if (identifier !== Hedgehog.ENTITY_OR_ITEM_ID) return;
        target.applyDamage(2, {cause: EntityDamageCause.entityAttack, damagingEntity: player});
    }
    static spawnAndThrow(player, strength = 1, variant, isBaby) {
        const headPos = player.getHeadLocation();
        const rot = player.getViewDirection();
        const hedgehog = player.dimension.spawnEntity(Hedgehog.ENTITY_OR_ITEM_ID, {
            x: headPos.x - rot.z * 0.3 + rot.x * 0.2,
            y: headPos.y - 0.2,  
            z: headPos.z + rot.x * 0.3 + rot.z * 0.2,
        });
        hedgehog.clearVelocity();
        hedgehog.applyImpulse({
            x: rot.x  * strength + player.getVelocity().x,
            y: rot.y ,
            z: rot.z * strength + player.getVelocity().z,
        });
        Hedgehog.applyHedgehogProperties(hedgehog, variant, isBaby, player);
        hedgehog.triggerEvent("sf_nba:projectile");
        hedgehog.setProperty("sf_nba:rolling", true);
        hedgehog.setDynamicProperty("sf_nba:last_thrown", Date.now());
        player.dimension.playSound("random.bow", player.location);
        return hedgehog;
    }
}
new Hedgehog();