// tracing: off
import * as A from "../../Collections/Immutable/Chunk"
import { pipe } from "../../Function"
import * as O from "../../Option"
import * as T from "../_internal/effect"
import * as M from "../_internal/managed"
import * as Ref from "../_internal/ref"
import { DefaultChunkSize, Stream } from "./definitions"
/**
 * Constructs a stream from a range of integers (lower bound included, upper bound not included)
 */

export function range(min, max, chunkSize = DefaultChunkSize) {
  const pull = (ref) =>
    T.map_(
      T.tap_(
        T.bind_(T.do, "start", () => Ref.getAndUpdate_(ref, (_) => _ + chunkSize)),
        ({ start }) => T.when_(T.fail(O.none), () => start >= max)
      ),
      ({ start }) => A.range(start, Math.min(start + chunkSize, max - 1))
    )

  return new Stream(M.map_(Ref.makeManagedRef(min), pull))
}
//# sourceMappingURL=range.js.map
