import { Block, Dimension, GameMode, ItemStack, Player } from "@minecraft/server";
import { CustomBlockTags } from "../utils/blockTags";
import { Piki } from "../../utils/typingConstants";
import { BlockValuedTagsKeys } from "../utils/blockConstants";
import { BlockHelper as BHelper } from "../utils/blockHelpers";
import { CommandHelper as CHelper } from "../../utils/commandHelper";
import { ItemHelper } from "../../items/utils/itemHelper";

export const CUSTOM_BRICKS = {
    /**
     * 
     * @param {Block} block 
     * @param {Dimension} dimension 
     * @param {ItemStack} itemStack 
     * @param {Player} player
     */
    dropLoot(block, dimension, itemStack, player) {
        const blockId = block.typeId

        if (player.getGameMode() == GameMode.creative) return;

        const toolRequired = BHelper.getBlockTagValue(block, BlockValuedTagsKeys.correctTool)
        const toolUsed = ItemHelper.getItemToolTier(itemStack)
        
        if (toolRequired != toolUsed) return;

        const lootFile = this._getLootFile(blockId)
        const command = CHelper.generateSpawnLootCommand(block.location, lootFile)
        dimension.runCommandAsync(command)
    },
    isCustomBrick(block) {
        const isCustom = CustomBlockTags.isCustom(block)
        const isBrick = CustomBlockTags.isBrick(block)
        return isCustom && isBrick
    },
    /**
     * HACK:
     * @param {string} blockTypeId 
     * @returns 
     */
    _getLootFile(blockTypeId) {
        return `${blockTypeId.replace(Piki.namespace, Piki.blockLootsPath)}`
    }
}