import * as tracing_1 from "../../Tracing";
const fileName_1 = "(@effect-ts/system): _src/Testing/Annotations/index.ts";
import * as Chunk from "../../Collections/Immutable/Chunk";
import * as SortedSet from "../../Collections/Immutable/SortedSet";
import * as Tuple from "../../Collections/Immutable/Tuple";
import * as T from "../../Effect";
import * as FiberRef from "../../FiberRef";
import { pipe } from "../../Function";
import { tag } from "../../Has";
import * as L from "../../Layer";
import * as St from "../../Structural";
import { fiberSet } from "../FiberSet";
import * as TestAnnotation from "../TestAnnotation";
import * as TAM from "../TestAnnotationMap";
export const AnnotationsId = /*#__PURE__*/Symbol();
/**
 * Tag for the Annotations service
 */

export const Annotations = /*#__PURE__*/tag(AnnotationsId);
/**
 * Constructs a new `Annotations` service.
 */

export const live = /*#__PURE__*/L.fromEffect_( /*#__PURE__*/T.gen(function* (_) {
  const fiberRef = yield* _(FiberRef.make(TAM.TestAnnotationMap.empty), fileName_1 + ":55:30");

  const annotate = (key, value) => FiberRef.update_(fiberRef, TAM.annotate(key, value));

  const get = key => T.map_(FiberRef.get(fiberRef), TAM.get(key), fileName_1 + ":64:41");

  const withAnnotation = effect => FiberRef.locally(TAM.TestAnnotationMap.empty)(T.foldM_(effect, e => T.flip(T.map_(FiberRef.get(fiberRef), _ => Tuple.tuple(e, _), fileName_1 + ":79:24"), fileName_1 + ":80:25"), a => T.map_(FiberRef.get(fiberRef), _ => Tuple.tuple(a, _), fileName_1 + ":86:24"), fileName_1 + ":74:20"))(fiberRef);

  const supervisedFibers = T.descriptorWith(d => T.chain_(get(TestAnnotation.fibers), fa => {
    switch (fa._tag) {
      case "Left":
        {
          return T.succeed(fiberSet, fileName_1 + ":99:31");
        }

      case "Right":
        {
          return T.map_(T.map_(T.forEach_(fa.right, ref => T.succeedWith(() => ref.get, fileName_1 + ":104:49"), fileName_1 + ":104:26"), Chunk.reduce(fiberSet, SortedSet.union_), fileName_1 + ":105:22"), SortedSet.filter(_ => !St.equals(_.id, d.id)), fileName_1 + ":106:22");
        }
    }
  }, fileName_1 + ":96:16"), fileName_1 + ":93:46");
  const annotations = {
    serviceId: AnnotationsId,
    annotate,
    get,
    supervisedFibers,
    withAnnotation
  };
  return annotations;
}, fileName_1 + ":54:8"), Annotations);
/**
 * Accesses an `Annotations` instance in the environment and executes the
 * specified effect with an empty annotation map, returning the annotation
 * map along with the result of execution.
 */

export function withAnnotation(effect) {
  return T.accessServiceM(Annotations)(_ => _.withAnnotation(effect), fileName_1 + ":132:39");
}
/**
 * Accesses an `Annotations` instance in the environment and appends the
 * specified annotation to the annotation map.
 */

export function annotate(key, value) {
  return T.accessServiceM(Annotations)(_ => _.annotate(key, value), fileName_1 + ":140:39");
}
/**
 * Accesses an `Annotations` instance in the environment and retrieves the
 * annotation of the specified type, or its default value if there is none.
 */

export function get(key) {
  return T.accessServiceM(Annotations)(_ => _.get(key), fileName_1 + ":148:39");
}
/**
 * Returns a set of all fibers in this test.
 */

export const supervisedFibers = /*#__PURE__*/T.accessServiceM(Annotations)(_ => _.supervisedFibers, fileName_1 + ":154:62");
//# sourceMappingURL=index.js.map