// priority: 1

// by blueversal

Platform.mods.kubejs.name = 'Beyond Depth Insanity';

StartupEvents.registry('item', (event) => {
    event
        .create('immortal_summoner')
        .useAnimation('bow')
        .rarity('rare')
        .maxStackSize(1)
        .use((level, player, hand) => {
            let dim = `${level.dimension}`;
            if (dim == 'minecraft:the_end' || dim == 'voidscape:void') {
                if (!level.isClientSide()) {
                    player.tell('§cYou cannot use this here!');
                }

                return false;
            }
            return true;
        })
        .useDuration((itemstack) => 1)
        .finishUsing((itemstack, level, entity) => {
            if (!entity.player) return itemstack;
            let main = entity.getHeldItem('main_hand');
            let off = entity.getHeldItem('off_hand');

            if (main.id == 'kubejs:immortal_summoner') {
                main.count--;
            } else if (off.id == 'kubejs:immortal_summoner') {
                off.count--;
            }

            global.summon_immortal(level, entity);
            return itemstack;
        })
        .displayName('Immortal Summoner')
        .tooltip('Summons the Immortal');
});

global.summon_immortal = (level, player) => {
    level.server.runCommandSilent(
        `/execute as ${player.username} in ${level.dimension} at ${player.username} run summon eeeabsmobs:immortal ~ ~ ~`
    );
    level.server.runCommandSilent(
        `/eta queue ${player.username} immortalIntro "<dur:80>[anchor value=MIDDLE][scale value=2.0][fade in=8 out=8][slam height=20 bounces=2 dur=0.6]<glitch frequency=4 chromatic=2.5><pulse frequency=2 phase=0.4><color value=#0079b6>IMMORTAL SUMMONED</color></pulse></glitch></scale>" | "<dur:100>[anchor value=MIDDLE][offset x=0 y=20][fade in=10 out=10]<typewriter speed=18 repeat=once>Guardian Core from Nameless Guardian is needed to awaken</typewriter>"`
    );
    level.server.runCommandSilent(`/effect give ${player.username} minecraft:darkness 5 0 true`);
    level.server.runCommandSilent(`/effect give ${player.username} minecraft:blindness 5 0 true`);
};