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

/**
 * Sent when a new subscription is created.
 *
 * @remarks
 *
 * When this event occurs, the subscription `status` might not be `active` yet, as we can still have to wait for the first payment to be processed.
 *
 * **Discord & Slack support:** Full
 */
export type WebhookSubscriptionCreatedPayload = {
  type: "subscription.created";
  timestamp: Date;
  data: Subscription;
};

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

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

export function webhookSubscriptionCreatedPayloadToJSON(
  webhookSubscriptionCreatedPayload: WebhookSubscriptionCreatedPayload,
): string {
  return JSON.stringify(
    WebhookSubscriptionCreatedPayload$outboundSchema.parse(
      webhookSubscriptionCreatedPayload,
    ),
  );
}
export function webhookSubscriptionCreatedPayloadFromJSON(
  jsonString: string,
): SafeParseResult<WebhookSubscriptionCreatedPayload, SDKValidationError> {
  return safeParse(
    jsonString,
    (x) => WebhookSubscriptionCreatedPayload$inboundSchema.parse(JSON.parse(x)),
    `Failed to parse 'WebhookSubscriptionCreatedPayload' from JSON`,
  );
}
