package net.minecraft.util.datafix.fixes;

import java.util.stream.Stream;
import com.mojang.datafixers.Typed;
import com.mojang.datafixers.types.Type;
import com.mojang.datafixers.OpticFinder;
import com.mojang.datafixers.DSL;
import com.mojang.datafixers.TypeRewriteRule;
import com.mojang.datafixers.DataFixUtils;
import java.util.function.Function;
import com.google.gson.JsonParseException;
import net.minecraft.util.GsonHelper;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.TextComponent;
import org.apache.commons.lang3.StringUtils;
import com.mojang.serialization.Dynamic;
import com.mojang.datafixers.schemas.Schema;
import com.mojang.datafixers.DataFix;

public class ItemWrittenBookPagesStrictJsonFix extends DataFix {
    public ItemWrittenBookPagesStrictJsonFix(final Schema schema, final boolean boolean2) {
        super(schema, boolean2);
    }
    
    public Dynamic<?> fixTag(final Dynamic<?> dynamic) {
        String string2;
        Component mr3;
        return dynamic.update("pages", dynamic2 -> (Dynamic)DataFixUtils.orElse(dynamic2.asStreamOpt().map(stream -> stream.map(dynamic -> {
            if (!dynamic.asString().result().isPresent()) {
                return dynamic;
            }
            else {
                string2 = dynamic.asString("");
                mr3 = null;
                if ("null".equals(string2) || StringUtils.isEmpty((CharSequence)string2)) {
                    mr3 = TextComponent.EMPTY;
                }
                else {
                    if (string2.charAt(0) != 34 || string2.charAt(string2.length() - 1) != 34) {
                        if (string2.charAt(0) != 123 || string2.charAt(string2.length() - 1) != 125) {
                            mr3 = new TextComponent(string2);
                            return dynamic.createString(Component.Serializer.toJson(mr3));
                        }
                    }
                    try {
                        mr3 = GsonHelper.<Component>fromJson(BlockEntitySignTextStrictJsonFix.GSON, string2, Component.class, (boolean)(1 != 0));
                        if (mr3 == null) {
                            mr3 = TextComponent.EMPTY;
                        }
                    }
                    catch (JsonParseException ex) {}
                    if (mr3 == null) {
                        try {
                            mr3 = Component.Serializer.fromJson(string2);
                        }
                        catch (JsonParseException ex2) {}
                    }
                    if (mr3 == null) {
                        try {
                            mr3 = Component.Serializer.fromJsonLenient(string2);
                        }
                        catch (JsonParseException ex3) {}
                    }
                    if (mr3 == null) {
                        mr3 = new TextComponent(string2);
                    }
                }
                return dynamic.createString(Component.Serializer.toJson(mr3));
            }
        })).map((Function)dynamic::createList).result(), dynamic.emptyList()));
    }
    
    public TypeRewriteRule makeRule() {
        final Type<?> type2 = this.getInputSchema().getType(References.ITEM_STACK);
        final OpticFinder<?> opticFinder3 = type2.findField("tag");
        return this.fixTypeEverywhereTyped("ItemWrittenBookPagesStrictJsonFix", (Type)type2, typed -> typed.updateTyped((OpticFinder)opticFinder3, typed -> typed.update(DSL.remainderFinder(), (Function)this::fixTag)));
    }
}
