var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var opentelemetry_exports = {};
__export(opentelemetry_exports, {
  detectSpanTypeAndBrief: () => detectSpanTypeAndBrief,
  retypeSpan: () => retypeSpan
});
module.exports = __toCommonJS(opentelemetry_exports);
var import_some = __toESM(require("@babel/runtime-corejs3/core-js/instance/some"));
var import_bind = __toESM(require("@babel/runtime-corejs3/core-js/instance/bind"));
var import_find = __toESM(require("@babel/runtime-corejs3/core-js/instance/find"));
var import_includes = __toESM(require("@babel/runtime-corejs3/core-js/instance/includes"));
var import_trim = __toESM(require("@babel/runtime-corejs3/core-js/instance/trim"));
var import_starts_with = __toESM(require("@babel/runtime-corejs3/core-js/instance/starts-with"));
var import_map = __toESM(require("@babel/runtime-corejs3/core-js/map"));
var import_map2 = __toESM(require("@babel/runtime-corejs3/core-js/instance/map"));
var import_project = require("../util/project");
var import_db = require("./db");
async function detectSpanTypeAndBrief({
  name,
  attributes
}) {
  var _context, _context2, _context7, _context8, _context12, _context13;
  const isHTTP = ((_context = attributes) == null ? void 0 : (0, import_bind.default)(_context2 = Function.call).call(_context2, (0, import_some.default)(_context), _context))?.((attribute) => attribute.key === "http.method");
  if (isHTTP) {
    var _context3, _context4, _context5, _context6;
    const method = ((_context3 = attributes) == null ? void 0 : (0, import_bind.default)(_context4 = Function.call).call(_context4, (0, import_find.default)(_context3), _context3))?.((attribute) => attribute.key === "http.method").value.stringValue;
    const url = ((_context5 = attributes) == null ? void 0 : (0, import_bind.default)(_context6 = Function.call).call(_context6, (0, import_find.default)(_context5), _context5))?.((attribute) => attribute.key === "http.url").value.stringValue;
    const brief = `${method} ${url}`.substring(0, 255);
    return {
      typeId: "HTTP",
      brief
    };
  }
  const graphqlAttributes = ["graphql.operation.type", "graphql.operation.name", "graphql.operation.document", "graphql.execute.operationName", "graphql.execute.document", "graphql.execute.result", "graphql.execute.error", "graphql.execute.variables"];
  const isGraphQL = ((_context7 = attributes) == null ? void 0 : (0, import_bind.default)(_context8 = Function.call).call(_context8, (0, import_some.default)(_context7), _context7))?.((attribute) => {
    return (0, import_includes.default)(graphqlAttributes).call(graphqlAttributes, attribute.key);
  });
  if (isGraphQL) {
    var _context9, _context0, _context1, _context10, _context11;
    const operationType = ((_context9 = attributes) == null ? void 0 : (0, import_bind.default)(_context0 = Function.call).call(_context0, (0, import_find.default)(_context9), _context9))?.((attribute) => attribute.key === "graphql.operation.type")?.value.stringValue;
    const operationName = ((_context1 = attributes) == null ? void 0 : (0, import_bind.default)(_context10 = Function.call).call(_context10, (0, import_find.default)(_context1), _context1))?.((attribute) => attribute.key === "graphql.operation.name")?.value.stringValue;
    const brief = (0, import_trim.default)(_context11 = `${operationType} ${operationName}`.substring(0, 255)).call(_context11);
    return {
      typeId: "GRAPHQL",
      brief
    };
  }
  const isSQL = ((_context12 = attributes) == null ? void 0 : (0, import_bind.default)(_context13 = Function.call).call(_context13, (0, import_some.default)(_context12), _context12))?.((attribute) => attribute.key === "db.statement");
  if (isSQL) {
    var _context14, _context15;
    const brief = ((_context14 = attributes) == null ? void 0 : (0, import_bind.default)(_context15 = Function.call).call(_context15, (0, import_find.default)(_context14), _context14))?.((attribute) => attribute.key === "db.statement").value.stringValue?.substring(0, 255);
    return {
      typeId: "SQL",
      brief
    };
  }
  const isPrisma = (0, import_starts_with.default)(name).call(name, "prisma:client:");
  if (isPrisma) {
    const brief = (0, import_find.default)(attributes).call(attributes, (attribute) => attribute.key === "name")?.value.stringValue?.substring(0, 255);
    return {
      typeId: "PRISMA",
      brief
    };
  }
  const isRedwoodService = (0, import_starts_with.default)(name).call(name, "redwoodjs:api:services");
  if (isRedwoodService) {
    const brief = getRelativeCodeFilepath((0, import_find.default)(attributes).call(attributes, (attribute) => attribute.key === "name")?.value.stringValue ?? "").substring(0, 255);
    return {
      typeId: "REDWOOD_SERVICE",
      brief
    };
  }
  const isRedwoodFunction = (0, import_starts_with.default)(name).call(name, "redwoodjs:api:functions");
  if (isRedwoodFunction) {
    const brief = getRelativeCodeFilepath((0, import_find.default)(attributes).call(attributes, (attribute) => attribute.key === "name")?.value.stringValue ?? "").substring(0, 255);
    return {
      typeId: "REDWOOD_FUNCTION",
      brief
    };
  }
  return {
    typeId: "GENERIC"
  };
}
async function retypeSpan({
  id
}) {
  var _context16;
  const span = await import_db.db.oTelTraceSpan.findUnique({
    where: {
      id
    },
    include: {
      attributes: true
    }
  });
  if (!span) {
    throw new Error(`Span not found: ${id}`);
  }
  const attributes = await import_db.db.$queryRaw`SELECT id, [value] FROM OTelTraceAttribute;`;
  const attributeValueMap = new import_map.default();
  for (let i = 0; i < attributes.length; i++) {
    attributeValueMap.set(attributes[i].id, attributes[i].value);
  }
  const convertedSpanAttributes = (0, import_map2.default)(_context16 = span.attributes).call(_context16, (attribute) => ({
    key: attribute.key,
    value: {
      // NOTE: Only handles string values for now, but this is fine as long as
      // detectSpanTypeAndBrief also only handles string values
      stringValue: attributeValueMap.get(attribute.id) ?? ""
    }
  }));
  const {
    typeId,
    brief
  } = await detectSpanTypeAndBrief({
    name: span.name,
    attributes: convertedSpanAttributes
  });
  await import_db.db.oTelTraceSpan.update({
    where: {
      id
    },
    data: {
      type: {
        connect: {
          id: typeId
        }
      },
      brief
    }
  });
}
function getRelativeCodeFilepath(filepath) {
  const projectRoot = (0, import_project.getUserProjectPaths)().base;
  return filepath.replace(projectRoot, "");
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  detectSpanTypeAndBrief,
  retypeSpan
});
//# sourceMappingURL=opentelemetry.js.map
