import { BlockManager } from "../Block/manager";
export class BlockPoweredEvent {
    tick = (block, dimension) => {
        const loc = block.location;
        let poweredState = block.permutation.getState(this.stateId);
        const offsets = [
            { x: 0, y: 1, z: 0 },
            { x: 0, y: -1, z: 0 },
            { x: 1, y: 0, z: 0 },
            { x: -1, y: 0, z: 0 },
            { x: 0, y: 0, z: 1 },
            { x: 0, y: 0, z: -1 }
        ];
        let powered = false;
        const redstonePower = block.getRedstonePower();
        if (redstonePower != 0 && redstonePower != undefined)
            powered = true;
        for (const offset of offsets) {
            if (powered)
                continue;
            const nearbyBlock = BlockManager.getBlock(dimension, { x: loc.x + offset.x, y: loc.y + offset.y, z: loc.z + offset.z });
            const redstonePower = nearbyBlock?.getRedstonePower();
            if (redstonePower != 0 && redstonePower != undefined)
                powered = true;
        }
        if (powered) {
            if (this.events.whilePowered)
                this.events.whilePowered({ block: block, dimension: dimension });
            if (!poweredState) {
                block.setPermutation(block.permutation.withState(this.stateId, true));
                if (this.events.onPower)
                    this.events.onPower({ block: block, dimension: dimension });
            }
        }
        else if (poweredState) {
            block.setPermutation(block.permutation.withState(this.stateId, false));
            if (this.events.onUnpowered)
                this.events.onUnpowered({ block: block, dimension: dimension });
        }
    };
    events;
    stateId;
    constructor(stateId, events) {
        this.events = events;
        this.stateId = stateId;
    }
}
