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

import * as z from "zod/v4-mini";
import { remap as remap$ } from "../../lib/primitives.js";
import { safeParse } from "../../lib/schemas.js";
import { ClosedEnum } from "../../types/enums.js";
import { Result as SafeParseResult } from "../../types/fp.js";
import { smartUnion } from "../../types/smartUnion.js";
import {
  ListResourceWebhookDelivery,
  ListResourceWebhookDelivery$inboundSchema,
} from "../components/listresourcewebhookdelivery.js";
import {
  WebhookEventType,
  WebhookEventType$outboundSchema,
} from "../components/webhookeventtype.js";
import { SDKValidationError } from "../errors/sdkvalidationerror.js";

/**
 * Filter by webhook endpoint ID.
 */
export type EndpointId = string | Array<string>;

/**
 * Filter by HTTP response code class (2xx, 3xx, 4xx, 5xx).
 */
export const HttpCodeClass = {
  Twoxx: "2xx",
  Threexx: "3xx",
  Fourxx: "4xx",
  Fivexx: "5xx",
} as const;
/**
 * Filter by HTTP response code class (2xx, 3xx, 4xx, 5xx).
 */
export type HttpCodeClass = ClosedEnum<typeof HttpCodeClass>;

/**
 * Filter by webhook event type.
 */
export type EventType = WebhookEventType | Array<WebhookEventType>;

export type WebhooksListWebhookDeliveriesRequest = {
  /**
   * Filter by webhook endpoint ID.
   */
  endpointId?: string | Array<string> | null | undefined;
  /**
   * Filter deliveries after this timestamp.
   */
  startTimestamp?: Date | null | undefined;
  /**
   * Filter deliveries before this timestamp.
   */
  endTimestamp?: Date | null | undefined;
  /**
   * Filter by delivery success status.
   */
  succeeded?: boolean | null | undefined;
  /**
   * Query to filter webhook deliveries.
   */
  query?: string | null | undefined;
  /**
   * Filter by HTTP response code class (2xx, 3xx, 4xx, 5xx).
   */
  httpCodeClass?: HttpCodeClass | null | undefined;
  /**
   * Filter by webhook event type.
   */
  eventType?: WebhookEventType | Array<WebhookEventType> | null | undefined;
  /**
   * Page number, defaults to 1.
   */
  page?: number | undefined;
  /**
   * Size of a page, defaults to 10. Maximum is 100.
   */
  limit?: number | undefined;
};

export type WebhooksListWebhookDeliveriesResponse = {
  result: ListResourceWebhookDelivery;
};

/** @internal */
export type EndpointId$Outbound = string | Array<string>;

/** @internal */
export const EndpointId$outboundSchema: z.ZodMiniType<
  EndpointId$Outbound,
  EndpointId
> = smartUnion([z.string(), z.array(z.string())]);

export function endpointIdToJSON(endpointId: EndpointId): string {
  return JSON.stringify(EndpointId$outboundSchema.parse(endpointId));
}

/** @internal */
export const HttpCodeClass$outboundSchema: z.ZodMiniEnum<typeof HttpCodeClass> =
  z.enum(HttpCodeClass);

/** @internal */
export type EventType$Outbound = string | Array<string>;

/** @internal */
export const EventType$outboundSchema: z.ZodMiniType<
  EventType$Outbound,
  EventType
> = smartUnion([
  WebhookEventType$outboundSchema,
  z.array(WebhookEventType$outboundSchema),
]);

export function eventTypeToJSON(eventType: EventType): string {
  return JSON.stringify(EventType$outboundSchema.parse(eventType));
}

/** @internal */
export type WebhooksListWebhookDeliveriesRequest$Outbound = {
  endpoint_id?: string | Array<string> | null | undefined;
  start_timestamp?: string | null | undefined;
  end_timestamp?: string | null | undefined;
  succeeded?: boolean | null | undefined;
  query?: string | null | undefined;
  http_code_class?: string | null | undefined;
  event_type?: string | Array<string> | null | undefined;
  page: number;
  limit: number;
};

/** @internal */
export const WebhooksListWebhookDeliveriesRequest$outboundSchema: z.ZodMiniType<
  WebhooksListWebhookDeliveriesRequest$Outbound,
  WebhooksListWebhookDeliveriesRequest
> = z.pipe(
  z.object({
    endpointId: z.optional(
      z.nullable(smartUnion([z.string(), z.array(z.string())])),
    ),
    startTimestamp: z.optional(
      z.nullable(z.pipe(z.date(), z.transform(v => v.toISOString()))),
    ),
    endTimestamp: z.optional(
      z.nullable(z.pipe(z.date(), z.transform(v => v.toISOString()))),
    ),
    succeeded: z.optional(z.nullable(z.boolean())),
    query: z.optional(z.nullable(z.string())),
    httpCodeClass: z.optional(z.nullable(HttpCodeClass$outboundSchema)),
    eventType: z.optional(
      z.nullable(
        smartUnion([
          WebhookEventType$outboundSchema,
          z.array(WebhookEventType$outboundSchema),
        ]),
      ),
    ),
    page: z._default(z.int(), 1),
    limit: z._default(z.int(), 10),
  }),
  z.transform((v) => {
    return remap$(v, {
      endpointId: "endpoint_id",
      startTimestamp: "start_timestamp",
      endTimestamp: "end_timestamp",
      httpCodeClass: "http_code_class",
      eventType: "event_type",
    });
  }),
);

export function webhooksListWebhookDeliveriesRequestToJSON(
  webhooksListWebhookDeliveriesRequest: WebhooksListWebhookDeliveriesRequest,
): string {
  return JSON.stringify(
    WebhooksListWebhookDeliveriesRequest$outboundSchema.parse(
      webhooksListWebhookDeliveriesRequest,
    ),
  );
}

/** @internal */
export const WebhooksListWebhookDeliveriesResponse$inboundSchema: z.ZodMiniType<
  WebhooksListWebhookDeliveriesResponse,
  unknown
> = z.pipe(
  z.object({
    Result: ListResourceWebhookDelivery$inboundSchema,
  }),
  z.transform((v) => {
    return remap$(v, {
      "Result": "result",
    });
  }),
);

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