import { MinecraftDimensionTypes } from '@minecraft/server';
import { MessageFormData } from '@minecraft/server-ui';
import { decreaseItemAmountInHand, getItemInSlot, translate } from '../../util/utils';
import Totem from '../totem';
function getTotemFromInteraction(player, lodestone) {
    if (!player.isValid())
        return undefined;
    const totem = Totem.fromLodestone(lodestone);
    if (!totem || !totem.isIntact())
        return undefined;
    const heldItem = getItemInSlot(player);
    if (!heldItem || heldItem.typeId !== 'twb_wisu:withered_core') {
        player.onScreenDisplay.setActionBar(translate('twb_wisu.totem.missing_core'));
        return undefined;
    }
    if (player.dimension.id !== MinecraftDimensionTypes.overworld) {
        player.onScreenDisplay.setActionBar(translate('twb_wisu.totem.wrong_dimension'));
        return undefined;
    }
    return totem;
}
export default class TotemInteractComponent {
    onPlayerInteract(e) {
        const { player, block: lodestone } = e;
        if (!player)
            return;
        if (!getTotemFromInteraction(player, lodestone))
            return;
        const messageForm = new MessageFormData()
            .title(translate('twb_wisu.totem.confirm_activation.title'))
            .body(translate('twb_wisu.totem.confirm_activation.body'))
            .button1(translate('twb_wisu.totem.confirm_activation.deny'))
            .button2(translate('twb_wisu.totem.confirm_activation.confirm'));
        messageForm.show(player).then((r) => {
            if (r.selection !== 1)
                return;
            const totem = getTotemFromInteraction(player, lodestone);
            if (!totem)
                return;
            totem.setActivated();
            decreaseItemAmountInHand(player, 1, true);
        });
    }
}
