package net.minecraft.client.renderer.block.model.multipart;

import com.google.common.base.MoreObjects;
import java.util.Optional;
import net.minecraft.world.level.block.state.properties.Property;
import java.util.stream.Collector;
import java.util.stream.Collectors;
import java.util.List;
import java.util.function.Predicate;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.state.StateDefinition;
import com.google.common.base.Splitter;

public class KeyValueCondition implements Condition {
    private static final Splitter PIPE_SPLITTER;
    private final String key;
    private final String value;
    
    public KeyValueCondition(final String string1, final String string2) {
        this.key = string1;
        this.value = string2;
    }
    
    @Override
    public Predicate<BlockState> getPredicate(final StateDefinition<Block, BlockState> byk) {
        final Property<?> bzm3 = byk.getProperty(this.key);
        if (bzm3 == null) {
            throw new RuntimeException(String.format("Unknown property '%s' on '%s'", this.key, byk.getOwner().toString()));
        }
        String string2 = this.value;
        final boolean boolean5 = !string2.isEmpty() && string2.charAt(0) == '!';
        if (boolean5) {
            string2 = string2.substring(1);
        }
        final List<String> list6 = KeyValueCondition.PIPE_SPLITTER.splitToList(string2);
        if (list6.isEmpty()) {
            throw new RuntimeException(String.format("Empty value '%s' for property '%s' on '%s'", this.value, this.key, byk.getOwner().toString()));
        }
        Predicate<BlockState> predicate2;
        if (list6.size() == 1) {
            predicate2 = this.getBlockStatePredicate(byk, bzm3, string2);
        }
        else {
            final List<Predicate<BlockState>> list7 = list6.stream().map(string -> this.getBlockStatePredicate(byk, bzm3, string)).collect(Collectors.toList());
            predicate2 = (byj -> list7.stream().anyMatch(predicate -> predicate.test(byj)));
        }
        return boolean5 ? predicate2.negate() : predicate2;
    }
    
    private Predicate<BlockState> getBlockStatePredicate(final StateDefinition<Block, BlockState> byk, final Property<?> bzm, final String string) {
        final Optional<?> optional5 = bzm.getValue(string);
        if (!optional5.isPresent()) {
            throw new RuntimeException(String.format("Unknown value '%s' for property '%s' on '%s' in '%s'", string, this.key, byk.getOwner().toString(), this.value));
        }
        return byj -> byj.getValue(bzm).equals(optional5.get());
    }
    
    @Override
    public String toString() {
        return MoreObjects.toStringHelper(this).add("key", this.key).add("value", this.value).toString();
    }
    
    static {
        PIPE_SPLITTER = Splitter.on('|').omitEmptyStrings();
    }
}
