import { Direction } from "@minecraft/server";
import { BlockPoweredEvent } from "../../Block Events/redstone";
export class BlockComponentFenceGate {
    onPlayerInteract = (data) => {
        const { block, face, dimension } = data;
        const open = block.permutation.getState(this.openStateId);
        switch (open) {
            case "none":
                let openTo = "forward";
                switch (face) {
                    case Direction.South:
                        openTo = "backward";
                        break;
                    case Direction.East:
                        openTo = "backward";
                        break;
                }
                block.setPermutation(block.permutation.withState(this.openStateId, openTo));
                if (this.openSound) {
                    const sound = this.openSound;
                    dimension.playSound(sound.id, block.center(), { volume: sound.volume, pitch: sound.pitch });
                }
                break;
            case "forward":
                block.setPermutation(block.permutation.withState(this.openStateId, "none"));
                if (this.closeSound) {
                    const sound = this.closeSound;
                    dimension.playSound(sound.id, block.center(), { volume: sound.volume, pitch: sound.pitch });
                }
                break;
            case "backward":
                block.setPermutation(block.permutation.withState(this.openStateId, "none"));
                if (this.closeSound) {
                    const sound = this.closeSound;
                    dimension.playSound(sound.id, block.center(), { volume: sound.volume, pitch: sound.pitch });
                }
                break;
        }
    };
    beforeOnPlayerPlace = (data) => {
        data.permutationToPlace = data.permutationToPlace.withState(this.displayStateId, false);
    };
    onTick = (data) => {
        this.redstoneEvent.tick(data.block, data.dimension);
    };
    redstoneEvent;
    openStateId;
    displayStateId;
    redstoneStateId;
    openSound;
    closeSound;
    constructor(openStateId, displayStateId, redstoneStateId, openSound, closeSound) {
        this.openStateId = openStateId;
        this.displayStateId = displayStateId;
        this.redstoneStateId = redstoneStateId;
        this.openSound = openSound;
        this.closeSound = closeSound;
        this.redstoneEvent = new BlockPoweredEvent(redstoneStateId, {
            onPower({ block, dimension }) {
                const open = block.permutation.getState(openStateId);
                if (open != "none")
                    return;
                block.setPermutation(block.permutation.withState(openStateId, "forward"));
                if (openSound) {
                    const sound = openSound;
                    dimension.playSound(sound.id, block.center(), { volume: sound.volume, pitch: sound.pitch });
                }
            },
            onUnpowered({ block, dimension }) {
                const open = block.permutation.getState(openStateId);
                if (open == "none")
                    return;
                block.setPermutation(block.permutation.withState(openStateId, "none"));
                if (closeSound) {
                    const sound = closeSound;
                    dimension.playSound(sound.id, block.center(), { volume: sound.volume, pitch: sound.pitch });
                }
            }
        });
    }
}
