import { world ,system} from "@minecraft/server"
import { UpdateBlocksAround } from "./block_update.js";
import { getScore } from "../utils/score.js";


   
const logType = ["minecraft:oak_log","minecraft:birch_log","minecraft:cherry_log","minecraft:jungle_log","minecraft:acacia_log","minecraft:dark_oak_log","minecraft:mangrove_log","minecraft:crimson_stem","minecraft:warped_stem","minecraft:spruce_log","edx:redwood_log"]
world.afterEvents.playerBreakBlock.subscribe(ev => {
    const { block,player,itemStackBeforeBreak } = ev;
    UpdateBlocksAround(block)
        
    
    if(itemStackBeforeBreak?.getTags()?.includes('axe_tier1'))
    {
        if(getScore(player,'mode')>=1)
            for(let i=1;i<5;i++)
            {
                if(logType.includes(block.above(i).typeId))
                {
                                system.runTimeout(() => {
                    block.dimension.spawnItem(block.above(i).getItemStack(1),block.location)
                    block.above(i).setType('air')
                },2)
                }
            }
    }
    if(itemStackBeforeBreak?.getTags()?.includes('axe_tier2'))
        {
            if(getScore(player,'mode')==1)
                for(let i=1;i<5;i++)
                {
                    if(logType.includes(block.above(i).typeId))
                    {
                                                        system.runTimeout(() => {
                        block.dimension.spawnItem(block.above(i).getItemStack(1),block.location)
                        block.above(i).setType('air')
                },2)   
                    }
                }
            if(getScore(player,'mode')>=2)
                for(let i=1;i<15;i++)
                {
                    if(logType.includes(block.above(i).typeId))
                    {
                        system.runTimeout(() => {
                        block.dimension.spawnItem(block.above(i).getItemStack(1),block.location)
                        block.above(i).setType('air')
                },2)   
                    }
                }
        }
    if(itemStackBeforeBreak?.getTags()?.includes('axe_tier3'))
    {
        if(getScore(player,'mode')==1)
            for(let i=1;i<5;i++)
            {
                if(logType.includes(block.above(i).typeId))
                {
                        system.runTimeout(() => {
                        block.dimension.spawnItem(block.above(i).getItemStack(1),block.location)
                        block.above(i).setType('air')
                },2)   
                }
            }
        if(getScore(player,'mode')==2)
            for(let i=1;i<15;i++)
            {
                if(logType.includes(block.above(i).typeId))
                {
                        system.runTimeout(() => {
                        block.dimension.spawnItem(block.above(i).getItemStack(1),block.location)
                        block.above(i).setType('air')
                },2)   
                }
            }
        if(getScore(player,'mode')>=3 && ((itemStackBeforeBreak?.typeId == "edx:void_axe3" && getScore(player,'light')!=1) || (itemStackBeforeBreak?.typeId == "edx:light_axe3" && getScore(player,'void')!=1)))
            for(let i=1;i<30;i++)
            {
                if(logType.includes(block.above(i).typeId))
                {
                        system.runTimeout(() => {
                        block.dimension.spawnItem(block.above(i).getItemStack(1),block.location)
                        block.above(i).setType('air')
                },2)   
                }
            }
    }
})