"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.fromIterable = fromIterable;

var tsplus_module_1 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@tsplus/stdlib/collections/List/definition"));

var tsplus_module_2 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@tsplus/stdlib/collections/List/forEach"));

var tsplus_module_3 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@tsplus/stdlib/data/Tuple/fromNative"));

var tsplus_module_4 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/stm/TMap/operations/indexOf"));

var tsplus_module_5 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@tsplus/stdlib/collections/List/prepend"));

var tsplus_module_6 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/stm/TRef/operations/make"));

var tsplus_module_7 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/stm/STM/operations/map"));

var tsplus_module_8 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/stm/STM/operations/flatMap"));

var tsplus_module_9 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/stm/TArray/operations/from"));

var tsplus_module_10 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@tsplus/stdlib/collections/List/from"));

var tsplus_module_11 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/stm/STM/operations/suspend"));

var _InternalTMap = /*#__PURE__*/require("@effect/core/stm/TMap/operations/_internal/InternalTMap");

function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }

function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

const INITIAL_CAPACITY = 16;

function nextPowerOfTwo(size) {
  const n = -1 >>> Math.clz32(size - 1);
  return n < 0 ? 1 : n + 1;
}

function allocate(capacity, data) {
  const buckets = Array.from({
    length: capacity
  }, () => tsplus_module_1.nil());
  const distinct = new Map();
  tsplus_module_2.forEach_(data, kv => distinct.set(kv.get(0), kv.get(1)));
  let size = 0;

  for (const kv0 of distinct) {
    const kv = tsplus_module_3.fromNative(kv0);
    const idx = tsplus_module_4.indexOf(kv.get(0), capacity);
    buckets[idx] = tsplus_module_5.prepend_(buckets[idx], kv);
    size += 1;
  }

  return tsplus_module_8.flatMap_(tsplus_module_9.from(() => buckets), tArray => tsplus_module_8.flatMap_(tsplus_module_6.make(() => tArray), tBuckets => tsplus_module_7.map_(tsplus_module_6.make(() => size), tSize => new _InternalTMap.InternalTMap(tBuckets, tSize))));
}
/**
 * Retrieves value associated with given key.
 *
 * @tsplus static ets/TMap/Ops fromIterable
 */


function fromIterable(data0) {
  return tsplus_module_11.suspend(() => {
    const data = tsplus_module_10.from(data0);
    const size = tsplus_module_1.length(data);
    const capacity = size < INITIAL_CAPACITY ? INITIAL_CAPACITY : nextPowerOfTwo(size);
    return allocate(capacity, data);
  });
}
//# sourceMappingURL=fromIterable.js.map