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

import * as z from "zod/v3";
import { safeParse } from "../lib/schemas.js";
import { ClosedEnum } from "../types/enums.js";
import { Result as SafeParseResult } from "../types/fp.js";
import * as types from "../types/primitives.js";
import { smartUnion } from "../types/smartUnion.js";
import { SDKValidationError } from "./sdkvalidationerror.js";
import { Session, Session$inboundSchema } from "./session.js";

/**
 * Sort direction for results by creation time.
 */
export const ListSessionsQueryParamSortOrder = {
  Asc: "asc",
  Desc: "desc",
} as const;
/**
 * Sort direction for results by creation time.
 */
export type ListSessionsQueryParamSortOrder = ClosedEnum<
  typeof ListSessionsQueryParamSortOrder
>;

export type ListSessionsRequest = {
  /**
   * The unique identifier or name of the project to list sessions for.
   */
  project?: string | undefined;
  /**
   * Filter sessions by sandbox name. Only sessions belonging to the specified sandbox are returned.
   */
  name?: string | undefined;
  /**
   * Maximum number of sessions to return in the response. Used for pagination.
   */
  limit?: number | undefined;
  /**
   * Opaque pagination cursor from a previous response.
   */
  cursor?: string | undefined;
  /**
   * Sort direction for results by creation time.
   */
  sortOrder?: ListSessionsQueryParamSortOrder | undefined;
  /**
   * 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 ListSessionsResponseBodyPagination = {
  count: number;
  next: string | null;
};

export type ListSessionsResponseBody2 = {
  sessions: Array<Session>;
  pagination: ListSessionsResponseBodyPagination;
};

export type ListSessionsResponseBody1 = {};

/**
 * The list of sessions matching the request filters.
 */
export type ListSessionsResponseBody =
  | ListSessionsResponseBody2
  | ListSessionsResponseBody1;

/** @internal */
export const ListSessionsQueryParamSortOrder$outboundSchema: z.ZodNativeEnum<
  typeof ListSessionsQueryParamSortOrder
> = z.nativeEnum(ListSessionsQueryParamSortOrder);

/** @internal */
export type ListSessionsRequest$Outbound = {
  project?: string | undefined;
  name?: string | undefined;
  limit: number;
  cursor?: string | undefined;
  sortOrder: string;
  teamId?: string | undefined;
  slug?: string | undefined;
};

/** @internal */
export const ListSessionsRequest$outboundSchema: z.ZodType<
  ListSessionsRequest$Outbound,
  z.ZodTypeDef,
  ListSessionsRequest
> = z.object({
  project: z.string().optional(),
  name: z.string().optional(),
  limit: z.number().default(20),
  cursor: z.string().optional(),
  sortOrder: ListSessionsQueryParamSortOrder$outboundSchema.default("desc"),
  teamId: z.string().optional(),
  slug: z.string().optional(),
});

export function listSessionsRequestToJSON(
  listSessionsRequest: ListSessionsRequest,
): string {
  return JSON.stringify(
    ListSessionsRequest$outboundSchema.parse(listSessionsRequest),
  );
}

/** @internal */
export const ListSessionsResponseBodyPagination$inboundSchema: z.ZodType<
  ListSessionsResponseBodyPagination,
  z.ZodTypeDef,
  unknown
> = z.object({
  count: types.number(),
  next: types.nullable(types.string()),
});

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

/** @internal */
export const ListSessionsResponseBody2$inboundSchema: z.ZodType<
  ListSessionsResponseBody2,
  z.ZodTypeDef,
  unknown
> = z.object({
  sessions: z.array(Session$inboundSchema),
  pagination: z.lazy(() => ListSessionsResponseBodyPagination$inboundSchema),
});

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

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

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

/** @internal */
export const ListSessionsResponseBody$inboundSchema: z.ZodType<
  ListSessionsResponseBody,
  z.ZodTypeDef,
  unknown
> = smartUnion([
  z.lazy(() => ListSessionsResponseBody2$inboundSchema),
  z.lazy(() => ListSessionsResponseBody1$inboundSchema),
]);

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