import { world, system } from '@minecraft/server';

const ticking_permutation = {
    onTick(event) {
        const block = event.block;
        const { x, y, z } = block.location;
        const blockPermutations = block.permutation.getAllStates();
        let newPermutationValue;
        if (blockPermutations.hasOwnProperty('furnideco:frame')) {
            let currentPermutationValue = blockPermutations['furnideco:frame'];
            if (block.typeId.includes("grandfather_clock")) {
                newPermutationValue = currentPermutationValue === 7 ? 0 : currentPermutationValue + 1;
                if (newPermutationValue === 1 || newPermutationValue === 3 || newPermutationValue === 5 || newPermutationValue === 7) {
                    block.dimension.playSound("furnideco.tick3", { x: x, y: y, z: z });
                }
                let newPermutation = block.permutation.withState('furnideco:frame', newPermutationValue);
                system.run(() => {
                    block.setPermutation(newPermutation);
                });
            }
        }
    }
};

world.beforeEvents.worldInitialize.subscribe(({ blockComponentRegistry }) => {
    blockComponentRegistry.registerCustomComponent(
        "furnideco:ticking_permutation",
        ticking_permutation
    );
});