import { BlockPermutation, Direction, EntityComponentTypes, ItemStack, world } from "@minecraft/server";
import { fallingBlocksManager } from "../fallingBlocksManager";
import { worldProxy } from "../worldProxy";
import { SuppItems } from "../reg/suppItems";
import { SuppStates } from "../reg/suppStates";
import { suppEvents } from "../suppEventHandler";
import { McBlocks } from "../reg/mcBlocks";
import { configs } from "../configs";
import { isReplaceable } from "../util/blockProperties";
world.afterEvents.entityDie.subscribe((event) => {
    const onFire = event.deadEntity.getComponent(EntityComponentTypes.OnFire);
    if (onFire) {
        if (Math.random() < (configs.ashFromMobsChance.getValue() / 100)) {
            event.deadEntity.dimension.spawnItem(new ItemStack(SuppItems.Ash, 1), event.deadEntity.location);
        }
    }
});
fallingBlocksManager.register("razz_sup:ash" /* SuppBlocks.Ash */, {
    placeEntityOntoSpecial(entity, target, permutation) {
        let blockBelow = target;
        if (blockBelow && blockBelow.typeId == "razz_sup:ash" /* SuppBlocks.Ash */) {
            const layers = blockBelow.permutation.getState(SuppStates.LayerCount);
            const entityLayers = permutation.getState(SuppStates.LayerCount);
            if (layers < 8) {
                //give all the layers that the entity can to this block, then update the entity with the remaining layers
                const newLayers = Math.min(layers + entityLayers, 8);
                blockBelow.setPermutation(blockBelow.permutation
                    .withState(SuppStates.LayerCount, newLayers));
                const remainingLayers = Math.max(entityLayers - (newLayers - layers), 0);
                entity.setDynamicProperty(SuppStates.LayerCount, remainingLayers);
                if (remainingLayers == 0) {
                    return true;
                }
                const above = blockBelow.above();
                if (isReplaceable(above)) {
                    //set remaining above
                    above.setPermutation(BlockPermutation.resolve("razz_sup:ash" /* SuppBlocks.Ash */)
                        .withState(SuppStates.LayerCount, remainingLayers));
                    return true;
                }
                //we have missing. need to drop
            }
            else {
                //try placing above
                const above = blockBelow.above();
                if (isReplaceable(above)) {
                    above.setPermutation(BlockPermutation.resolve("razz_sup:ash" /* SuppBlocks.Ash */)
                        .withState(SuppStates.LayerCount, entityLayers));
                    entity.setDynamicProperty(SuppStates.LayerCount, 0);
                    return true;
                }
            }
        }
        return false;
    },
    canFallThroughSpecial(blockBelow) {
        return blockBelow.typeId == "razz_sup:ash" /* SuppBlocks.Ash */ && blockBelow.permutation.getState(SuppStates.LayerCount) < 8;
    }
});
suppEvents.subscribeOnNeighborChanged((event) => {
    if (event.fromDir == Direction.Up) {
        const block = event.block;
        if (block.typeId == "razz_sup:ashen_basalt" /* SuppBlocks.AshenBasalt */ && event.neighborBlock.typeId != "razz_sup:ash" /* SuppBlocks.Ash */) {
            block.setType(McBlocks.Basalt);
        }
        else if (block.typeId == McBlocks.Basalt && event.neighborBlock.typeId == "razz_sup:ash" /* SuppBlocks.Ash */) {
            block.setType("razz_sup:ashen_basalt" /* SuppBlocks.AshenBasalt */);
        }
    }
});
export const AshLayerComponent = {
    onRandomTick(event) {
        if (worldProxy.hasRainAt(event.block)) {
            //decreases 1layer
            const layers = event.block.permutation.getState(SuppStates.LayerCount);
            const value = layers - 1;
            if (value <= 0) {
                event.block.setType(McBlocks.Air);
                suppEvents.fireOnBlockBroken(event.block);
            }
            else
                event.block.setPermutation(event.block.permutation.withState(SuppStates.LayerCount, value));
        }
    }
};
