import { ItemStack } from '@minecraft/server';
import { getBlockContainer, shrinkItemStack, getBlockState, updateBlockState } from '../../utils/utils.js';
import { getEntityHeldItem, setEntityHeldItem } from '../../utils/players.js';
import { HammerRecipes } from '../../recipes/recipes.js';
import { addItemToDrawerV2 } from '../../compact/storage_drawers.js';

class AutoHammerBlockComponent {
    onTick(event) {
        const block = event.block;
        const hammerRecipe = getAutoHammerRecipe(block.permutation);
        const stage = getStage(block.permutation);
        if (hammerRecipe === HammerRecipes.EMPTY) {
            if (stage === AutoHammerStage.STAGE_UP) {
                const containerAbove = getBlockContainer(block.above(1));
                if (!containerAbove) {
                    return;
                }
                for (let i = 0; i < containerAbove.size; i++) {
                    const itemStack = containerAbove.getItem(i);
                    if (!itemStack) {
                        continue;
                    }
                    const recipe = HammerRecipes.getHammerRecipeFromBlockId(itemStack.typeId);
                    if (recipe !== HammerRecipes.EMPTY) {
                        const newStack = shrinkItemStack(itemStack);
                        containerAbove.setItem(i, newStack);
                        setAutoHammerRecipe(block, recipe);
                        return;
                    }
                }
                return;
            }
        }
        if (stage === AutoHammerStage.STAGE_CRUSH_FOUR) {
            const blockUnder = block.below(1);
            const containerBelow = getBlockContainer(blockUnder);
            const stackToAdd = new ItemStack(hammerRecipe.itemDrop, 1);
            if (!containerBelow) {
                if (blockUnder.hasTag("ftb_sd:storage_drawer")) {
                    addItemToDrawerV2(blockUnder, stackToAdd);
                }
                return;
            }
            const stackNotAdded = containerBelow.addItem(stackToAdd);
            if (stackNotAdded !== undefined) {
                return;
            }
            setAutoHammerRecipe(block, HammerRecipes.EMPTY);
            setStage(block, AutoHammerStage.STAGE_UP);
            return;
        }
        setStage(block, stage + 1);
    }
    onPlayerInteract(event) {
        const block = event.block;
        const player = event.player;
        const hammerRecipe = getAutoHammerRecipe(block.permutation);
        const heldItem = getEntityHeldItem(player);
        if (hammerRecipe === HammerRecipes.EMPTY) {
            if (heldItem === undefined) {
                return;
            }
            const recipe = HammerRecipes.getHammerRecipeFromBlockId(heldItem.typeId);
            if (recipe === HammerRecipes.EMPTY) {
                return;
            }
            const newStack = shrinkItemStack(heldItem);
            setEntityHeldItem(player, newStack);
            setAutoHammerRecipe(block, recipe);
            return;
        }
    }
}
function getStage(blockPermutation) {
    return getBlockState(blockPermutation, AutoHammerBlockStates.STAGE);
}
function setStage(block, stage) {
    updateBlockState(block, AutoHammerBlockStates.STAGE, stage);
}
function setAutoHammerRecipe(block, hammerRecipe) {
    updateBlockState(block, AutoHammerBlockStates.BLOCK_TYPE_1, hammerRecipe.autoHammerId[0]);
    updateBlockState(block, AutoHammerBlockStates.BLOCK_TYPE_2, hammerRecipe.autoHammerId[1]);
}
function getAutoHammerRecipe(block) {
    const blockType1 = getBlockState(block, AutoHammerBlockStates.BLOCK_TYPE_1);
    const blockType2 = getBlockState(block, AutoHammerBlockStates.BLOCK_TYPE_2);
    for (const recipe of HammerRecipes.RECIPES) {
        if (recipe.autoHammerId[0] === blockType1 && recipe.autoHammerId[1] === blockType2) {
            return recipe;
        }
    }
    return HammerRecipes.EMPTY;
}
var AutoHammerBlockStates;
(function (AutoHammerBlockStates) {
    AutoHammerBlockStates["BLOCK_TYPE_1"] = "ftb_sieves:block_type_1";
    AutoHammerBlockStates["BLOCK_TYPE_2"] = "ftb_sieves:block_type_2";
    AutoHammerBlockStates["STAGE"] = "ftb_sieves:stage";
})(AutoHammerBlockStates || (AutoHammerBlockStates = {}));
var AutoHammerStage;
(function (AutoHammerStage) {
    AutoHammerStage[AutoHammerStage["STAGE_UP"] = 0] = "STAGE_UP";
    AutoHammerStage[AutoHammerStage["STAGE_CRUSH_ONE"] = 1] = "STAGE_CRUSH_ONE";
    AutoHammerStage[AutoHammerStage["STAGE_CRUSH_TWO"] = 2] = "STAGE_CRUSH_TWO";
    AutoHammerStage[AutoHammerStage["STAGE_CRUSH_THREE"] = 3] = "STAGE_CRUSH_THREE";
    AutoHammerStage[AutoHammerStage["STAGE_CRUSH_FOUR"] = 4] = "STAGE_CRUSH_FOUR";
})(AutoHammerStage || (AutoHammerStage = {}));

export { AutoHammerBlockComponent, AutoHammerBlockStates, AutoHammerStage };
