"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.NodeEndHandler = void 0;
const di_1 = require("@n8n/di");
const api_1 = require("@opentelemetry/api");
const semantic_conventions_1 = require("@opentelemetry/semantic-conventions");
const otel_constants_1 = require("../otel.constants");
let NodeEndHandler = class NodeEndHandler {
    handle(ctx, spans) {
        const node = ctx.workflow.nodes.find((n) => n.name === ctx.nodeName);
        if (!node)
            return;
        const span = spans.removeNode(ctx.executionId, node.id);
        if (!span)
            return;
        const outputItems = this.countItems(ctx.taskData.data);
        span.setAttribute(otel_constants_1.ATTR.NODE_ITEMS_OUTPUT, outputItems);
        const inputItems = this.countInputItems(ctx);
        span.setAttribute(otel_constants_1.ATTR.NODE_ITEMS_INPUT, inputItems);
        if (ctx.taskData.metadata?.tracing) {
            for (const [key, value] of Object.entries(ctx.taskData.metadata.tracing)) {
                span.setAttribute(`n8n.node.custom.${key}`, value);
            }
        }
        if (ctx.taskData.error) {
            span.setStatus({ code: api_1.SpanStatusCode.ERROR });
            span.addEvent('exception', {
                [semantic_conventions_1.ATTR_EXCEPTION_MESSAGE]: ctx.taskData.error.message,
                [semantic_conventions_1.ATTR_EXCEPTION_TYPE]: ctx.taskData.error.constructor.name,
                [semantic_conventions_1.ATTR_EXCEPTION_STACKTRACE]: ctx.taskData.error.stack,
            });
        }
        else {
            span.setStatus({ code: api_1.SpanStatusCode.OK });
        }
        span.end();
    }
    countItems(data) {
        if (!data?.main)
            return 0;
        return data.main.reduce((sum, branch) => sum + (branch?.length ?? 0), 0);
    }
    countInputItems(ctx) {
        const runData = ctx.executionData.resultData.runData;
        let total = 0;
        for (const source of ctx.taskData.source) {
            if (!source)
                continue;
            const sourceRuns = runData[source.previousNode];
            if (!sourceRuns)
                continue;
            const run = sourceRuns[source.previousNodeRun ?? 0];
            if (!run?.data?.main)
                continue;
            const branch = run.data.main[source.previousNodeOutput ?? 0];
            total += branch?.length ?? 0;
        }
        return total;
    }
};
exports.NodeEndHandler = NodeEndHandler;
exports.NodeEndHandler = NodeEndHandler = __decorate([
    (0, di_1.Service)()
], NodeEndHandler);
//# sourceMappingURL=node-end.handler.js.map