package net.minecraft.util.datafix.fixes;

import com.mojang.datafixers.types.DynamicOps;
import java.util.stream.Stream;
import java.util.Optional;
import com.mojang.datafixers.DataFixUtils;
import com.mojang.datafixers.Dynamic;
import com.mojang.datafixers.util.Pair;
import com.mojang.datafixers.types.Type;
import java.util.function.Function;
import java.util.Objects;
import com.mojang.datafixers.DSL;
import com.mojang.datafixers.TypeRewriteRule;
import com.mojang.datafixers.schemas.Schema;
import com.mojang.datafixers.DataFix;

public abstract class PoiTypeRename extends DataFix {
    public PoiTypeRename(final Schema schema, final boolean boolean2) {
        super(schema, boolean2);
    }
    
    protected TypeRewriteRule makeRule() {
        final Type<Pair<String, Dynamic<?>>> type2 = (Type<Pair<String, Dynamic<?>>>)DSL.named(References.POI_CHUNK.typeName(), DSL.remainderType());
        if (!Objects.equals(type2, this.getInputSchema().getType(References.POI_CHUNK))) {
            throw new IllegalStateException("Poi type is not what was expected.");
        }
        return this.fixTypeEverywhere("POI rename", (Type)type2, dynamicOps -> pair -> pair.mapSecond((Function)this::cap));
    }
    
    private <T> Dynamic<T> cap(final Dynamic<T> dynamic) {
        return (Dynamic<T>)dynamic.update("Sections", dynamic -> dynamic.updateMapValues(pair -> pair.mapSecond(dynamic -> dynamic.update("Records", dynamic -> (Dynamic)DataFixUtils.orElse((Optional)this.renameRecords(dynamic), dynamic)))));
    }
    
    private <T> Optional<Dynamic<T>> renameRecords(final Dynamic<T> dynamic) {
        return dynamic.asStreamOpt().<Dynamic<T>>map(stream -> dynamic.createList((Stream)stream.map(dynamic -> dynamic.update("type", dynamic -> (Dynamic)DataFixUtils.orElse((Optional)((Dynamic)dynamic).asString().map(this::rename).map(dynamic::createString), dynamic)))));
    }
    
    protected abstract String rename(final String string);
}
