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

import net.minecraft.world.entity.LivingEntity;
import javax.annotation.Nullable;
import net.minecraft.world.level.Level;
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 ebz, final BlockModel drs, final Function<ResourceLocation, UnbakedModel> function, final List<ItemOverride> list) {
        this.overrides = Lists.newArrayList();
        final UnbakedModel ecf5;
        Collections.reverse(this.overrideModels = list.stream().map(drw -> {
            ecf5 = function.apply(drw.getModel());
            if (Objects.equals(ecf5, drs)) {
                return null;
            }
            else {
                return ebz.bake(drw.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 ebv, final ItemStack ben, @Nullable final Level bjw, @Nullable final LivingEntity akz) {
        if (!this.overrides.isEmpty()) {
            int integer6 = 0;
            while (integer6 < this.overrides.size()) {
                final ItemOverride drw7 = this.overrides.get(integer6);
                if (drw7.test(ben, bjw, akz)) {
                    final BakedModel ebv2 = this.overrideModels.get(integer6);
                    if (ebv2 == null) {
                        return ebv;
                    }
                    return ebv2;
                }
                else {
                    ++integer6;
                }
            }
        }
        return ebv;
    }
    
    static {
        EMPTY = new ItemOverrides();
    }
}
