/*
 * 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 {
  CustomerState,
  CustomerState$inboundSchema,
  CustomerState$Outbound,
  CustomerState$outboundSchema,
} from "./customerstate.js";

/**
 * Sent when a customer state has changed.
 *
 * @remarks
 *
 * It's triggered when:
 *
 * * Customer is created, updated or deleted.
 * * A subscription is created or updated.
 * * A benefit is granted or revoked.
 *
 * **Discord & Slack support:** Basic
 */
export type WebhookCustomerStateChangedPayload = {
  type: "customer.state_changed";
  timestamp: Date;
  data: CustomerState;
};

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

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

export function webhookCustomerStateChangedPayloadToJSON(
  webhookCustomerStateChangedPayload: WebhookCustomerStateChangedPayload,
): string {
  return JSON.stringify(
    WebhookCustomerStateChangedPayload$outboundSchema.parse(
      webhookCustomerStateChangedPayload,
    ),
  );
}
export function webhookCustomerStateChangedPayloadFromJSON(
  jsonString: string,
): SafeParseResult<WebhookCustomerStateChangedPayload, SDKValidationError> {
  return safeParse(
    jsonString,
    (x) =>
      WebhookCustomerStateChangedPayload$inboundSchema.parse(JSON.parse(x)),
    `Failed to parse 'WebhookCustomerStateChangedPayload' from JSON`,
  );
}
