/*
 * 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 { Result as SafeParseResult } from "../../types/fp.js";
import { SDKValidationError } from "../errors/sdkvalidationerror.js";
import {
  WebhookEventType,
  WebhookEventType$inboundSchema,
} from "./webhookeventtype.js";
import { WebhookFormat, WebhookFormat$inboundSchema } from "./webhookformat.js";

/**
 * A webhook endpoint.
 */
export type WebhookEndpoint = {
  /**
   * Creation timestamp of the object.
   */
  createdAt: Date;
  /**
   * Last modification timestamp of the object.
   */
  modifiedAt: Date | null;
  /**
   * The ID of the object.
   */
  id: string;
  /**
   * The URL where the webhook events will be sent.
   */
  url: string;
  /**
   * An optional name for the webhook endpoint to help organize and identify it.
   */
  name?: string | null | undefined;
  format: WebhookFormat;
  /**
   * The secret used to sign the webhook events.
   */
  secret: string;
  /**
   * The organization ID associated with the webhook endpoint.
   */
  organizationId: string;
  /**
   * The events that will trigger the webhook.
   */
  events: Array<WebhookEventType>;
  /**
   * Whether the webhook endpoint is enabled and will receive events.
   */
  enabled: boolean;
};

/** @internal */
export const WebhookEndpoint$inboundSchema: z.ZodMiniType<
  WebhookEndpoint,
  unknown
> = z.pipe(
  z.object({
    created_at: z.pipe(
      z.iso.datetime({ offset: true }),
      z.transform(v => new Date(v)),
    ),
    modified_at: z.nullable(
      z.pipe(z.iso.datetime({ offset: true }), z.transform(v => new Date(v))),
    ),
    id: z.string(),
    url: z.string(),
    name: z.optional(z.nullable(z.string())),
    format: WebhookFormat$inboundSchema,
    secret: z.string(),
    organization_id: z.string(),
    events: z.array(WebhookEventType$inboundSchema),
    enabled: z.boolean(),
  }),
  z.transform((v) => {
    return remap$(v, {
      "created_at": "createdAt",
      "modified_at": "modifiedAt",
      "organization_id": "organizationId",
    });
  }),
);

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