package net.minecraft.world.level.levelgen.structure.templatesystem;

import java.util.function.Function;
import javax.annotation.Nullable;
import com.google.common.collect.UnmodifiableIterator;
import net.minecraft.world.level.block.state.BlockState;
import java.util.Random;
import net.minecraft.core.Vec3i;
import net.minecraft.util.Mth;
import net.minecraft.core.BlockPos;
import net.minecraft.world.level.LevelReader;
import java.util.Collection;
import java.util.List;
import com.google.common.collect.ImmutableList;
import com.mojang.serialization.Codec;

public class RuleProcessor extends StructureProcessor {
    public static final Codec<RuleProcessor> CODEC;
    private final ImmutableList<ProcessorRule> rules;
    
    public RuleProcessor(final List<? extends ProcessorRule> list) {
        this.rules = ImmutableList.<ProcessorRule>copyOf(list);
    }
    
    @Nullable
    @Override
    public StructureTemplate.StructureBlockInfo processBlock(final LevelReader bqd, final BlockPos fu2, final BlockPos fu3, final StructureTemplate.StructureBlockInfo c4, final StructureTemplate.StructureBlockInfo c5, final StructurePlaceSettings cvb) {
        final Random random8 = new Random(Mth.getSeed(c5.pos));
        final BlockState cfj9 = bqd.getBlockState(c5.pos);
        for (final ProcessorRule cuu11 : this.rules) {
            if (cuu11.test(c5.state, cfj9, c4.pos, c5.pos, fu3, random8)) {
                return new StructureTemplate.StructureBlockInfo(c5.pos, cuu11.getOutputState(), cuu11.getOutputTag());
            }
        }
        return c5;
    }
    
    @Override
    protected StructureProcessorType<?> getType() {
        return StructureProcessorType.RULE;
    }
    
    static {
        CODEC = ProcessorRule.CODEC.listOf().fieldOf("rules").xmap((Function)RuleProcessor::new, cux -> cux.rules).codec();
    }
}
