/*
 * Code generated by Speakeasy (https://speakeasy.com). DO NOT EDIT.
 */

import * as z from "zod/v3";
import { safeParse } from "../lib/schemas.js";
import { Result as SafeParseResult } from "../types/fp.js";
import * as types from "../types/primitives.js";
import { SDKValidationError } from "./sdkvalidationerror.js";

export type GetProjectTraceRequest = {
  /**
   * The project ID
   */
  projectId: string;
  /**
   * The Vercel CLI request ID associated with the trace
   */
  requestId: string;
  /**
   * The Team identifier to perform the request on behalf of.
   */
  teamId?: string | undefined;
  /**
   * The Team slug to perform the request on behalf of.
   */
  slug?: string | undefined;
};

export type GetProjectTraceResources = {
  name: string;
  attributes: { [k: string]: string };
};

export type Library = {
  name: string;
  version?: string | undefined;
};

export type GetProjectTraceStatus = {
  code: number;
  message?: string | undefined;
};

export type GetProjectTraceEvents = {
  name: string;
  timestamp: Array<number>;
  attributes: { [k: string]: any };
};

export type Spans = {
  name: string;
  kind: number;
  resource: string;
  library: Library;
  spanId: string;
  parentSpanId?: string | undefined;
  status: GetProjectTraceStatus;
  traceState?: string | undefined;
  traceFlags: number;
  attributes: { [k: string]: any };
  links: Array<{ [k: string]: any }>;
  events: Array<GetProjectTraceEvents>;
  startTime: Array<number>;
  endTime: Array<number>;
  duration: Array<number>;
};

export type Trace = {
  traceId: string;
  resources?: Array<GetProjectTraceResources> | undefined;
  spans: Array<Spans>;
  rootSpanId?: string | undefined;
};

export type GetProjectTraceResponseBody = {
  trace: Trace;
};

/** @internal */
export type GetProjectTraceRequest$Outbound = {
  projectId: string;
  requestId: string;
  teamId?: string | undefined;
  slug?: string | undefined;
};

/** @internal */
export const GetProjectTraceRequest$outboundSchema: z.ZodType<
  GetProjectTraceRequest$Outbound,
  z.ZodTypeDef,
  GetProjectTraceRequest
> = z.object({
  projectId: z.string(),
  requestId: z.string(),
  teamId: z.string().optional(),
  slug: z.string().optional(),
});

export function getProjectTraceRequestToJSON(
  getProjectTraceRequest: GetProjectTraceRequest,
): string {
  return JSON.stringify(
    GetProjectTraceRequest$outboundSchema.parse(getProjectTraceRequest),
  );
}

/** @internal */
export const GetProjectTraceResources$inboundSchema: z.ZodType<
  GetProjectTraceResources,
  z.ZodTypeDef,
  unknown
> = z.object({
  name: types.string(),
  attributes: z.record(types.string()),
});

export function getProjectTraceResourcesFromJSON(
  jsonString: string,
): SafeParseResult<GetProjectTraceResources, SDKValidationError> {
  return safeParse(
    jsonString,
    (x) => GetProjectTraceResources$inboundSchema.parse(JSON.parse(x)),
    `Failed to parse 'GetProjectTraceResources' from JSON`,
  );
}

/** @internal */
export const Library$inboundSchema: z.ZodType<Library, z.ZodTypeDef, unknown> =
  z.object({
    name: types.string(),
    version: types.optional(types.string()),
  });

export function libraryFromJSON(
  jsonString: string,
): SafeParseResult<Library, SDKValidationError> {
  return safeParse(
    jsonString,
    (x) => Library$inboundSchema.parse(JSON.parse(x)),
    `Failed to parse 'Library' from JSON`,
  );
}

/** @internal */
export const GetProjectTraceStatus$inboundSchema: z.ZodType<
  GetProjectTraceStatus,
  z.ZodTypeDef,
  unknown
> = z.object({
  code: types.number(),
  message: types.optional(types.string()),
});

export function getProjectTraceStatusFromJSON(
  jsonString: string,
): SafeParseResult<GetProjectTraceStatus, SDKValidationError> {
  return safeParse(
    jsonString,
    (x) => GetProjectTraceStatus$inboundSchema.parse(JSON.parse(x)),
    `Failed to parse 'GetProjectTraceStatus' from JSON`,
  );
}

/** @internal */
export const GetProjectTraceEvents$inboundSchema: z.ZodType<
  GetProjectTraceEvents,
  z.ZodTypeDef,
  unknown
> = z.object({
  name: types.string(),
  timestamp: z.array(types.number()),
  attributes: z.record(z.any()),
});

export function getProjectTraceEventsFromJSON(
  jsonString: string,
): SafeParseResult<GetProjectTraceEvents, SDKValidationError> {
  return safeParse(
    jsonString,
    (x) => GetProjectTraceEvents$inboundSchema.parse(JSON.parse(x)),
    `Failed to parse 'GetProjectTraceEvents' from JSON`,
  );
}

/** @internal */
export const Spans$inboundSchema: z.ZodType<Spans, z.ZodTypeDef, unknown> = z
  .object({
    name: types.string(),
    kind: types.number(),
    resource: types.string(),
    library: z.lazy(() => Library$inboundSchema),
    spanId: types.string(),
    parentSpanId: types.optional(types.string()),
    status: z.lazy(() => GetProjectTraceStatus$inboundSchema),
    traceState: types.optional(types.string()),
    traceFlags: types.number(),
    attributes: z.record(z.any()),
    links: z.array(z.record(z.any())),
    events: z.array(z.lazy(() => GetProjectTraceEvents$inboundSchema)),
    startTime: z.array(types.number()),
    endTime: z.array(types.number()),
    duration: z.array(types.number()),
  });

export function spansFromJSON(
  jsonString: string,
): SafeParseResult<Spans, SDKValidationError> {
  return safeParse(
    jsonString,
    (x) => Spans$inboundSchema.parse(JSON.parse(x)),
    `Failed to parse 'Spans' from JSON`,
  );
}

/** @internal */
export const Trace$inboundSchema: z.ZodType<Trace, z.ZodTypeDef, unknown> = z
  .object({
    traceId: types.string(),
    resources: types.optional(
      z.array(z.lazy(() => GetProjectTraceResources$inboundSchema)),
    ),
    spans: z.array(z.lazy(() => Spans$inboundSchema)),
    rootSpanId: types.optional(types.string()),
  });

export function traceFromJSON(
  jsonString: string,
): SafeParseResult<Trace, SDKValidationError> {
  return safeParse(
    jsonString,
    (x) => Trace$inboundSchema.parse(JSON.parse(x)),
    `Failed to parse 'Trace' from JSON`,
  );
}

/** @internal */
export const GetProjectTraceResponseBody$inboundSchema: z.ZodType<
  GetProjectTraceResponseBody,
  z.ZodTypeDef,
  unknown
> = z.object({
  trace: z.lazy(() => Trace$inboundSchema),
});

export function getProjectTraceResponseBodyFromJSON(
  jsonString: string,
): SafeParseResult<GetProjectTraceResponseBody, SDKValidationError> {
  return safeParse(
    jsonString,
    (x) => GetProjectTraceResponseBody$inboundSchema.parse(JSON.parse(x)),
    `Failed to parse 'GetProjectTraceResponseBody' from JSON`,
  );
}
