import { e as gql, j as jsxRuntimeExports, h as m, r as reactExports, c as a, d as c, bs as a$1, bt as s, n as namedRoutes, s as s$1, L as Link, w as h, bu as ForwardRef, k as c$1, l as i, bv as b } from "./index-Cr4zbAtA.js";
import { M as Metadata } from "./Metadata-aK5zM6ek.js";
import { c as createCell } from "./createCell-D7cbP3PP.js";
import { S as SpanGenericToggleContext, a as SpanLink } from "./SpanLink-bbDwZtyl.js";
import { L as LinkingIcon } from "./LinkingIcon-P8eDpGZr.js";
import { t as toDate } from "./toDate-C1yAaWVq.js";
import { a as addLeadingZeros } from "./addLeadingZeros-BZPD4mFo.js";
function formatISO(date, options) {
  const _date = toDate(date);
  if (isNaN(_date.getTime())) {
    throw new RangeError("Invalid time value");
  }
  const format = (options == null ? void 0 : options.format) ?? "extended";
  const representation = (options == null ? void 0 : options.representation) ?? "complete";
  let result = "";
  let tzOffset = "";
  const dateDelimiter = format === "extended" ? "-" : "";
  const timeDelimiter = format === "extended" ? ":" : "";
  if (representation !== "time") {
    const day = addLeadingZeros(_date.getDate(), 2);
    const month = addLeadingZeros(_date.getMonth() + 1, 2);
    const year = addLeadingZeros(_date.getFullYear(), 4);
    result = `${year}${dateDelimiter}${month}${dateDelimiter}${day}`;
  }
  if (representation !== "date") {
    const offset = _date.getTimezoneOffset();
    if (offset !== 0) {
      const absoluteOffset = Math.abs(offset);
      const hourOffset = addLeadingZeros(Math.trunc(absoluteOffset / 60), 2);
      const minuteOffset = addLeadingZeros(absoluteOffset % 60, 2);
      const sign = offset < 0 ? "+" : "-";
      tzOffset = `${sign}${hourOffset}:${minuteOffset}`;
    } else {
      tzOffset = "Z";
    }
    const hour = addLeadingZeros(_date.getHours(), 2);
    const minute = addLeadingZeros(_date.getMinutes(), 2);
    const second = addLeadingZeros(_date.getSeconds(), 2);
    const separator = result === "" ? "" : "T";
    const time = [hour, minute, second].join(timeDelimiter);
    result = `${result}${separator}${time}${tzOffset}`;
  }
  return result;
}
const QUERY$2 = gql`
  query FindSpanAncestorsQuery($id: String!) {
    ancestors: otelSpanAncestors(id: $id) {
      id
      spanId
      name
      type {
        id
        name
        colour
      }
    }
  }
`;
const Loading$2 = () => /* @__PURE__ */ jsxRuntimeExports.jsx("div", { children: /* @__PURE__ */ jsxRuntimeExports.jsx(m, { children: "Loading..." }) });
const Empty$2 = () => /* @__PURE__ */ jsxRuntimeExports.jsx("div", { children: /* @__PURE__ */ jsxRuntimeExports.jsx(m, { children: "None..." }) });
const Failure$2 = ({
  error
}) => /* @__PURE__ */ jsxRuntimeExports.jsxs("div", {
  className: "font-bold text-tremor-brand dark:text-dark-tremor-brand",
  children: [
    "Error: ",
    error == null ? void 0 : error.message
  ]
});
const Success$2 = ({
  ancestors
}) => {
  const {
    show
  } = reactExports.useContext(SpanGenericToggleContext);
  return /* @__PURE__ */ jsxRuntimeExports.jsx(a, { flexDirection: "col", className: "mt-4 max-h-[100vh] gap-2 overflow-y-scroll p-2", children: ancestors.filter((span) => span.type.id !== "GENERIC" || show.ancestors).map((span) => /* @__PURE__ */ jsxRuntimeExports.jsx(SpanLink, { id: span.spanId, name: span.name, type: span.type }, span.id)) });
};
const SpanAncestorsCell = createCell({ QUERY: QUERY$2, Loading: Loading$2, Empty: Empty$2, Failure: Failure$2, Success: Success$2, displayName: "SpanAncestorsCell" });
const SpanAttribute = ({
  keyName: key,
  value,
  type: _type
}) => {
  let valueComp = /* @__PURE__ */ jsxRuntimeExports.jsx("pre", { className: "!text-wrap my-4 !whitespace-normal !break-all text-sm text-tremor-content dark:text-dark-tremor-content", children: value });
  try {
    const parsed = JSON.parse(value);
    valueComp = /* @__PURE__ */ jsxRuntimeExports.jsx("pre", { className: "!text-wrap my-4 !whitespace-normal !break-words text-sm text-tremor-content first:text-sm dark:text-dark-tremor-content", children: JSON.stringify(parsed, void 0, 2) });
  } catch (_error) {
  }
  return /* @__PURE__ */ jsxRuntimeExports.jsxs(a, { flexDirection: "col", className: "gap-1", alignItems: "start", justifyContent: "start", children: [
    /* @__PURE__ */ jsxRuntimeExports.jsx(m, { className: "my-4", children: key }),
    valueComp
  ] });
};
const QUERY$1 = gql`
  query FindSpanDescendantsQuery($id: String!) {
    descendants: otelSpanDescendants(id: $id) {
      id
      spanId
      name
      type {
        id
        name
        colour
      }
    }
  }
`;
const Loading$1 = () => /* @__PURE__ */ jsxRuntimeExports.jsx("div", { children: /* @__PURE__ */ jsxRuntimeExports.jsx(m, { children: "Loading..." }) });
const Empty$1 = () => /* @__PURE__ */ jsxRuntimeExports.jsx("div", { children: /* @__PURE__ */ jsxRuntimeExports.jsx(m, { children: "None..." }) });
const Failure$1 = ({
  error
}) => /* @__PURE__ */ jsxRuntimeExports.jsxs("div", {
  className: "font-bold text-tremor-brand dark:text-dark-tremor-brand",
  children: [
    "Error: ",
    error == null ? void 0 : error.message
  ]
});
const Success$1 = ({
  descendants
}) => {
  const {
    show
  } = reactExports.useContext(SpanGenericToggleContext);
  return /* @__PURE__ */ jsxRuntimeExports.jsx(a, { flexDirection: "col", className: "mt-4 max-h-[100vh] gap-2 overflow-y-scroll p-2", children: descendants.filter((span) => span.type.id !== "GENERIC" || show.descendants).map((span) => /* @__PURE__ */ jsxRuntimeExports.jsx(SpanLink, { id: span.spanId, name: span.name, type: span.type }, span.id)) });
};
const SpanDescendantsCell = createCell({ QUERY: QUERY$1, Loading: Loading$1, Empty: Empty$1, Failure: Failure$1, Success: Success$1, displayName: "SpanDescendantsCell" });
const SpanEvent = ({
  name,
  startTimeNano,
  attributes
}) => {
  return /* @__PURE__ */ jsxRuntimeExports.jsx(c, { className: "w-full p-4", children: /* @__PURE__ */ jsxRuntimeExports.jsxs(a, { flexDirection: "col", justifyContent: "start", alignItems: "start", className: "min-h-0", children: [
    /* @__PURE__ */ jsxRuntimeExports.jsxs(a$1, { children: [
      /* @__PURE__ */ jsxRuntimeExports.jsxs(s, { children: [
        /* @__PURE__ */ jsxRuntimeExports.jsx(m, { children: "Name" }),
        /* @__PURE__ */ jsxRuntimeExports.jsx(m, { children: name })
      ] }),
      /* @__PURE__ */ jsxRuntimeExports.jsxs(s, { children: [
        /* @__PURE__ */ jsxRuntimeExports.jsx(m, { children: "Start (ns)" }),
        /* @__PURE__ */ jsxRuntimeExports.jsx(m, { children: startTimeNano })
      ] }),
      attributes.length > 0 ? /* @__PURE__ */ jsxRuntimeExports.jsx(s, { children: /* @__PURE__ */ jsxRuntimeExports.jsx(m, { children: "Attributes" }) }) : null
    ] }),
    /* @__PURE__ */ jsxRuntimeExports.jsx("div", { className: "space-y-2", children: attributes.map(({
      key,
      value,
      type
    }) => {
      return /* @__PURE__ */ jsxRuntimeExports.jsx(SpanAttribute, { keyName: key, value, type }, key);
    }) })
  ] }) });
};
const SpanMetadata = ({
  spanId,
  traceId,
  parentId,
  name,
  kind,
  statusCode,
  statusMessage,
  startTimeNano,
  endTimeNano
}) => {
  const durationNano = Number(BigInt(endTimeNano) - BigInt(startTimeNano));
  let statusCodeComp = /* @__PURE__ */ jsxRuntimeExports.jsxs("span", { className: "italic", children: [
    "Unknown: ",
    statusCode
  ] });
  switch (statusCode.toString()) {
    case "0":
      statusCodeComp = /* @__PURE__ */ jsxRuntimeExports.jsx("span", { children: "Unset" });
      break;
    case "1":
      statusCodeComp = /* @__PURE__ */ jsxRuntimeExports.jsx("span", { children: "OK" });
      break;
    case "2":
      statusCodeComp = /* @__PURE__ */ jsxRuntimeExports.jsx("span", { children: "Error" });
      break;
  }
  let kindComp = /* @__PURE__ */ jsxRuntimeExports.jsxs("span", { className: "italic", children: [
    "Unknown: ",
    kind
  ] });
  switch (kind.toString()) {
    case "0":
      kindComp = /* @__PURE__ */ jsxRuntimeExports.jsx("span", { children: "Internal" });
      break;
    case "1":
      kindComp = /* @__PURE__ */ jsxRuntimeExports.jsx("span", { children: "Server" });
      break;
    case "2":
      kindComp = /* @__PURE__ */ jsxRuntimeExports.jsx("span", { children: "Client" });
      break;
    case "3":
      kindComp = /* @__PURE__ */ jsxRuntimeExports.jsx("span", { children: "Producer" });
      break;
    case "4":
      kindComp = /* @__PURE__ */ jsxRuntimeExports.jsx("span", { children: "Consumer" });
      break;
  }
  return /* @__PURE__ */ jsxRuntimeExports.jsxs(a$1, { children: [
    /* @__PURE__ */ jsxRuntimeExports.jsxs(s, { children: [
      /* @__PURE__ */ jsxRuntimeExports.jsx("span", { children: "Span ID" }),
      /* @__PURE__ */ jsxRuntimeExports.jsxs(a, { flexDirection: "row", className: "gap-1", justifyContent: "end", children: [
        /* @__PURE__ */ jsxRuntimeExports.jsx(LinkingIcon, { to: namedRoutes.opentelemetrySpan({
          id: spanId
        }) }),
        /* @__PURE__ */ jsxRuntimeExports.jsx("span", { children: spanId })
      ] })
    ] }),
    /* @__PURE__ */ jsxRuntimeExports.jsxs(s, { children: [
      /* @__PURE__ */ jsxRuntimeExports.jsx("span", { children: "Trace ID" }),
      /* @__PURE__ */ jsxRuntimeExports.jsxs(a, { flexDirection: "row", className: "gap-1", justifyContent: "end", children: [
        /* @__PURE__ */ jsxRuntimeExports.jsx(LinkingIcon, { to: namedRoutes.opentelemetryTrace({
          id: traceId
        }) }),
        /* @__PURE__ */ jsxRuntimeExports.jsx("span", { children: traceId })
      ] })
    ] }),
    /* @__PURE__ */ jsxRuntimeExports.jsxs(s, { children: [
      /* @__PURE__ */ jsxRuntimeExports.jsx("span", { children: "Parent ID" }),
      parentId ? /* @__PURE__ */ jsxRuntimeExports.jsxs(a, { flexDirection: "row", className: "gap-1", justifyContent: "end", children: [
        /* @__PURE__ */ jsxRuntimeExports.jsx(LinkingIcon, { to: namedRoutes.opentelemetrySpan({
          id: parentId
        }) }),
        /* @__PURE__ */ jsxRuntimeExports.jsx("span", { children: parentId })
      ] }) : /* @__PURE__ */ jsxRuntimeExports.jsx("span", { children: "-" })
    ] }),
    /* @__PURE__ */ jsxRuntimeExports.jsxs(s, { children: [
      /* @__PURE__ */ jsxRuntimeExports.jsx("span", { children: "Name" }),
      /* @__PURE__ */ jsxRuntimeExports.jsx("span", { children: name })
    ] }),
    /* @__PURE__ */ jsxRuntimeExports.jsxs(s, { children: [
      /* @__PURE__ */ jsxRuntimeExports.jsx("span", { children: "Kind" }),
      kindComp
    ] }),
    /* @__PURE__ */ jsxRuntimeExports.jsxs(s, { children: [
      /* @__PURE__ */ jsxRuntimeExports.jsx("span", { children: "Status Code" }),
      statusCodeComp
    ] }),
    /* @__PURE__ */ jsxRuntimeExports.jsxs(s, { children: [
      /* @__PURE__ */ jsxRuntimeExports.jsx("span", { children: "Status Message" }),
      /* @__PURE__ */ jsxRuntimeExports.jsx("span", { children: statusMessage ?? "-" })
    ] }),
    /* @__PURE__ */ jsxRuntimeExports.jsxs(s, { children: [
      /* @__PURE__ */ jsxRuntimeExports.jsx("span", { children: "Started At" }),
      /* @__PURE__ */ jsxRuntimeExports.jsx("span", { children: formatISO(toDate(parseInt(startTimeNano) / 1e6), {
        format: "extended",
        representation: "complete"
      }) })
    ] }),
    /* @__PURE__ */ jsxRuntimeExports.jsxs(s, { children: [
      /* @__PURE__ */ jsxRuntimeExports.jsx("span", { children: "Ended At" }),
      /* @__PURE__ */ jsxRuntimeExports.jsx("span", { children: formatISO(toDate(parseInt(endTimeNano) / 1e6), {
        format: "extended",
        representation: "complete"
      }) })
    ] }),
    /* @__PURE__ */ jsxRuntimeExports.jsxs(s, { children: [
      /* @__PURE__ */ jsxRuntimeExports.jsx("span", { children: "Duration (ns)" }),
      /* @__PURE__ */ jsxRuntimeExports.jsx("span", { children: durationNano.toLocaleString() })
    ] }),
    /* @__PURE__ */ jsxRuntimeExports.jsxs(s, { children: [
      /* @__PURE__ */ jsxRuntimeExports.jsx("span", { children: "Duration (msec)" }),
      /* @__PURE__ */ jsxRuntimeExports.jsx("span", { children: (durationNano / 1e6).toLocaleString() })
    ] }),
    /* @__PURE__ */ jsxRuntimeExports.jsxs(s, { children: [
      /* @__PURE__ */ jsxRuntimeExports.jsx("span", { children: "Duration (sec)" }),
      /* @__PURE__ */ jsxRuntimeExports.jsx("span", { children: (durationNano / 1e9).toLocaleString() })
    ] })
  ] });
};
const SpanResource = ({
  resource
}) => {
  const attributes = resource.attributes ?? [];
  return /* @__PURE__ */ jsxRuntimeExports.jsx(a$1, { className: "overflow-x-scroll", children: attributes.map((attr) => /* @__PURE__ */ jsxRuntimeExports.jsxs(s, { children: [
    /* @__PURE__ */ jsxRuntimeExports.jsx("span", { children: attr.key }),
    /* @__PURE__ */ jsxRuntimeExports.jsx("span", { children: attr.value })
  ] }, attr.id)) });
};
const QUERY = gql`
  query FindSpanQuery($id: String!) {
    span: otelSpan(id: $id) {
      id
      spanId
      traceId
      name
      kind
      attributes {
        id
        key
        value
        type
      }
      parentId
      resource {
        attributes {
          id
          key
          value
          type
        }
      }
      startTimeNano
      endTimeNano
      type {
        id
        name
        colour
      }
      statusCode
      statusMessage
      events {
        id
        name
        attributes {
          id
          key
          value
          type
        }
        startTimeNano
      }
      createdAt
    }
  }
`;
const Loading = () => /* @__PURE__ */ jsxRuntimeExports.jsx("div", { children: "Loading..." });
const Empty = () => /* @__PURE__ */ jsxRuntimeExports.jsx("div", { children: "Empty" });
const Failure = ({
  error
}) => /* @__PURE__ */ jsxRuntimeExports.jsxs("div", {
  className: "font-bold text-tremor-brand dark:text-dark-tremor-brand",
  children: [
    "Error: ",
    error == null ? void 0 : error.message
  ]
});
const Success = ({
  span
}) => {
  const {
    show,
    setShow
  } = reactExports.useContext(SpanGenericToggleContext);
  return /* @__PURE__ */ jsxRuntimeExports.jsxs(jsxRuntimeExports.Fragment, {
    children: [
      /* @__PURE__ */ jsxRuntimeExports.jsxs(a, {
        flexDirection: "row",
        className: "gap-x-4",
        children: [
          /* @__PURE__ */ jsxRuntimeExports.jsxs("div", {
            className: "flex-grow",
            children: [
              /* @__PURE__ */ jsxRuntimeExports.jsx(s$1, { children: "OpenTelemetry Span" }),
              /* @__PURE__ */ jsxRuntimeExports.jsxs(m, {
                children: [
                  "ID: ",
                  span.spanId
                ]
              })
            ]
          }),
          /* @__PURE__ */ jsxRuntimeExports.jsx("div", { children: /* @__PURE__ */ jsxRuntimeExports.jsx(Link, { to: namedRoutes.opentelemetryTrace({
            id: span.traceId
          }), children: /* @__PURE__ */ jsxRuntimeExports.jsx(h, { className: "p-2", children: /* @__PURE__ */ jsxRuntimeExports.jsx(ForwardRef, { className: "h-6 w-6 shrink-0", "aria-hidden": "true" }) }) }) })
        ]
      }),
      /* @__PURE__ */ jsxRuntimeExports.jsx(a, { className: "mt-6 space-y-6", flexDirection: "col", children: /* @__PURE__ */ jsxRuntimeExports.jsxs(c$1, {
        numItems: 1,
        numItemsLg: 3,
        className: "w-full min-w-full gap-4",
        children: [
          /* @__PURE__ */ jsxRuntimeExports.jsx(i, { numColSpan: 1, numColSpanLg: 2, children: /* @__PURE__ */ jsxRuntimeExports.jsxs(c, {
            className: "overflow-x-scroll",
            children: [
              /* @__PURE__ */ jsxRuntimeExports.jsx(s$1, { children: "Attributes" }),
              /* @__PURE__ */ jsxRuntimeExports.jsx(a, { flexDirection: "col", className: "gap-2", justifyContent: "start", alignItems: "start", children: span.attributes.length > 0 ? span.attributes.map(({
                key,
                value,
                type
              }) => {
                return /* @__PURE__ */ jsxRuntimeExports.jsx(SpanAttribute, { keyName: key, value, type }, key);
              }) : /* @__PURE__ */ jsxRuntimeExports.jsx(m, { children: "No attributes..." }) })
            ]
          }) }),
          /* @__PURE__ */ jsxRuntimeExports.jsx(i, { numColSpan: 1, numColSpanLg: 1, children: /* @__PURE__ */ jsxRuntimeExports.jsxs("div", {
            className: "space-y-4",
            children: [
              /* @__PURE__ */ jsxRuntimeExports.jsxs(c, {
                children: [
                  /* @__PURE__ */ jsxRuntimeExports.jsxs(a, {
                    flexDirection: "row",
                    justifyContent: "between",
                    children: [
                      /* @__PURE__ */ jsxRuntimeExports.jsx(s$1, { children: "Ancestors" }),
                      /* @__PURE__ */ jsxRuntimeExports.jsx(b, { id: "switch", name: "switch", checked: show.ancestors, onChange: (v) => setShow({
                        ...show,
                        ancestors: v
                      }) })
                    ]
                  }),
                  /* @__PURE__ */ jsxRuntimeExports.jsx(SpanAncestorsCell, { id: span.spanId })
                ]
              }),
              /* @__PURE__ */ jsxRuntimeExports.jsxs(c, {
                children: [
                  /* @__PURE__ */ jsxRuntimeExports.jsxs(a, {
                    flexDirection: "row",
                    justifyContent: "between",
                    children: [
                      /* @__PURE__ */ jsxRuntimeExports.jsx(s$1, { children: "Descendants" }),
                      /* @__PURE__ */ jsxRuntimeExports.jsx(b, { id: "switch", name: "switch", checked: show.descendants, onChange: (v) => setShow({
                        ...show,
                        descendants: v
                      }) })
                    ]
                  }),
                  /* @__PURE__ */ jsxRuntimeExports.jsx(SpanDescendantsCell, { id: span.spanId })
                ]
              })
            ]
          }) }),
          /* @__PURE__ */ jsxRuntimeExports.jsxs(c, {
            children: [
              /* @__PURE__ */ jsxRuntimeExports.jsx(s$1, { children: "Metadata" }),
              /* @__PURE__ */ jsxRuntimeExports.jsx(SpanMetadata, { spanId: span.spanId, traceId: span.traceId, parentId: span.parentId, name: span.name, kind: span.kind, statusCode: span.statusCode, statusMessage: span.statusMessage, startTimeNano: span.startTimeNano, endTimeNano: span.endTimeNano })
            ]
          }),
          /* @__PURE__ */ jsxRuntimeExports.jsxs(c, {
            children: [
              /* @__PURE__ */ jsxRuntimeExports.jsx(s$1, { children: "Resources" }),
              /* @__PURE__ */ jsxRuntimeExports.jsx(SpanResource, { resource: span.resource })
            ]
          }),
          /* @__PURE__ */ jsxRuntimeExports.jsxs(c, {
            children: [
              /* @__PURE__ */ jsxRuntimeExports.jsx(s$1, { children: "Events" }),
              /* @__PURE__ */ jsxRuntimeExports.jsx(a, { flexDirection: "col", className: "gap-2", justifyContent: "start", alignItems: "start", children: span.events.length > 0 ? span.events.map((event) => /* @__PURE__ */ jsxRuntimeExports.jsx(SpanEvent, { name: event.name, attributes: event.attributes, startTimeNano: event.startTimeNano }, event.startTimeNano + event.name)) : /* @__PURE__ */ jsxRuntimeExports.jsx(m, { children: "No events..." }) })
            ]
          })
        ]
      }) })
    ]
  });
};
const SpanCell = createCell({ QUERY, Loading, Empty, Failure, Success, displayName: "SpanCell" });
const OpenTelemetrySpanPage = ({
  id
}) => {
  const [show, setShow] = reactExports.useState({
    ancestors: false,
    descendants: false
  });
  return /* @__PURE__ */ jsxRuntimeExports.jsxs(jsxRuntimeExports.Fragment, { children: [
    /* @__PURE__ */ jsxRuntimeExports.jsx(Metadata, { title: "OpenTelemetrySpan", description: "OpenTelemetrySpan page" }),
    /* @__PURE__ */ jsxRuntimeExports.jsx(SpanGenericToggleContext.Provider, { value: {
      show,
      setShow
    }, children: /* @__PURE__ */ jsxRuntimeExports.jsx(SpanCell, { id }) })
  ] });
};
export {
  OpenTelemetrySpanPage as default
};
