// 
// Decompiled by Procyon v0.6.0
// 

package com.hypixel.hytale.assetstore.map;

import it.unimi.dsi.fastutil.ints.IntIterator;
import com.hypixel.hytale.assetstore.AssetExtraInfo;
import javax.annotation.Nonnull;
import java.util.Map;
import com.hypixel.hytale.assetstore.codec.AssetCodec;
import it.unimi.dsi.fastutil.ints.IntSets;
import it.unimi.dsi.fastutil.ints.IntSet;
import com.hypixel.fastutil.ints.Int2ObjectConcurrentHashMap;
import com.hypixel.hytale.assetstore.JsonAsset;

public abstract class AssetMapWithIndexes<K, T extends JsonAsset<K>> extends DefaultAssetMap<K, T>
{
    public static final int NOT_FOUND = Integer.MIN_VALUE;
    protected final Int2ObjectConcurrentHashMap<IntSet> indexedTagStorage;
    protected final Int2ObjectConcurrentHashMap<IntSet> unmodifiableIndexedTagStorage;
    
    public AssetMapWithIndexes() {
        this.indexedTagStorage = new Int2ObjectConcurrentHashMap<IntSet>();
        this.unmodifiableIndexedTagStorage = new Int2ObjectConcurrentHashMap<IntSet>();
    }
    
    @Override
    protected void clear() {
        super.clear();
        this.indexedTagStorage.clear();
        this.unmodifiableIndexedTagStorage.clear();
    }
    
    public IntSet getIndexesForTag(final int index) {
        return this.unmodifiableIndexedTagStorage.getOrDefault(index, IntSets.EMPTY_SET);
    }
    
    @Override
    protected void putAssetTags(final AssetCodec<K, T> codec, final Map<K, T> loadedAssets) {
    }
    
    protected void putAssetTag(@Nonnull final AssetCodec<K, T> codec, final K key, final int index, final T value) {
        final AssetExtraInfo.Data data = codec.getData(value);
        if (data == null) {
            return;
        }
        final IntIterator iterator = data.getExpandedTagIndexes().iterator();
        while (iterator.hasNext()) {
            final int tag = iterator.nextInt();
            this.putAssetTag(key, index, tag);
        }
    }
    
    protected void putAssetTag(final K key, final int index, final int tag) {
        this.putAssetTag(key, tag);
        this.indexedTagStorage.computeIfAbsent(tag, k -> {
            final IntSet set = Int2ObjectConcurrentHashMap.newKeySet(3);
            this.unmodifiableIndexedTagStorage.put(k, IntSets.unmodifiable(set));
            return set;
        }).add(index);
    }
    
    @Override
    public boolean requireReplaceOnRemove() {
        return true;
    }
}
