"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.live = exports.defaultRandom = exports.LiveRandom = void 0;

var tsplus_module_1 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@tsplus/stdlib/data/LazyValue"));

var tsplus_module_2 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/io/Random/definition"));

var tsplus_module_3 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/io/Layer/operations/fromValue"));

var tsplus_module_4 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@tsplus/stdlib/utilities/RandomPCG"));

var tsplus_module_5 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/io/Effect/operations/succeed"));

var tsplus_module_6 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/io/Effect/operations/flatMap"));

var tsplus_module_7 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/io/Effect/operations/excl-forEach"));

var tsplus_module_8 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/io/Effect/operations/do"));

var tsplus_module_9 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/io/Effect/operations/tap"));

var tsplus_module_10 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/io/Effect/operations/map"));

var tsplus_module_11 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/io/Effect/operations/suspendSucceed"));

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; }

var _a;

const fileName_1 = "(@effect/core) _src/io/Random/operations/live.ts";

/**
 * @tsplus static ets/Random/Ops default
 */
const defaultRandom_1 = /*#__PURE__*/tsplus_module_1.LazyValue.make(() => new LiveRandom(Math.random() * 4294967296 >>> 0));
const defaultRandom = defaultRandom_1;
/**
 * @tsplus static ets/Random/Ops live
 */

exports.defaultRandom = defaultRandom;
const live = /*#__PURE__*/tsplus_module_3.fromValue(tsplus_module_2.Random.Tag, () => defaultRandom_1.value);
exports.live = live;

class LiveRandom {
  constructor(seed) {
    this.seed = seed;
    this[_a] = tsplus_module_2.RandomSym;
    this.PRNG = new tsplus_module_4.RandomPCG(seed);
  }

  get next() {
    return tsplus_module_5.succeed(() => this.PRNG.number(), fileName_1 + ":23:26");
  }

  get nextBoolean() {
    return tsplus_module_6.flatMap_(this.next, n => tsplus_module_5.succeed(() => n > 0.5, fileName_1 + ":27:51"), fileName_1 + ":27:29");
  }

  get nextInt() {
    return tsplus_module_5.succeed(() => this.PRNG.integer(0), fileName_1 + ":31:26");
  }

  nextRange(low, high, __tsplusTrace) {
    return tsplus_module_6.flatMap_(this.next, n => tsplus_module_5.succeed(() => (high - low) * n + low, fileName_1 + ":35:51"), fileName_1 + ":35:29");
  }

  nextIntBetween(low, high, __tsplusTrace) {
    return tsplus_module_5.succeed(() => this.PRNG.integer(high - low) + low, fileName_1 + ":43:26");
  }

  shuffle(collection, __tsplusTrace) {
    return shuffleWith(collection, n => this.nextIntBetween(0, n, fileName_1 + ":50:62"), fileName_1 + ":50:23");
  }

}

exports.LiveRandom = LiveRandom;
_a = tsplus_module_2.RandomSym;

function shuffleWith(collection, nextIntBounded, __tsplusTrace) {
  return tsplus_module_11.suspendSucceed(() => {
    const collection0 = collection();
    return tsplus_module_10.map_(tsplus_module_9.tap_(tsplus_module_8.bindValue_(tsplus_module_8.bind_(tsplus_module_8.Do(), "buffer", () => tsplus_module_5.succeed(() => {
      const buffer = [];

      for (const element of collection0) {
        buffer.push(element);
      }

      return buffer;
    }, __tsplusTrace), __tsplusTrace), "swap", ({
      buffer
    }) => (i1, i2) => tsplus_module_5.succeed(() => {
      const tmp = buffer[i1];
      buffer[i1] = buffer[i2];
      buffer[i2] = tmp;
      return buffer;
    }, __tsplusTrace), __tsplusTrace), ({
      buffer,
      swap
    }) => {
      const ns = [];

      for (let i = buffer.length; i >= 2; i = i - 1) {
        ns.push(i);
      }

      return tsplus_module_7.forEachDiscard(() => ns, n => tsplus_module_6.flatMap_(nextIntBounded(n), k => swap(n - 1, k), __tsplusTrace), __tsplusTrace);
    }, __tsplusTrace), ({
      buffer
    }) => buffer, __tsplusTrace);
  }, __tsplusTrace);
}
//# sourceMappingURL=live.js.map