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

import * as z from "zod/v4-mini";
import { safeParse } from "../../lib/schemas.js";
import { Result as SafeParseResult } from "../../types/fp.js";
import { SDKValidationError } from "../errors/sdkvalidationerror.js";
import {
  Customer,
  Customer$inboundSchema,
  Customer$Outbound,
  Customer$outboundSchema,
} from "./customer.js";

/**
 * Sent when a new customer is created.
 *
 * @remarks
 *
 * A customer can be created:
 *
 * * After a successful checkout.
 * * Programmatically via the API.
 *
 * **Discord & Slack support:** Basic
 */
export type WebhookCustomerCreatedPayload = {
  type: "customer.created";
  timestamp: Date;
  data: Customer;
};

/** @internal */
export const WebhookCustomerCreatedPayload$inboundSchema: z.ZodMiniType<
  WebhookCustomerCreatedPayload,
  unknown
> = z.object({
  type: z.literal("customer.created"),
  timestamp: z.pipe(
    z.iso.datetime({ offset: true }),
    z.transform(v => new Date(v)),
  ),
  data: Customer$inboundSchema,
});
/** @internal */
export type WebhookCustomerCreatedPayload$Outbound = {
  type: "customer.created";
  timestamp: string;
  data: Customer$Outbound;
};

/** @internal */
export const WebhookCustomerCreatedPayload$outboundSchema: z.ZodMiniType<
  WebhookCustomerCreatedPayload$Outbound,
  WebhookCustomerCreatedPayload
> = z.object({
  type: z.literal("customer.created"),
  timestamp: z.pipe(z.date(), z.transform(v => v.toISOString())),
  data: Customer$outboundSchema,
});

export function webhookCustomerCreatedPayloadToJSON(
  webhookCustomerCreatedPayload: WebhookCustomerCreatedPayload,
): string {
  return JSON.stringify(
    WebhookCustomerCreatedPayload$outboundSchema.parse(
      webhookCustomerCreatedPayload,
    ),
  );
}
export function webhookCustomerCreatedPayloadFromJSON(
  jsonString: string,
): SafeParseResult<WebhookCustomerCreatedPayload, SDKValidationError> {
  return safeParse(
    jsonString,
    (x) => WebhookCustomerCreatedPayload$inboundSchema.parse(JSON.parse(x)),
    `Failed to parse 'WebhookCustomerCreatedPayload' from JSON`,
  );
}
