import * as tsplus_module_1 from "@effect/core/stream/Stream/operations/repeatEffectChunkMaybe";
import * as tsplus_module_2 from "@effect/core/stream/Pull/operations/end";
import * as tsplus_module_3 from "@effect/core/io/Effect/operations/zipRight";
import * as tsplus_module_4 from "@effect/core/stream/Take/operations/done";
import * as tsplus_module_5 from "@effect/core/io/Effect/operations/flatMap";
import * as tsplus_module_6 from "@effect/core/io/Effect/operations/onError";
import * as tsplus_module_7 from "@effect/core/io/Ref/operations/make";
import * as tsplus_module_8 from "@effect/core/stream/Take/operations/fromPull";
import * as tsplus_module_9 from "@effect/core/io/Cause/operations/isInterrupted";
import * as tsplus_module_10 from "@effect/core/io/Effect/operations/runtime";
import * as tsplus_module_11 from "@effect/core/io/Queue/external/bounded";
import * as tsplus_module_12 from "@effect/core/io/Effect/operations/acquireRelease";
import * as tsplus_module_13 from "@effect/core/io/Effect/operations/do";
import * as tsplus_module_14 from "@effect/core/io/Effect/operations/tap";
import * as tsplus_module_15 from "@effect/core/io/Effect/operations/map";
import * as tsplus_module_16 from "@effect/core/stream/Stream/operations/scoped";
import * as tsplus_module_17 from "@effect/core/stream/Stream/operations/flatMap";
import { isFiberFailure } from "@effect/core/io/Cause/errors";
/**
 * Creates a stream from an asynchronous callback that can be called multiple
 * times. The registration of the callback itself returns an a scoped
 * resource. The optionality of the error type `E` can be used to signal the
 * end of the stream, by setting it to `None`.
 *
 * @tsplus static ets/Stream/Ops asyncScoped
 */

export function asyncScoped(register, outputBuffer = 16, __tsplusTrace) {
  return tsplus_module_17.flatMap_(tsplus_module_16.scoped(() => tsplus_module_15.map_(tsplus_module_13.bind_(tsplus_module_14.tap_(tsplus_module_13.bind_(tsplus_module_13.bind_(tsplus_module_13.Do(), "output", () => tsplus_module_12.acquireRelease(() => tsplus_module_11.bounded(outputBuffer, __tsplusTrace), queue => queue.shutdown, __tsplusTrace), __tsplusTrace), "runtime", () => tsplus_module_10.runtime(__tsplusTrace), __tsplusTrace), ({
    output,
    runtime
  }) => register(async k => {
    try {
      runtime.unsafeRunPromise(tsplus_module_5.flatMap_(tsplus_module_8.fromPull(k), take => output.offer(take, __tsplusTrace), __tsplusTrace), __tsplusTrace);
    } catch (e) {
      if (isFiberFailure(e)) {
        if (!tsplus_module_9.isInterrupted(e.cause)) {
          throw e;
        }
      }
    }
  }), __tsplusTrace), "done", () => tsplus_module_7.makeRef(() => false, __tsplusTrace), __tsplusTrace), ({
    done,
    output
  }) => tsplus_module_5.flatMap_(done.get(__tsplusTrace), isDone => isDone ? tsplus_module_2.end : tsplus_module_6.onError_(tsplus_module_5.flatMap_(output.take, take => tsplus_module_4.done(take, __tsplusTrace), __tsplusTrace), () => tsplus_module_3.zipRight_(done.set(true, __tsplusTrace), () => output.shutdown, __tsplusTrace), __tsplusTrace), __tsplusTrace), __tsplusTrace), __tsplusTrace), pull => tsplus_module_1.repeatEffectChunkMaybe(() => pull, __tsplusTrace), __tsplusTrace);
}
//# sourceMappingURL=asyncScoped.mjs.map