package net.minecraft.util.datafix.fixes;

import com.mojang.datafixers.Typed;
import java.util.Optional;
import com.mojang.datafixers.OpticFinder;
import com.mojang.datafixers.types.Type;
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 ItemStackMapIdFix extends DataFix {
    public ItemStackMapIdFix(final Schema schema, final boolean boolean2) {
        super(schema, boolean2);
    }
    
    public TypeRewriteRule makeRule() {
        final Type<?> type2 = this.getInputSchema().getType(References.ITEM_STACK);
        final OpticFinder<Pair<String, String>> opticFinder3 = (OpticFinder<Pair<String, String>>)DSL.fieldFinder("id", DSL.named(References.ITEM_NAME.typeName(), DSL.namespacedString()));
        final OpticFinder<?> opticFinder4 = type2.findField("tag");
        final OpticFinder opticFinder5;
        final Optional<Pair<String, String>> optional4;
        Dynamic<?> dynamic5;
        final OpticFinder opticFinder6;
        Typed<?> typed2;
        Dynamic<?> dynamic6;
        Dynamic<?> dynamic7;
        return this.fixTypeEverywhereTyped("ItemInstanceMapIdFix", (Type)type2, typed -> {
            optional4 = (Optional<Pair<String, String>>)typed.getOptional(opticFinder5);
            if (optional4.isPresent() && Objects.equals(optional4.get().getSecond(), "minecraft:filled_map")) {
                dynamic5 = typed.get(DSL.remainderFinder());
                typed2 = typed.getOrCreateTyped(opticFinder6);
                dynamic6 = typed2.get(DSL.remainderFinder());
                dynamic7 = dynamic6.set("map", dynamic6.createInt(dynamic5.get("Damage").asInt(0)));
                return typed.set(opticFinder6, typed2.set(DSL.remainderFinder(), dynamic7));
            }
            else {
                return typed;
            }
        });
    }
}
