import { world, system } from "@minecraft/server";
import { damageCustomItem } from "./durability";
import { CurrentEventProp } from "./utilities";
import { tornadoSaverId } from "./main";
const enchantedClockid = 'tvf_randev:enchanted_clock';
const enchantedClockSoundUse = 'lodestone_compass.link_compass_to_lodestone';
const enchantedClockSoundNotUse = 'block.false_permissions';
world.beforeEvents.itemUse.subscribe((event)=>{
    if (event.itemStack.typeId != enchantedClockid) return;
    const feedback = get_ProcessedEvent(event.source);
    if (feedback.event != undefined) {
        feedback.event.timer = feedback.event.maxDuration;
        feedback.event.forcedEnd = true;
        CurrentEventProp.update(feedback.event);
    //system.run(() => { world.getDimension("overworld").runCommand(`/time set day`) })
    }
    if (feedback.need_damage) {
        world.sendMessage([
            {
                translate: `tvf_randev.enchanted_clock.succsess`
            }
        ]);
        system.run(()=>event.source.playSound(enchantedClockSoundUse, {
                pitch: 0.5
            }));
        damageCustomItem(event.source, event.itemStack);
    } else {
        system.run(()=>event.source.playSound(enchantedClockSoundNotUse));
        world.sendMessage([
            {
                translate: `tvf_randev.enchanted_clock.error`
            }
        ]);
    }
});
function get_ProcessedEvent(player) {
    let feedback = {
        need_damage: false,
        event: undefined
    };
    const event = CurrentEventProp.getProp();
    const entityId = [
        'tvf_randev:black_hole',
        'tvf_randev:tornado',
        'tvf_randev:meteor'
    ];
    entityId.forEach((typeId)=>{
        player.dimension.getEntities({
            type: typeId
        }).forEach((ent)=>{
            system.run(()=>{
                ent.remove();
                let data = JSON.parse(world.getDynamicProperty(tornadoSaverId));
                delete data[ent.id];
                world.setDynamicProperty(tornadoSaverId, JSON.stringify(data));
            });
            feedback.need_damage = true;
        });
    });
    if (event.status == 'is_happening') {
        feedback.event = event;
        feedback.need_damage = true;
    }
    return feedback;
}
