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 java.util.Optional;
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.datafixers.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 lf3;
        return dynamic.update("pages", dynamic2 -> (Dynamic)DataFixUtils.orElse((Optional)dynamic2.asStreamOpt().map(stream -> stream.map(dynamic -> {
            if (!dynamic.asString().isPresent()) {
                return dynamic;
            }
            else {
                string2 = dynamic.asString("");
                lf3 = null;
                if ("null".equals(string2) || StringUtils.isEmpty((CharSequence)string2)) {
                    lf3 = new TextComponent("");
                }
                else {
                    if (string2.charAt(0) != 34 || string2.charAt(string2.length() - 1) != 34) {
                        if (string2.charAt(0) != 123 || string2.charAt(string2.length() - 1) != 125) {
                            lf3 = new TextComponent(string2);
                            return dynamic.createString(Component.Serializer.toJson(lf3));
                        }
                    }
                    try {
                        lf3 = GsonHelper.<Component>fromJson(BlockEntitySignTextStrictJsonFix.GSON, string2, Component.class, (boolean)(1 != 0));
                        if (lf3 == null) {
                            lf3 = new TextComponent("");
                        }
                    }
                    catch (JsonParseException ex) {}
                    if (lf3 == null) {
                        try {
                            lf3 = Component.Serializer.fromJson(string2);
                        }
                        catch (JsonParseException ex2) {}
                    }
                    if (lf3 == null) {
                        try {
                            lf3 = Component.Serializer.fromJsonLenient(string2);
                        }
                        catch (JsonParseException ex3) {}
                    }
                    if (lf3 == null) {
                        lf3 = new TextComponent(string2);
                    }
                }
                return dynamic.createString(Component.Serializer.toJson(lf3));
            }
        })).map(dynamic::createList), 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)));
    }
}
