package net.minecraft.util.datafix.fixes;

import java.util.function.Function;
import com.mojang.serialization.DynamicOps;
import com.mojang.datafixers.types.templates.TaggedChoice;
import com.mojang.datafixers.util.Pair;
import com.mojang.datafixers.Typed;
import com.mojang.datafixers.types.Type;
import com.mojang.datafixers.TypeRewriteRule;
import com.mojang.datafixers.schemas.Schema;
import com.mojang.datafixers.DataFix;

public abstract class EntityRenameFix extends DataFix {
    protected final String name;
    
    public EntityRenameFix(final String string, final Schema schema, final boolean boolean3) {
        super(schema, boolean3);
        this.name = string;
    }
    
    public TypeRewriteRule makeRule() {
        final TaggedChoice.TaggedChoiceType<String> taggedChoiceType2 = (TaggedChoice.TaggedChoiceType<String>)this.getInputSchema().findChoiceType(References.ENTITY);
        final TaggedChoice.TaggedChoiceType<String> taggedChoiceType3 = (TaggedChoice.TaggedChoiceType<String>)this.getOutputSchema().findChoiceType(References.ENTITY);
        final String string6;
        final TaggedChoice.TaggedChoiceType taggedChoiceType4;
        final Type<?> type7;
        final Pair<String, Typed<?>> pair2;
        final TaggedChoice.TaggedChoiceType taggedChoiceType5;
        final Type<?> type8;
        final IllegalStateException ex;
        return this.fixTypeEverywhere(this.name, (Type)taggedChoiceType2, (Type)taggedChoiceType3, dynamicOps -> pair -> {
            string6 = (String)pair.getFirst();
            type7 = taggedChoiceType4.types().get(string6);
            pair2 = this.fix(string6, this.getEntity(pair.getSecond(), dynamicOps, type7));
            type8 = taggedChoiceType5.types().get(pair2.getFirst());
            if (!type8.equals(((Typed)pair2.getSecond()).getType(), true, true)) {
                new IllegalStateException(String.format("Dynamic type check failed: %s not equal to %s", type8, ((Typed)pair2.getSecond()).getType()));
                throw ex;
            }
            else {
                return Pair.of(pair2.getFirst(), ((Typed)pair2.getSecond()).getValue());
            }
        });
    }
    
    private <A> Typed<A> getEntity(final Object object, final DynamicOps<?> dynamicOps, final Type<A> type) {
        return (Typed<A>)new Typed((Type)type, (DynamicOps)dynamicOps, object);
    }
    
    protected abstract Pair<String, Typed<?>> fix(final String string, final Typed<?> typed);
}
