import { system, WeatherType, world } from "@minecraft/server";
import { getOrCreateWorldData, setWorldData } from "./util/miscUtils";
import { vecEqual } from "./util/mthUtils";
const WEATHER_KEY = "razz_sup_weather";
const knownDimensions = new Set();
knownDimensions.add("overworld");
knownDimensions.add("nether");
knownDimensions.add("the_end");
class WorldProxy {
    //Fix constructor
    constructor() {
        this.tickTime = 0;
        this.timeOfDay = 0;
        const orCreateWorldData = getOrCreateWorldData(WEATHER_KEY, this.encodeWeather(WeatherType.Clear));
        this.weather = this.decodeWeather(orCreateWorldData);
        world.afterEvents.weatherChange.subscribe((event) => {
            this.weather = event.newWeather;
            this.tickTime = system.currentTick;
            setWorldData(WEATHER_KEY, this.encodeWeather(event.newWeather));
        });
        world.afterEvents.playerDimensionChange.subscribe((event) => {
            //best we can do to detect new dimensions
            const dim = event.toDimension.id;
            if (!knownDimensions.has(dim)) {
                knownDimensions.add(dim);
            }
        });
        system.runInterval(() => {
            this.timeOfDay = world.getTimeOfDay();
            this.tickTime++;
        }, 1);
    }
    encodeWeather(type) {
        switch (type) {
            case WeatherType.Clear:
                return 0;
            case WeatherType.Rain:
                return 1;
            case WeatherType.Thunder:
                return 2;
            default:
                return 0;
        }
    }
    decodeWeather(str) {
        switch (str) {
            case 0:
                return WeatherType.Clear;
            case 1:
                return WeatherType.Rain;
            case 2:
                return WeatherType.Thunder;
            default:
                return WeatherType.Clear;
        }
    }
    getKnownDimensions() {
        const dimensions = [];
        for (const d of knownDimensions) {
            const dim = world.getDimension(d);
            if (dim) {
                dimensions.push(dim);
            }
        }
        return dimensions;
    }
    getWeather() {
        return this.weather;
    }
    getTimeOfDay() {
        return this.timeOfDay;
    }
    getTickTime() {
        return this.tickTime;
    }
    hasRainAt(block) {
        if (this.getWeather() != WeatherType.Clear) {
            const top = block.dimension.getTopmostBlock({ x: block.x, z: block.z });
            if (top && vecEqual(top.location, block.location)) {
                return true;
            }
        }
        return false;
    }
}
export const worldProxy = new WorldProxy();
