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

import net.minecraft.nbt.Tag;
import net.minecraft.util.Deserializer;
import net.minecraft.core.Registry;
import net.minecraft.nbt.NbtOps;
import java.util.Map;
import com.google.common.collect.ImmutableMap;
import com.mojang.datafixers.Dynamic;
import com.mojang.datafixers.types.DynamicOps;
import java.util.Random;
import javax.annotation.Nullable;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.world.level.block.state.BlockState;

public class ProcessorRule {
    private final RuleTest inputPredicate;
    private final RuleTest locPredicate;
    private final BlockState outputState;
    @Nullable
    private final CompoundTag outputTag;
    
    public ProcessorRule(final RuleTest cmm1, final RuleTest cmm2, final BlockState byj) {
        this(cmm1, cmm2, byj, null);
    }
    
    public ProcessorRule(final RuleTest cmm1, final RuleTest cmm2, final BlockState byj, @Nullable final CompoundTag jt) {
        this.inputPredicate = cmm1;
        this.locPredicate = cmm2;
        this.outputState = byj;
        this.outputTag = jt;
    }
    
    public boolean test(final BlockState byj1, final BlockState byj2, final Random random) {
        return this.inputPredicate.test(byj1, random) && this.locPredicate.test(byj2, random);
    }
    
    public BlockState getOutputState() {
        return this.outputState;
    }
    
    @Nullable
    public CompoundTag getOutputTag() {
        return this.outputTag;
    }
    
    public <T> Dynamic<T> serialize(final DynamicOps<T> dynamicOps) {
        final T object3 = (T)dynamicOps.createMap((Map)ImmutableMap.of(dynamicOps.createString("input_predicate"), this.inputPredicate.<T>serialize(dynamicOps).getValue(), dynamicOps.createString("location_predicate"), this.locPredicate.<T>serialize(dynamicOps).getValue(), dynamicOps.createString("output_state"), BlockState.<T>serialize(dynamicOps, this.outputState).getValue()));
        if (this.outputTag == null) {
            return (Dynamic<T>)new Dynamic((DynamicOps)dynamicOps, object3);
        }
        return (Dynamic<T>)new Dynamic((DynamicOps)dynamicOps, dynamicOps.mergeInto(object3, dynamicOps.createString("output_nbt"), new Dynamic((DynamicOps)NbtOps.INSTANCE, this.outputTag).convert((DynamicOps)dynamicOps).getValue()));
    }
    
    public static <T> ProcessorRule deserialize(final Dynamic<T> dynamic) {
        final Dynamic<T> dynamic2 = (Dynamic<T>)dynamic.get("input_predicate").orElseEmptyMap();
        final Dynamic<T> dynamic3 = (Dynamic<T>)dynamic.get("location_predicate").orElseEmptyMap();
        final RuleTest cmm4 = Deserializer.<T, AlwaysTrueTest, RuleTestType>deserialize(dynamic2, Registry.RULE_TEST, "predicate_type", AlwaysTrueTest.INSTANCE);
        final RuleTest cmm5 = Deserializer.<T, AlwaysTrueTest, RuleTestType>deserialize(dynamic3, Registry.RULE_TEST, "predicate_type", AlwaysTrueTest.INSTANCE);
        final BlockState byj6 = BlockState.deserialize((com.mojang.datafixers.Dynamic<Object>)dynamic.get("output_state").orElseEmptyMap());
        final CompoundTag jt7 = dynamic.get("output_nbt").map(dynamic -> (Tag)dynamic.convert((DynamicOps)NbtOps.INSTANCE).getValue()).orElse(null);
        return new ProcessorRule(cmm4, cmm5, byj6, jt7);
    }
}
