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

import net.minecraft.world.entity.LivingEntity;
import javax.annotation.Nullable;
import net.minecraft.client.multiplayer.ClientLevel;
import net.minecraft.world.item.ItemStack;
import java.util.stream.Collector;
import java.util.stream.Collectors;
import net.minecraft.client.resources.model.ModelState;
import net.minecraft.client.resources.model.BlockModelRotation;
import java.util.Objects;
import net.minecraft.client.resources.model.UnbakedModel;
import net.minecraft.resources.ResourceLocation;
import java.util.function.Function;
import net.minecraft.client.resources.model.ModelBakery;
import java.util.Collections;
import com.google.common.collect.Lists;
import net.minecraft.client.resources.model.BakedModel;
import java.util.List;

public class ItemOverrides {
    public static final ItemOverrides EMPTY;
    private final List<ItemOverride> overrides;
    private final List<BakedModel> overrideModels;
    
    private ItemOverrides() {
        this.overrides = Lists.newArrayList();
        this.overrideModels = Collections.<BakedModel>emptyList();
    }
    
    public ItemOverrides(final ModelBakery enl, final BlockModel ecy, final Function<ResourceLocation, UnbakedModel> function, final List<ItemOverride> list) {
        this.overrides = Lists.newArrayList();
        final UnbakedModel enr5;
        Collections.reverse(this.overrideModels = list.stream().map(edc -> {
            enr5 = function.apply(edc.getModel());
            if (Objects.equals(enr5, ecy)) {
                return null;
            }
            else {
                return enl.bake(edc.getModel(), BlockModelRotation.X0_Y0);
            }
        }).collect(Collectors.toList()));
        for (int integer6 = list.size() - 1; integer6 >= 0; --integer6) {
            this.overrides.add(list.get(integer6));
        }
    }
    
    @Nullable
    public BakedModel resolve(final BakedModel enh, final ItemStack bki, @Nullable final ClientLevel dym, @Nullable final LivingEntity aoy) {
        if (!this.overrides.isEmpty()) {
            int integer6 = 0;
            while (integer6 < this.overrides.size()) {
                final ItemOverride edc7 = this.overrides.get(integer6);
                if (edc7.test(bki, dym, aoy)) {
                    final BakedModel enh2 = this.overrideModels.get(integer6);
                    if (enh2 == null) {
                        return enh;
                    }
                    return enh2;
                }
                else {
                    ++integer6;
                }
            }
        }
        return enh;
    }
    
    static {
        EMPTY = new ItemOverrides();
    }
}
