import { Block, BlockPermutation, ItemStack, Player } from "@minecraft/server";
import { brokenToolSwitcherView } from "../../../views/items/brokenToolSwitcherView";

/**
 * Evento onUseOn del componente brokenToolSwitcherComponent
 * Addon: Electrical Creativity BP
 * @param {{blockFace: string, source: Player, usedOnBlockPermutation: BlockPermutation, itemStack: ItemStack, 
 * faceLocation: {x: number, y: number, z: number}, block: Block}} event 
 */
export const brokenToolSwitcherOnUseOnEvent = ({ blockFace, source, usedOnBlockPermutation, itemStack, faceLocation, block }) => {
    if (itemStack.getDynamicProperty('nps_ec:owner_id') == undefined) {
        itemStack.setDynamicProperty('nps_ec:owner_id', source.id);
        brokenToolSwitcherView(source, itemStack);
    } else {
        const owner_id = itemStack.getDynamicProperty('nps_ec:owner_id');
        if (owner_id == source.id) {
            brokenToolSwitcherView(source, itemStack);
        } else {
            source.runCommand(`titleraw @s actionbar { "rawtext":[ { "translate":"text.tool_switcher.message", "with": ["${source.nameTag}"] } ] }`)
        }
    }
}