"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.supervisedFibers = exports.get = exports.annotate = exports.withAnnotation = exports.live = exports.Annotations = exports.AnnotationsId = void 0;
const tracing_1 = __importStar(require("../../Tracing"));
const fileName_1 = "(@effect-ts/system): _src/Testing/Annotations/index.ts";
const Chunk = __importStar(require("../../Collections/Immutable/Chunk"));
const SortedSet = __importStar(require("../../Collections/Immutable/SortedSet"));
const Tuple = __importStar(require("../../Collections/Immutable/Tuple"));
const T = __importStar(require("../../Effect"));
const FiberRef = __importStar(require("../../FiberRef"));
const Function_1 = require("../../Function");
const Has_1 = require("../../Has");
const L = __importStar(require("../../Layer"));
const St = __importStar(require("../../Structural"));
const FiberSet_1 = require("../FiberSet");
const TestAnnotation = __importStar(require("../TestAnnotation"));
const TAM = __importStar(require("../TestAnnotationMap"));
exports.AnnotationsId = Symbol();
/**
 * Tag for the Annotations service
 */
exports.Annotations = (0, Has_1.tag)(exports.AnnotationsId);
/**
 * Constructs a new `Annotations` service.
 */
exports.live = L.fromEffect_(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_1.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_1.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: exports.AnnotationsId,
        annotate,
        get,
        supervisedFibers,
        withAnnotation
    };
    return annotations;
}, fileName_1 + ":54:8"), exports.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.
 */
function withAnnotation(effect) {
    return T.accessServiceM(exports.Annotations)((_) => _.withAnnotation(effect), fileName_1 + ":132:39");
}
exports.withAnnotation = withAnnotation;
/**
 * Accesses an `Annotations` instance in the environment and appends the
 * specified annotation to the annotation map.
 */
function annotate(key, value) {
    return T.accessServiceM(exports.Annotations)((_) => _.annotate(key, value), fileName_1 + ":140:39");
}
exports.annotate = annotate;
/**
 * Accesses an `Annotations` instance in the environment and retrieves the
 * annotation of the specified type, or its default value if there is none.
 */
function get(key) {
    return T.accessServiceM(exports.Annotations)((_) => _.get(key), fileName_1 + ":148:39");
}
exports.get = get;
/**
 * Returns a set of all fibers in this test.
 */
exports.supervisedFibers = T.accessServiceM(exports.Annotations)((_) => _.supervisedFibers, fileName_1 + ":154:62");
//# sourceMappingURL=index.js.map