import { system, world } from "@minecraft/server"
import { biomeFogMap } from ".././cyd_dino_menu"

import { getBiome } from './biome_settings'

const FOG_TAG = "cyd_dino_runtime"

system.runInterval(async () => {
    const fogEnabled = world.getDynamicProperty('cyd_dino:enabled_fog') ?? false

    for (const player of world.getPlayers()) {
        if (!fogEnabled) {
            player.runCommand(`fog @s remove ${FOG_TAG}`)
            return
        }

        const biome = await getBiome(player)
        if (!biome) continue

        const fogId = biomeFogMap.get(biome) ?? 'cyd_dino:fog_default'

        try {
            player.runCommand(`fog @s remove ${FOG_TAG}`);
        } catch { }

        try {
            player.runCommand(`fog @s push ${fogId} ${FOG_TAG}`);
        } catch { }
    }
}, 40)