import * as tracing_1 from "../../Tracing"
import * as tracing_2 from "../../Tracing"
const fileName_1 = "(@effect-ts/system): _src/Testing/FibersPerTest/index.ts"
import * as Chunk from "../../Collections/Immutable/Chunk"
import * as SS from "../../Collections/Immutable/SortedSet"
import * as T from "../../Effect"
import * as E from "../../Either"
import { runtimeOrd } from "../../Fiber"
import { pipe } from "../../Function"
import * as Supervisor from "../../Supervisor"
import { AtomicReference } from "../../Support/AtomicReference"
import * as Annotations from "../Annotations"
import { Int } from "../Int"
import { fibers } from "../TestAnnotation"
export function fibersPerTest(self) {
  const acquire = T.tap_(
    T.succeedWith(
      () => new AtomicReference(SS.make(runtimeOrd())),
      fileName_1 + ":15:18"
    ),
    (ref) => Annotations.annotate(fibers, E.right(Chunk.single(ref))),
    fileName_1 + ":16:10"
  )
  const release = T.chain_(
    Annotations.get(fibers),
    (f) => {
      switch (f._tag) {
        case "Left":
          return T.unit

        case "Right":
          return T.tap_(
            T.map_(
              T.map_(
                T.forEach_(
                  f.right,
                  (_) => T.succeedWith(() => _.get, fileName_1 + ":28:43"),
                  fileName_1 + ":28:22"
                ),
                Chunk.reduce(SS.make(runtimeOrd()), SS.union_),
                fileName_1 + ":29:18"
              ),
              SS.size,
              fileName_1 + ":30:18"
            ),
            (n) => Annotations.annotate(fibers, E.left(Int(n))),
            fileName_1 + ":31:18"
          )
      }
    },
    fileName_1 + ":21:12"
  )
  return T.bracket_(
    acquire,
    (ref) =>
      T.chain_(
        Supervisor.fibersIn(ref),
        (supervisor) => T.supervised_(self, supervisor, fileName_1 + ":42:46"),
        fileName_1 + ":42:16"
      ),
    () => release,
    fileName_1 + ":37:20"
  )
}
//# sourceMappingURL=index.js.map
