package net.minecraft.util.datafix.fixes;

import com.mojang.datafixers.DataFixUtils;
import com.mojang.datafixers.OpticFinder;
import com.mojang.datafixers.types.Type;
import java.util.Optional;
import com.mojang.datafixers.Typed;
import com.mojang.datafixers.Dynamic;
import java.util.Objects;
import com.mojang.datafixers.util.Pair;
import com.mojang.datafixers.DSL;
import com.mojang.datafixers.TypeRewriteRule;
import com.mojang.datafixers.schemas.Schema;
import com.mojang.datafixers.DataFix;

public class ItemSpawnEggFix extends DataFix {
    private static final String[] ID_TO_ENTITY;
    
    public ItemSpawnEggFix(final Schema schema, final boolean boolean2) {
        super(schema, boolean2);
    }
    
    public TypeRewriteRule makeRule() {
        final Schema schema2 = this.getInputSchema();
        final Type<?> type3 = schema2.getType(References.ITEM_STACK);
        final OpticFinder<Pair<String, String>> opticFinder4 = (OpticFinder<Pair<String, String>>)DSL.fieldFinder("id", DSL.named(References.ITEM_NAME.typeName(), DSL.namespacedString()));
        final OpticFinder<String> opticFinder5 = (OpticFinder<String>)DSL.fieldFinder("id", DSL.string());
        final OpticFinder<?> opticFinder6 = type3.findField("tag");
        final OpticFinder<?> opticFinder7 = opticFinder6.type().findField("EntityTag");
        final OpticFinder<?> opticFinder8 = DSL.typeFinder(schema2.getTypeRaw(References.ENTITY));
        final OpticFinder opticFinder9;
        final Optional<Pair<String, String>> optional8;
        Dynamic<?> dynamic9;
        short short10;
        final OpticFinder opticFinder10;
        Optional<? extends Typed<?>> optional9;
        final OpticFinder opticFinder11;
        Optional<? extends Typed<?>> optional10;
        final OpticFinder opticFinder12;
        Optional<? extends Typed<?>> optional11;
        final OpticFinder opticFinder13;
        Optional<String> optional12;
        Typed<?> typed2;
        String string16;
        Typed<?> typed3;
        Typed<?> typed4;
        Typed<?> typed5;
        Dynamic<?> dynamic10;
        Typed<?> typed6;
        Dynamic<?> dynamic11;
        return this.fixTypeEverywhereTyped("ItemSpawnEggFix", (Type)type3, typed -> {
            optional8 = (Optional<Pair<String, String>>)typed.getOptional(opticFinder9);
            if (optional8.isPresent() && Objects.equals(optional8.get().getSecond(), "minecraft:spawn_egg")) {
                dynamic9 = typed.get(DSL.remainderFinder());
                short10 = dynamic9.get("Damage").asShort((short)0);
                optional9 = typed.getOptionalTyped(opticFinder10);
                optional10 = optional9.flatMap(typed -> typed.getOptionalTyped(opticFinder11));
                optional11 = optional10.flatMap(typed -> typed.getOptionalTyped(opticFinder12));
                optional12 = optional11.<String>flatMap(typed -> typed.getOptional(opticFinder13));
                typed2 = typed;
                string16 = ItemSpawnEggFix.ID_TO_ENTITY[short10 & 0xFF];
                if (string16 != null && (!optional12.isPresent() || !Objects.equals(optional12.get(), string16))) {
                    typed3 = typed.getOrCreateTyped(opticFinder10);
                    typed4 = typed3.getOrCreateTyped(opticFinder11);
                    typed5 = typed4.getOrCreateTyped(opticFinder12);
                    dynamic10 = typed5.write().set("id", dynamic9.createString(string16));
                    typed6 = ((Optional)this.getOutputSchema().getTypeRaw(References.ENTITY).readTyped((Dynamic)dynamic10).getSecond()).<Throwable>orElseThrow(() -> new IllegalStateException("Could not parse new entity"));
                    typed2 = typed2.set(opticFinder10, typed3.set(opticFinder11, typed4.set(opticFinder12, (Typed)typed6)));
                }
                if (short10 != 0) {
                    dynamic11 = dynamic9.set("Damage", dynamic9.createShort((short)0));
                    typed2 = typed2.set(DSL.remainderFinder(), dynamic11);
                }
                return typed2;
            }
            else {
                return typed;
            }
        });
    }
    
    static {
        ID_TO_ENTITY = (String[])DataFixUtils.make(new String[256], arr -> {
            arr[1] = "Item";
            arr[2] = "XPOrb";
            arr[7] = "ThrownEgg";
            arr[8] = "LeashKnot";
            arr[9] = "Painting";
            arr[10] = "Arrow";
            arr[11] = "Snowball";
            arr[12] = "Fireball";
            arr[13] = "SmallFireball";
            arr[14] = "ThrownEnderpearl";
            arr[15] = "EyeOfEnderSignal";
            arr[16] = "ThrownPotion";
            arr[17] = "ThrownExpBottle";
            arr[18] = "ItemFrame";
            arr[19] = "WitherSkull";
            arr[20] = "PrimedTnt";
            arr[21] = "FallingSand";
            arr[22] = "FireworksRocketEntity";
            arr[23] = "TippedArrow";
            arr[24] = "SpectralArrow";
            arr[25] = "ShulkerBullet";
            arr[26] = "DragonFireball";
            arr[30] = "ArmorStand";
            arr[41] = "Boat";
            arr[42] = "MinecartRideable";
            arr[43] = "MinecartChest";
            arr[44] = "MinecartFurnace";
            arr[45] = "MinecartTNT";
            arr[46] = "MinecartHopper";
            arr[47] = "MinecartSpawner";
            arr[40] = "MinecartCommandBlock";
            arr[48] = "Mob";
            arr[49] = "Monster";
            arr[50] = "Creeper";
            arr[51] = "Skeleton";
            arr[52] = "Spider";
            arr[53] = "Giant";
            arr[54] = "Zombie";
            arr[55] = "Slime";
            arr[56] = "Ghast";
            arr[57] = "PigZombie";
            arr[58] = "Enderman";
            arr[59] = "CaveSpider";
            arr[60] = "Silverfish";
            arr[61] = "Blaze";
            arr[62] = "LavaSlime";
            arr[63] = "EnderDragon";
            arr[64] = "WitherBoss";
            arr[65] = "Bat";
            arr[66] = "Witch";
            arr[67] = "Endermite";
            arr[68] = "Guardian";
            arr[69] = "Shulker";
            arr[90] = "Pig";
            arr[91] = "Sheep";
            arr[92] = "Cow";
            arr[93] = "Chicken";
            arr[94] = "Squid";
            arr[95] = "Wolf";
            arr[96] = "MushroomCow";
            arr[97] = "SnowMan";
            arr[98] = "Ozelot";
            arr[99] = "VillagerGolem";
            arr[100] = "EntityHorse";
            arr[101] = "Rabbit";
            arr[120] = "Villager";
            arr[200] = "EnderCrystal";
        });
    }
}
