//done with event so we dont consume the interaction for all other clicks
import { system, world } from "@minecraft/server";
export const EndStoneLampComponent = {
    //apparently having this also swings the animation JUSt on empty item, at random. unironically just what we want...
    beforeOnPlayerPlace: (event) => {
        // Generate random values
        const randomLampA = Math.floor(Math.random() * 2); // 0 or 1
        const randomLampB = Math.floor(Math.random() * 13); // 0 to 12
        // Set block states
        event.permutationToPlace = event.permutationToPlace.withState("razz_sup:lamp_id_a", randomLampA)
            .withState("razz_sup:lamp_id_b", randomLampB)
            .withState("razz_sup:end_lamp_placed", 1);
    }
};
//now this isn't in the component as consuming all interactions for a full block is bad
world.beforeEvents.playerInteractWithBlock.subscribe((event) => {
    const block = event.block;
    if (block.typeId != "razz_sup:end_stone_lamp" /* SuppBlocks.EndStoneLamp */)
        return;
    const item = event.itemStack;
    if (item)
        return;
    cycleLampState(block);
    //no need to cancel as its empty item interaction
});
function cycleLampState(block) {
    const permutation = block.permutation;
    const dim = block.dimension;
    //has clean states because its greater than 16
    const lampA = permutation.getState("razz_sup:lamp_id_a");
    const lampB = permutation.getState("razz_sup:lamp_id_b");
    let currentIndex = lampA * 13 + lampB;
    let nextIndex = (currentIndex + 1) % 26;
    let newLampA = Math.floor(nextIndex / 13);
    let newLampB = nextIndex % 13;
    system.run(() => {
        //TODO: sound here
        dim.setBlockPermutation(block.location, permutation
            .withState("razz_sup:lamp_id_a", newLampA)
            .withState("razz_sup:lamp_id_b", newLampB));
    });
}
