package net.minecraft.util.datafix.fixes;

import com.mojang.datafixers.Typed;
import java.util.function.Predicate;
import com.mojang.serialization.Dynamic;
import java.util.function.Function;
import com.mojang.datafixers.DSL;
import com.mojang.datafixers.TypeRewriteRule;
import com.mojang.datafixers.schemas.Schema;
import com.mojang.datafixers.DataFix;

public class RedstoneWireConnectionsFix extends DataFix {
    public RedstoneWireConnectionsFix(final Schema schema) {
        super(schema, false);
    }
    
    protected TypeRewriteRule makeRule() {
        final Schema schema2 = this.getInputSchema();
        return this.fixTypeEverywhereTyped("RedstoneConnectionsFix", schema2.getType(References.BLOCK_STATE), typed -> typed.update(DSL.remainderFinder(), (Function)this::updateRedstoneConnections));
    }
    
    private <T> Dynamic<T> updateRedstoneConnections(final Dynamic<T> dynamic) {
        final boolean boolean3 = dynamic.get("Name").asString().result().filter("minecraft:redstone_wire"::equals).isPresent();
        if (!boolean3) {
            return dynamic;
        }
        final String string2;
        final String string3;
        final String string4;
        final String string5;
        final boolean boolean4;
        final boolean boolean5;
        final String string6;
        final String string7;
        final String string8;
        final String string9;
        return (Dynamic<T>)dynamic.update("Properties", dynamic -> {
            string2 = dynamic.get("east").asString("none");
            string3 = dynamic.get("west").asString("none");
            string4 = dynamic.get("north").asString("none");
            string5 = dynamic.get("south").asString("none");
            boolean4 = (isConnected(string2) || isConnected(string3));
            boolean5 = (isConnected(string4) || isConnected(string5));
            string6 = ((!isConnected(string2) && !boolean5) ? "side" : string2);
            string7 = ((!isConnected(string3) && !boolean5) ? "side" : string3);
            string8 = ((!isConnected(string4) && !boolean4) ? "side" : string4);
            string9 = ((!isConnected(string5) && !boolean4) ? "side" : string5);
            return dynamic.update("east", dynamic -> dynamic.createString(string6)).update("west", dynamic -> dynamic.createString(string7)).update("north", dynamic -> dynamic.createString(string8)).update("south", dynamic -> dynamic.createString(string9));
        });
    }
    
    private static boolean isConnected(final String string) {
        return !"none".equals(string);
    }
}
