import { world, system, ItemStack, Direction, EquipmentSlot, EntityComponentTypes, } from "@minecraft/server";
import { MinecraftEffectTypes, callingHornsDatabaseId, spawnSaddleEvents, loreMap, allMountIDs } from "../DATA/Config";
import { JsonDatabase } from "../U/database";
const callingHornsDatabase = new JsonDatabase(world, callingHornsDatabaseId);
export class SaddleEventMapping {
    static getEvent(saddleProperty, numKey) {
        if (!saddleProperty) {
            return "kicu_mmad:spawn_with_none_saddle";
        }
        return this.events[numKey] || "Unknown Event";
    }
}
SaddleEventMapping.events = spawnSaddleEvents;
export function callingHornStore(player, log) {
    const hit = player.getEntitiesFromViewDirection({ maxDistance: 20 });
    if (hit.length > 0) {
        const entity = getHit(hit);
        if (entity !== undefined) {
            const entityRiding = player.getComponent(EntityComponentTypes.Riding);
            if (entityRiding?.entityRidingOn === undefined) {
                system.run(() => {
                    //yes this is intentionally duplicated. Due to a Minecraft issue where entity health cant be updated due to damage multiplyer changes.
                    entity.addEffect(MinecraftEffectTypes.Resistance, 150, { amplifier: 255, showParticles: false });
                    player.playSound("sound.kicu_mmad_calling_horn_dismiss", {
                        pitch: 1.0,
                        volume: 1.0,
                    });
                    const savedMountId = entity.typeId;
                    const theKey = `kicu_mmad:${player.id}:${entity.id}`;
                    const propertyMapping = createPropertyMapping(entity);
                    const equippable = player.getComponent("minecraft:equippable");
                    const newItem = new ItemStack("kicu_mmad:calling_horn_occupied", 1);
                    const customName = propertyMapping["nameTag"];
                    if (customName) {
                        newItem.setLore([customName, `§5§k${player.id}§r`, `§5§k${entity.id}§r`]);
                    }
                    else {
                        newItem.setLore([loreMap[savedMountId], `§5§k${player.id}§r`, `§5§k${entity.id}§r`]);
                    }
                    //yes this is intentionally duplicated. Due to a Minecraft issue where entity health cant be updated due to damage multiplyer changes we made.
                    entity.setProperty("kicu_mmad:invulnerable", true);
                    system.runTimeout(() => {
                        entity.setProperty("kicu_mmad:invulnerable", true);
                    }, 20);
                    callingHornsDatabase.set(theKey, propertyMapping);
                    equippable.setEquipment(EquipmentSlot.Mainhand, newItem);
                    entity.triggerEvent("kicu_mmad:stop_riding");
                    entity.triggerEvent("kicu_mmad:set_capture_true");
                    //yes this is intentionally duplicated. Due to a Minecraft issue where entity health cant be updated due to damage multiplyer changes we made.
                    entity.setProperty("kicu_mmad:invulnerable", true);
                    system.runTimeout(() => {
                        entity.setProperty("kicu_mmad:invulnerable", true);
                    }, 20);
                    entity.addEffect(MinecraftEffectTypes.Resistance, 150, { amplifier: 255, showParticles: false });
                });
            }
        }
    }
}
export function callingHornRelease(player, itemStack, log) {
    try {
        const entityRiding = player.getComponent(EntityComponentTypes.Riding);
        if (entityRiding?.entityRidingOn === undefined) {
            const thisBlock = player.getBlockFromViewDirection({ includeLiquidBlocks: false });
            if (thisBlock && thisBlock.face === Direction.Up) {
                system.run(() => {
                    const mainhand = player.getComponent("minecraft:equippable");
                    const itemLore = itemStack.getLore();
                    if (mainhand && itemLore.length > 2) {
                        player.playSound("sound.kicu_mmad_calling_horn_summon", {
                            pitch: 1.0,
                            volume: 1.0,
                        });
                        const [savedMountName, savedPlayerId, savedMountId] = itemLore;
                        const theKey = `kicu_mmad:${cleanString(savedPlayerId)}:${cleanString(savedMountId)}`;
                        const propertyMapping = callingHornsDatabase.get(theKey);
                        const mountSpawnId = propertyMapping["identifier"];
                        const mountSaddle = propertyMapping["saddle"];
                        const mountSaddleType = propertyMapping["saddle_type"];
                        const newMount = player.dimension.spawnEntity(mountSpawnId, {
                            x: thisBlock.block.location.x,
                            y: thisBlock.block.location.y + 1,
                            z: thisBlock.block.location.z,
                        });
                        newMount.setProperty("kicu_mmad:invulnerable", true);
                        newMount.addEffect(MinecraftEffectTypes.Resistance, 150, { amplifier: 255, showParticles: false });
                        applyPropertyMapping(newMount, propertyMapping);
                        newMount.triggerEvent("kicu_mmad:set_capture_false");
                        newMount.addEffect(MinecraftEffectTypes.Invisibility, 4, { amplifier: 1, showParticles: false });
                        newMount.addEffect(MinecraftEffectTypes.Slowness, 100, { amplifier: 255, showParticles: false });
                        mainhand.setEquipment(EquipmentSlot.Mainhand, new ItemStack("kicu_mmad:calling_horn", 1));
                        newMount.triggerEvent(SaddleEventMapping.getEvent(mountSaddle, mountSaddleType));
                        callingHornsDatabase.delete(theKey);
                        newMount.triggerEvent(SaddleEventMapping.getEvent(mountSaddle, mountSaddleType));
                        callingHornsDatabase.delete(theKey);
                        //yes this is intentionally duplicated. Due to a Minecraft issue where entity health cant be updated due to damage multiplyer changes.
                        system.runTimeout(() => {
                            newMount.setProperty("kicu_mmad:invulnerable", false);
                        }, 60);
                    }
                });
            }
        }
    }
    catch { }
}
function cleanString(input) {
    return input.replace(/§[dkr5]/g, "");
}
function getHealthComp(entity) {
    return entity.getComponent("health");
}
export function createPropertyMapping(savedMount) {
    return {
        saddle: savedMount.getProperty("kicu_mmad:saddle") ?? false,
        saddle_type: savedMount.getProperty("kicu_mmad:saddle_type") ?? 0,
        food_counter: savedMount.getProperty("kicu_mmad:food_counter") ?? 0,
        nameTag: savedMount.nameTag ?? undefined,
        health: getHealthComp(savedMount).currentValue ?? undefined,
        identifier: savedMount.typeId ?? undefined,
    };
}
export function applyPropertyMapping(newEntity, propertyMapping) {
    for (const key in propertyMapping) {
        if (!key)
            continue;
        const theKey = propertyMapping[key];
        if (key === "nameTag") {
            system.run(() => {
                if (theKey) {
                    newEntity.nameTag = theKey;
                }
            });
        }
        else if (key === "health") {
            system.run(() => {
                if (theKey) {
                    const healthComponent = getHealthComp(newEntity);
                    healthComponent.setCurrentValue(theKey);
                }
            });
        }
        else if (key === "identifier") {
            continue;
        }
        else {
            newEntity.setProperty("kicu_mmad:" + key, theKey);
        }
    }
}
function getHit(hit) {
    for (let i = 0; i < hit.length; i++) {
        let ent = hit[i].entity;
        if (ent && allMountIDs.includes(ent.typeId)) {
            if (!ent.getProperty("kicu_mmad:sleep")) {
                return ent;
            }
        }
    }
}

//# sourceMappingURL=../../../../../_KiCuMountsBPDebug/kicu/mmad/C/CallingHorn.js.map
