import { performRotation } from "./wrenchStuff";
import { flipDir, getADirection, isSameAxis, offsetPos } from "../util/dirUtils";
import { SuppStates } from "../reg/suppStates";
import { worldProxy } from "../worldProxy";
export const TurnTableTickComponent = {
    onTick(event) {
        const block = event.block;
        const power = block.getRedstonePower() | 0;
        if ((power > 0) != block.permutation.getState(SuppStates.Powered)) {
            block.setPermutation(block.permutation.withState(SuppStates.Powered, power > 0));
        }
        if (power <= 0)
            return;
        const interval = 1 + ((16 - power) * 2); // has to be a multiple of tick rate (1)
        //tick starts when block is placed. syncing with tick time would be tricky and not worth it
        if (power > 0 && worldProxy.getTickTime() % interval == 0) {
            const counterClockWise = false; // block.permutation.getState("razz_sup:left"); //perm
            let dir = getADirection(block); // perm
            dir = flipDir(dir);
            const targetBlock = offsetPos(block, dir);
            if (targetBlock && targetBlock.isValid() && !targetBlock.isAir) {
                performRotation(targetBlock, dir, counterClockWise);
            }
        }
    },
};
export const TurnTableInteractComponent = {
    onPlayerInteract(event) {
        const block = event.block;
        const dir = getADirection(block);
        if (isSameAxis(dir, event.face))
            return;
        const side = block.permutation.getState(SuppStates.Rotation);
        block.setPermutation(block.permutation.withState(SuppStates.Rotation, side == "left" ? "right" : "left"));
        const opt = { pitch: side == "left" ? 0.5 : 0.55, volume: 1 };
        event.dimension.playSound("random.lever_click", block.center(), opt);
    }
};
