"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.prettyTraceSafe = exports.prettyTrace = exports.truncatedParentTrace = exports.parents = exports.ancestryLength = exports.ancestryLengthSafe = exports.Trace = exports.traceLocation = exports.SourceLocation = exports.NoLocation = void 0;
// ets_tracing: off
const L = __importStar(require("../Collections/Immutable/List/core"));
const O = __importStar(require("../Option"));
const S = __importStar(require("../Sync"));
const id_1 = require("./id");
class NoLocation {
    constructor() {
        this._tag = "NoLocation";
    }
}
exports.NoLocation = NoLocation;
class SourceLocation {
    constructor(location) {
        this.location = location;
        this._tag = "SourceLocation";
    }
}
exports.SourceLocation = SourceLocation;
function traceLocation(k) {
    if (k) {
        return new SourceLocation(k);
    }
    return new NoLocation();
}
exports.traceLocation = traceLocation;
class Trace {
    constructor(fiberId, executionTrace, stackTrace, parentTrace) {
        this.fiberId = fiberId;
        this.executionTrace = executionTrace;
        this.stackTrace = stackTrace;
        this.parentTrace = parentTrace;
    }
}
exports.Trace = Trace;
function ancestryLengthSafe(trace, i) {
    const parent = trace.parentTrace;
    if (parent._tag === "None") {
        return S.succeed(i);
    }
    else {
        return S.suspend(() => ancestryLengthSafe(parent.value, i + 1));
    }
}
exports.ancestryLengthSafe = ancestryLengthSafe;
function ancestryLength(trace) {
    return S.run(ancestryLengthSafe(trace, 0));
}
exports.ancestryLength = ancestryLength;
function parents(trace) {
    const pushable = L.emptyPushable();
    let parent = O.toUndefined(trace.parentTrace);
    while (parent != null) {
        L.push_(pushable, parent);
        parent = O.toUndefined(parent.parentTrace);
    }
    return pushable;
}
exports.parents = parents;
function truncatedParentTrace(trace, maxAncestors) {
    if (ancestryLength(trace) > maxAncestors) {
        return L.reduceRight_(L.take_(parents(trace), maxAncestors), O.none, (trace, parent) => O.some(new Trace(trace.fiberId, trace.executionTrace, trace.stackTrace, parent)));
    }
    else {
        return trace.parentTrace;
    }
}
exports.truncatedParentTrace = truncatedParentTrace;
function prettyTrace(trace) {
    return S.run(prettyTraceSafe(trace));
}
exports.prettyTrace = prettyTrace;
function prettyTraceSafe(trace) {
    return S.gen(function* ($) {
        const execution = L.filter_(trace.executionTrace, (_) => _._tag === "SourceLocation");
        const stack = L.filter_(trace.stackTrace, (_) => _._tag === "SourceLocation");
        const execTrace = !L.isEmpty(execution);
        const stackTrace = !L.isEmpty(stack);
        const execPrint = execTrace
            ? [
                `Fiber: ${(0, id_1.prettyFiberId)(trace.fiberId)} Execution trace:`,
                "",
                ...L.toArray(L.map_(execution, (a) => `  ${a.location}`))
            ]
            : [`Fiber: ${(0, id_1.prettyFiberId)(trace.fiberId)} Execution trace: <empty trace>`];
        const stackPrint = stackTrace
            ? [
                `Fiber: ${(0, id_1.prettyFiberId)(trace.fiberId)} was supposed to continue to:`,
                "",
                ...L.toArray(L.map_(stack, (e) => `  a future continuation at ${e.location}`))
            ]
            : [
                `Fiber: ${(0, id_1.prettyFiberId)(trace.fiberId)} was supposed to continue to: <empty trace>`
            ];
        const parent = trace.parentTrace;
        const ancestry = parent._tag === "None"
            ? [`Fiber: ${(0, id_1.prettyFiberId)(trace.fiberId)} was spawned by: <empty trace>`]
            : [
                `Fiber: ${(0, id_1.prettyFiberId)(trace.fiberId)} was spawned by:`,
                yield* $(prettyTraceSafe(parent.value))
            ];
        return ["", ...stackPrint, "", ...execPrint, "", ...ancestry].join("\n");
    });
}
exports.prettyTraceSafe = prettyTraceSafe;
//# sourceMappingURL=tracing.js.map