import { Direction } from "@minecraft/server";
import { getRandomDirection } from "../util/dirUtils";
import { getBlocksInRadius, hasAnyAround, spawnParticleOnBlockFace, spawnParticleOnBlockFaces } from "../util/miscUtils";
import { McBlocks } from "../reg/mcBlocks";
import { suppEvents } from "../suppEventHandler";
import { hasWater, isIce, isLava } from "../util/blockProperties";
import { vecToString } from "../util/mthUtils";
import { McParticles } from "../reg/mcParticles";
import { SuppParticles } from "../reg/suppParticles";
suppEvents.subscribeOnNeighborChanged((event => {
    if (event.block.typeId == "razz_sup:pumice" /* SuppBlocks.Pumice */) {
        tryAbsorbing(event.block);
    }
    else if (event.block.typeId == "razz_sup:scorching_pumice" /* SuppBlocks.ScorchingPumice */) {
        tryCooling(event.block);
    }
}));
export const PumiceExtinguishComponent = {
    onPlace(event) {
        const block = event.block;
        tryCooling(block);
    },
    onTick(event) {
        const dir = getRandomDirection([Direction.Up]);
        spawnParticleOnBlockFace(event.block, dir, McParticles.LavaDrip, 1, 0.0225);
    }
};
export const PumiceSuckLavaComponent = {
    onPlace(event) {
        const block = event.block;
        tryAbsorbing(block);
    }
};
function tryCooling(block) {
    if (hasAnyAround(block, undefined, b => {
        return hasWater(b) || isIce(b);
    })) {
        if (!hasAnyAround(block, undefined, isLava)) {
            block.dimension.playSound("random.fizz", block.center());
            block.setType("razz_sup:pumice" /* SuppBlocks.Pumice */);
            spawnParticleOnBlockFaces(block, McParticles.Smoke, 5);
        }
    }
}
function tryAbsorbing(block) {
    const dimension = block.dimension;
    const radius = 3;
    const position = block.location;
    //so we start from the center location. we have a list of visited positions. our goal is to recursively visit all locations till radius
    const blocks = getBlocksInRadius(position, radius, dimension, (b) => (b.typeId == McBlocks.Lava || b.typeId == McBlocks.FlowingLava));
    if (blocks.length > 0) {
        block.dimension.spawnParticle(SuppParticles.PumiceAbsorbLava, block.center());
        dimension.playSound("bucket.fill_lava", block.center());
        //has at least 1 lava source
        for (const b of blocks) {
            if (b.typeId == McBlocks.Lava && b.permutation.getState("liquid_depth") == 0) {
                console.log(vecToString(b.location));
                block.setType("razz_sup:scorching_pumice" /* SuppBlocks.LavaPumice */);
                break;
            }
        }
        for (const neighbor of blocks) {
            neighbor.setType(McBlocks.Air);
        }
    }
}
