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

import java.util.Map;
import com.google.common.collect.ImmutableMap;
import java.util.stream.Stream;
import com.mojang.datafixers.types.DynamicOps;
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.function.Function;
import com.mojang.datafixers.Dynamic;
import java.util.Collection;
import java.util.List;
import com.google.common.collect.ImmutableList;

public class RuleProcessor extends StructureProcessor {
    private final ImmutableList<ProcessorRule> rules;
    
    public RuleProcessor(final List<ProcessorRule> list) {
        this.rules = ImmutableList.<ProcessorRule>copyOf(list);
    }
    
    public RuleProcessor(final Dynamic<?> dynamic) {
        this(dynamic.get("rules").asList((Function)ProcessorRule::deserialize));
    }
    
    @Nullable
    @Override
    public StructureTemplate.StructureBlockInfo processBlock(final LevelReader bjz, final BlockPos fk, final StructureTemplate.StructureBlockInfo b3, final StructureTemplate.StructureBlockInfo b4, final StructurePlaceSettings cmp) {
        final Random random7 = new Random(Mth.getSeed(b4.pos));
        final BlockState byj8 = bjz.getBlockState(b4.pos);
        for (final ProcessorRule cmi10 : this.rules) {
            if (cmi10.test(b4.state, byj8, random7)) {
                return new StructureTemplate.StructureBlockInfo(b4.pos, cmi10.getOutputState(), cmi10.getOutputTag());
            }
        }
        return b4;
    }
    
    @Override
    protected StructureProcessorType getType() {
        return StructureProcessorType.RULE;
    }
    
    @Override
    protected <T> Dynamic<T> getDynamic(final DynamicOps<T> dynamicOps) {
        return (Dynamic<T>)new Dynamic((DynamicOps)dynamicOps, dynamicOps.createMap((Map)ImmutableMap.of(dynamicOps.createString("rules"), dynamicOps.createList((Stream)this.rules.stream().map(cmi -> cmi.<T>serialize(dynamicOps).getValue())))));
    }
}
