import * as tsplus_module_1 from "@tsplus/stdlib/collections/List/definition";
import * as tsplus_module_2 from "@tsplus/stdlib/collections/Chunk/unsafeGet";
import * as tsplus_module_3 from "@effect/core/stm/TRef/operations/unsafeGet";
import * as tsplus_module_4 from "@effect/core/stm/TMap/operations/indexOf";
import * as tsplus_module_5 from "@tsplus/stdlib/data/Tuple/make";
import * as tsplus_module_6 from "@effect/core/stm/TRef/operations/unsafeMake";
import * as tsplus_module_7 from "@tsplus/stdlib/collections/Chunk/definition";
import * as tsplus_module_8 from "@effect/core/stm/TRef/operations/unsafeSet";
import * as tsplus_module_9 from "@tsplus/stdlib/structure/Equals";
import * as tsplus_module_10 from "@tsplus/stdlib/collections/List/exists";
import * as tsplus_module_11 from "@tsplus/stdlib/collections/List/map";
import * as tsplus_module_12 from "@effect/core/stm/STM/operations/effect";
import { concreteTArray, InternalTArray } from "@effect/core/stm/TArray/operations/_internal/InternalTArray";
import { concreteTMap } from "@effect/core/stm/TMap/operations/_internal/InternalTMap";
const LOAD_FACTOR = .75;

function resize(self, journal, buckets, k, v) {
  void 0;
  const capacity = buckets.chunk.length;
  const newCapacity = capacity << 1;
  const newBuckets = Array.from({
    length: newCapacity
  }, () => tsplus_module_1.nil());
  let i = 0;

  while (i < capacity) {
    const pairs = tsplus_module_3.unsafeGet_(tsplus_module_2.unsafeGet_(buckets.chunk, i), journal);

    for (const pair of pairs) {
      const idx = tsplus_module_4.indexOf(pair.get(0), newCapacity);
      newBuckets[idx] = tsplus_module_1.cons(pair, newBuckets[idx]);
    }

    i += 1;
  }

  const newIdx = tsplus_module_4.indexOf(k, newCapacity);
  newBuckets[newIdx] = tsplus_module_1.cons(tsplus_module_5.make(k, v), newBuckets[newIdx]);
  const newArray = new Array(newCapacity);
  i = 0;

  while (i < newCapacity) {
    newArray[i] = tsplus_module_6.unsafeMake(newBuckets[i]);
    i += 1;
  }

  void 0;
  tsplus_module_8.unsafeSet_(self.tBuckets, new InternalTArray(tsplus_module_7.from(newArray)), journal);
}
/**
 * Stores new binding into the map.
 *
 * @tsplus fluent ets/TMap put
 */


export function put_(self, k, v) {
  void 0;
  return tsplus_module_12.effect(journal => {
    const buckets = tsplus_module_3.unsafeGet_(self.tBuckets, journal);
    void 0;
    const capacity = buckets.chunk.length;
    const idx = tsplus_module_4.indexOf(k, capacity);
    const bucket = tsplus_module_3.unsafeGet_(tsplus_module_2.unsafeGet_(buckets.chunk, idx), journal);
    const shouldUpdate = tsplus_module_10.exists_(bucket, _ => tsplus_module_9.equals(_.get(0), k));

    if (shouldUpdate) {
      const newBucket = tsplus_module_11.map_(bucket, kv => tsplus_module_9.equals(kv.get(0), k) ? tsplus_module_5.make(k, v) : kv);
      tsplus_module_8.unsafeSet_(tsplus_module_2.unsafeGet_(buckets.chunk, idx), newBucket, journal);
    } else {
      const newSize = tsplus_module_3.unsafeGet_(self.tSize, journal) + 1;
      tsplus_module_8.unsafeSet_(self.tSize, newSize, journal);

      if (capacity * LOAD_FACTOR < newSize) {
        resize(self, journal, buckets, k, v);
      } else {
        const newBucket = tsplus_module_1.cons(tsplus_module_5.make(k, v), bucket);
        tsplus_module_8.unsafeSet_(tsplus_module_2.unsafeGet_(buckets.chunk, idx), newBucket, journal);
      }
    }
  });
}
/**
 * Stores new binding into the map.
 *
 * @tsplus static ets/TMap/Aspects put
 */

export const put = (k, v) => self => put_(self, k, v);
//# sourceMappingURL=put.mjs.map