package net.minecraft.client.renderer.block;

import net.minecraft.world.level.block.state.properties.Property;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.client.resources.model.ModelResourceLocation;
import java.util.Iterator;
import net.minecraft.world.level.block.Block;
import net.minecraft.core.Registry;
import net.minecraft.client.renderer.texture.TextureAtlasSprite;
import com.google.common.collect.Maps;
import net.minecraft.client.resources.model.ModelManager;
import net.minecraft.client.resources.model.BakedModel;
import net.minecraft.world.level.block.state.BlockState;
import java.util.Map;

public class BlockModelShaper {
    private final Map<BlockState, BakedModel> modelByStateCache;
    private final ModelManager modelManager;
    
    public BlockModelShaper(final ModelManager eca) {
        this.modelByStateCache = Maps.newIdentityHashMap();
        this.modelManager = eca;
    }
    
    public TextureAtlasSprite getParticleIcon(final BlockState byj) {
        return this.getBlockModel(byj).getParticleIcon();
    }
    
    public BakedModel getBlockModel(final BlockState byj) {
        BakedModel ebv3 = this.modelByStateCache.get(byj);
        if (ebv3 == null) {
            ebv3 = this.modelManager.getMissingModel();
        }
        return ebv3;
    }
    
    public ModelManager getModelManager() {
        return this.modelManager;
    }
    
    public void rebuildCache() {
        this.modelByStateCache.clear();
        for (final Block bph3 : Registry.BLOCK) {
            final BakedModel bakedModel;
            bph3.getStateDefinition().getPossibleStates().forEach(byj -> bakedModel = this.modelByStateCache.put(byj, this.modelManager.getModel(stateToModelLocation(byj))));
        }
    }
    
    public static ModelResourceLocation stateToModelLocation(final BlockState byj) {
        return stateToModelLocation(Registry.BLOCK.getKey(byj.getBlock()), byj);
    }
    
    public static ModelResourceLocation stateToModelLocation(final ResourceLocation sm, final BlockState byj) {
        return new ModelResourceLocation(sm, statePropertiesToString(byj.getValues()));
    }
    
    public static String statePropertiesToString(final Map<Property<?>, Comparable<?>> map) {
        final StringBuilder stringBuilder2 = new StringBuilder();
        for (final Map.Entry<Property<?>, Comparable<?>> entry4 : map.entrySet()) {
            if (stringBuilder2.length() != 0) {
                stringBuilder2.append(',');
            }
            final Property<?> bzm5 = entry4.getKey();
            stringBuilder2.append(bzm5.getName());
            stringBuilder2.append('=');
            stringBuilder2.append(BlockModelShaper.getValue(bzm5, entry4.getValue()));
        }
        return stringBuilder2.toString();
    }
    
    private static <T extends Comparable<T>> String getValue(final Property<T> bzm, final Comparable<?> comparable) {
        return bzm.getName((T)comparable);
    }
}
