package net.minecraft.world.level.storage;

import net.minecraft.nbt.Tag;
import java.util.Iterator;
import net.minecraft.world.level.saveddata.SavedData;
import java.util.stream.Stream;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.resources.ResourceLocation;
import com.google.common.collect.Maps;
import java.util.Map;

public class CommandStorage {
    private final Map<String, Container> namespaces;
    private final DimensionDataStorage storage;
    
    public CommandStorage(final DimensionDataStorage daa) {
        this.namespaces = Maps.newHashMap();
        this.storage = daa;
    }
    
    private Container newStorage(final String string1, final String string2) {
        final Container a4 = new Container(string2);
        this.namespaces.put(string1, a4);
        return a4;
    }
    
    public CompoundTag get(final ResourceLocation uh) {
        final String string3 = uh.getNamespace();
        final String string4 = createId(string3);
        final Container a5 = this.storage.<Container>get(() -> this.newStorage(string3, string4), string4);
        return (a5 != null) ? a5.get(uh.getPath()) : new CompoundTag();
    }
    
    public void set(final ResourceLocation uh, final CompoundTag le) {
        final String string4 = uh.getNamespace();
        final String string5 = createId(string4);
        this.storage.<Container>computeIfAbsent(() -> this.newStorage(string4, string5), string5).put(uh.getPath(), le);
    }
    
    public Stream<ResourceLocation> keys() {
        return this.namespaces.entrySet().stream().<ResourceLocation>flatMap(entry -> entry.getValue().getKeys((String)entry.getKey()));
    }
    
    private static String createId(final String string) {
        return "command_storage_" + string;
    }
    
    static class Container extends SavedData {
        private final Map<String, CompoundTag> storage;
        
        public Container(final String string) {
            super(string);
            this.storage = Maps.newHashMap();
        }
        
        @Override
        public void load(final CompoundTag le) {
            final CompoundTag le2 = le.getCompound("contents");
            for (final String string5 : le2.getAllKeys()) {
                this.storage.put(string5, le2.getCompound(string5));
            }
        }
        
        @Override
        public CompoundTag save(final CompoundTag le) {
            final CompoundTag le4 = new CompoundTag();
            this.storage.forEach((string, le3) -> le4.put(string, le3.copy()));
            le.put("contents", le4);
            return le;
        }
        
        public CompoundTag get(final String string) {
            final CompoundTag le3 = this.storage.get(string);
            return (le3 != null) ? le3 : new CompoundTag();
        }
        
        public void put(final String string, final CompoundTag le) {
            if (le.isEmpty()) {
                this.storage.remove(string);
            }
            else {
                this.storage.put(string, le);
            }
            this.setDirty();
        }
        
        public Stream<ResourceLocation> getKeys(final String string) {
            return this.storage.keySet().stream().<ResourceLocation>map(string2 -> new ResourceLocation(string, string2));
        }
    }
}
