import { system } from "@minecraft/server";
import { getItemInHand, getItemInInventory, giveItemToPlayer, isAxe, isHoe, isPickaxe, isShovel, isSword, setItemInInventory, setItemInMainHand, spillInventory, splitItem } from "../util/miscUtils";
import { blockEntityManager } from "../blockEntitiesManager";
import { SuppEntities } from "../reg/suppEntities";
import { isItem2d } from "../util/blockProperties";
import { McTags } from "../reg/mcTags";
import { McItems } from "../reg/mcItems";
import { SuppItems } from "../reg/suppItems";
import { SuppStates } from "../reg/suppStates";
blockEntityManager.registerEntityBlock("razz_sup:pedestal" /* SuppBlocks.Pedestal */, SuppEntities.Pedestal, {
    isOptional() {
        return true;
    },
    onBroken(blockPermutation, pos, blockEntity) {
        spillInventory(blockEntity);
    }
});
const ITEM_MODEL_TYPE = "razz_sup:item_model_type";
export const PedestalComponent = {
    onPlayerInteract(event) {
        let block = event.block;
        const player = event.player;
        if (!player || player.isSneaking)
            return;
        const be = blockEntityManager.getOrCreateBlockEntity(block);
        const itemInHand = getItemInHand(player);
        const beItem = getItemInInventory(be);
        if (itemInHand && !beItem) {
            const item = splitItem(player, itemInHand).clone();
            system.runTimeout(() => {
                setItemInMainHand(be, item);
            }, 2); //so animation is already playing
            setItemInInventory(be, item);
            const type = getItemModelType(item);
            be.setProperty(ITEM_MODEL_TYPE, type);
            block.dimension.playSound("block.itemframe.add_item", block.center());
            block.setPermutation(block.permutation.withState(SuppStates.HasItem, true));
        }
        else if (!itemInHand && beItem) {
            giveItemToPlayer(player, beItem);
            setItemInInventory(be, null);
            setItemInMainHand(be, null);
            block.setPermutation(block.permutation.withState(SuppStates.HasItem, false));
        }
        const newItem = getItemInInventory(be);
        if (!newItem) {
            be.remove();
        }
    }
};
function isTool(item) {
    return item.hasTag(McTags.IsTool) || isShovel(item) || isAxe(item) || isPickaxe(item) || isHoe(item) || isSword(item)
        || item.typeId == McItems.Bow || item.typeId == McItems.FishingRod || item.typeId == SuppItems.Slingshot;
}
function isShield(item) {
    return item.typeId.endsWith("shield");
}
//let me just rant how BAD the attachment system is. All the models with attachment are SUPER inconsistent with respect to vanilla rotations and positions. take a bow or examle. try punching with it. Yea.
//oh and want to give a item a custom model with vanilla rotations? screw you! painfully try to recreate them becuase the ones there are hardcoded and even then it wont match,animationwill be all woky.
//oh and if you do manage good job, item thrown on the ground will still have 2d model lmao. Why can blocks be customized? why blocks create their item form automatically? why does it seam its all "simplified" for the sake of being simpliied when in reality it just gives us a clunky inconsistent and less powerful system?
function getItemModelType(item) {
    if (isShield(item))
        return "shield";
    if (item.typeId == McItems.Spyglass || item.typeId == SuppItems.Slingshot)
        return "spyglass";
    if (isTool(item))
        return "tool";
    if (isItem2d(item))
        return "item";
    return "block";
}
