package net.minecraft.util.datafix.fixes;

import com.mojang.datafixers.Typed;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.TextComponent;
import java.util.stream.Stream;
import com.mojang.datafixers.types.Type;
import com.mojang.datafixers.OpticFinder;
import com.mojang.datafixers.DataFixUtils;
import java.util.function.Function;
import com.mojang.serialization.Dynamic;
import com.mojang.datafixers.DSL;
import com.mojang.datafixers.TypeRewriteRule;
import com.mojang.datafixers.schemas.Schema;
import com.mojang.datafixers.DataFix;

public class ItemLoreFix extends DataFix {
    public ItemLoreFix(final Schema schema, final boolean boolean2) {
        super(schema, boolean2);
    }
    
    protected TypeRewriteRule makeRule() {
        final Type<?> type2 = this.getInputSchema().getType(References.ITEM_STACK);
        final OpticFinder<?> opticFinder3 = type2.findField("tag");
        return this.fixTypeEverywhereTyped("Item Lore componentize", (Type)type2, typed -> typed.updateTyped((OpticFinder)opticFinder3, typed -> typed.update(DSL.remainderFinder(), dynamic -> dynamic.update("display", dynamic -> dynamic.update("Lore", dynamic -> (Dynamic)DataFixUtils.orElse(((Dynamic)dynamic).asStreamOpt().map((Function)ItemLoreFix::fixLoreList).map((Function)dynamic::createList).result(), dynamic))))));
    }
    
    private static <T> Stream<Dynamic<T>> fixLoreList(final Stream<Dynamic<T>> stream) {
        return stream.<Dynamic<T>>map(dynamic -> (Dynamic)DataFixUtils.orElse(((Dynamic)dynamic).asString().map((Function)ItemLoreFix::fixLoreEntry).map((Function)dynamic::createString).result(), dynamic));
    }
    
    private static String fixLoreEntry(final String string) {
        return Component.Serializer.toJson(new TextComponent(string));
    }
}
