package mapped;

import net.runelite.mapping.ObfuscatedName;
import net.runelite.mapping.ObfuscatedSignature;
import net.runelite.rs.api.RSDemotingHashTable;

@ObfuscatedName("ls")
public final class DemotingHashTable implements RSDemotingHashTable {
   @ObfuscatedName("at")
   int capacity;
   @ObfuscatedName("ah")
   int remaining;
   @ObfuscatedName("ar")
   @ObfuscatedSignature(
      descriptor = "Ltz;"
   )
   IterableNodeHashTable hashTable;
   @ObfuscatedName("ao")
   @ObfuscatedSignature(
      descriptor = "Lpo;"
   )
   IterableDualNodeQueue queue;
   @ObfuscatedName("ab")
   @ObfuscatedSignature(
      descriptor = "Llr;"
   )
   class295 field3103;

   public DemotingHashTable(int var1, int var2) {
      this.queue = new IterableDualNodeQueue();
      this.capacity = var1;
      this.remaining = var1;

      int var3;
      for(var3 = 1; var3 + var3 < var1 && var3 < var2; var3 += var3) {
         ;
      }

      this.hashTable = new IterableNodeHashTable(var3);
   }

   public DemotingHashTable(int var1) {
      this(var1, var1);
   }

   @ObfuscatedName("at")
   public Object get_base(long var1) {
      Wrapper var3 = (Wrapper)this.hashTable.get(var1);
      if(var3 == null) {
         return null;
      } else {
         Object var4 = var3.get_base();
         if(var4 == null) {
            var3.remove_base();
            var3.removeDual_base();
            this.remaining += var3.size;
            return null;
         } else {
            if(var3.isSoft_base()) {
               DirectWrapper var5 = new DirectWrapper(var4, var3.size);
               this.hashTable.put(var5, var3.key);
               this.queue.method7382(var5);
               var5.keyDual = 0L;
               var3.remove_base();
               var3.removeDual_base();
            } else {
               this.queue.method7382(var3);
               var3.keyDual = 0L;
            }

            return var4;
         }
      }
   }

   @ObfuscatedName("ah")
   void remove_base(long var1) {
      Wrapper var3 = (Wrapper)this.hashTable.get(var1);
      this.method5840(var3);
   }

   @ObfuscatedName("ar")
   @ObfuscatedSignature(
      descriptor = "(Lla;)V"
   )
   void method5840(Wrapper var1) {
      if(var1 != null) {
         var1.remove_base();
         var1.removeDual_base();
         this.remaining += var1.size;
      }

   }

   @ObfuscatedName("ao")
   public void method5861_base(Object var1, long var2) {
      this.put_base(var1, var2, 1);
   }

   @ObfuscatedName("ab")
   public void put_base(Object var1, long var2, int var4) {
      if(var4 > this.capacity) {
         throw new IllegalStateException();
      } else {
         this.remove_base(var2);
         this.remaining -= var4;

         while(this.remaining < 0) {
            Wrapper var5 = (Wrapper)this.queue.method7384();
            if(var5 == null) {
               throw new RuntimeException("");
            }

            if(!var5.isSoft_base()) {
               ;
            }

            this.method5840(var5);
            if(this.field3103 != null) {
               this.field3103.method5835_base(var5.get_base());
            }
         }

         DirectWrapper var6 = new DirectWrapper(var1, var4);
         this.hashTable.put(var6, var2);
         this.queue.method7382(var6);
         var6.keyDual = 0L;
      }
   }

   @ObfuscatedName("au")
   public void demote_base(int var1) {
      for(Wrapper var2 = (Wrapper)this.queue.method7385(); var2 != null; var2 = (Wrapper)this.queue.method7387()) {
         if(var2.isSoft_base()) {
            if(var2.get_base() == null) {
               var2.remove_base();
               var2.removeDual_base();
               this.remaining += var2.size;
            }
         } else if(++var2.keyDual > (long)var1) {
            SoftWrapper var3 = new SoftWrapper(var2.get_base(), var2.size);
            this.hashTable.put(var3, var2.key);
            IterableDualNodeQueue.method7383(var3, var2);
            var2.remove_base();
            var2.removeDual_base();
         }
      }

   }

   @ObfuscatedName("aa")
   public void clear_base() {
      this.queue.clear_base();
      this.hashTable.clear_base();
      this.remaining = this.capacity;
   }
}
