/*
 * 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 { smartUnion } from "../../types/smartUnion.js";
import {
  DiscountDuration,
  DiscountDuration$outboundSchema,
} from "./discountduration.js";
import { DiscountType, DiscountType$outboundSchema } from "./discounttype.js";
import {
  PresentmentCurrency,
  PresentmentCurrency$outboundSchema,
} from "./presentmentcurrency.js";

export type DiscountUpdateMetadata = string | number | number | boolean;

/**
 * Schema to update a discount.
 */
export type DiscountUpdate = {
  /**
   * Key-value object allowing you to store additional information.
   *
   * @remarks
   *
   * The key must be a string with a maximum length of **40 characters**.
   * The value must be either:
   *
   * * A string with a maximum length of **500 characters**
   * * An integer
   * * A floating-point number
   * * A boolean
   *
   * You can store up to **50 key-value pairs**.
   */
  metadata?: { [k: string]: string | number | number | boolean } | undefined;
  name?: string | null | undefined;
  /**
   * Code customers can use to apply the discount during checkout. Must be between 3 and 256 characters long and contain only alphanumeric characters.If not provided, the discount can only be applied via the API.
   */
  code?: string | null | undefined;
  /**
   * Optional timestamp after which the discount is redeemable.
   */
  startsAt?: Date | null | undefined;
  /**
   * Optional timestamp after which the discount is no longer redeemable.
   */
  endsAt?: Date | null | undefined;
  /**
   * Optional maximum number of times the discount can be redeemed.
   */
  maxRedemptions?: number | null | undefined;
  duration?: DiscountDuration | null | undefined;
  durationInMonths?: number | null | undefined;
  type?: DiscountType | null | undefined;
  /**
   * @deprecated field: This will be removed in a future release, please migrate away from it as soon as possible.
   */
  amount?: number | null | undefined;
  /**
   * @deprecated field: This will be removed in a future release, please migrate away from it as soon as possible.
   */
  currency?: PresentmentCurrency | null | undefined;
  amounts?: { [k: string]: number } | null | undefined;
  basisPoints?: number | null | undefined;
  products?: Array<string> | null | undefined;
};

/** @internal */
export type DiscountUpdateMetadata$Outbound =
  | string
  | number
  | number
  | boolean;

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

export function discountUpdateMetadataToJSON(
  discountUpdateMetadata: DiscountUpdateMetadata,
): string {
  return JSON.stringify(
    DiscountUpdateMetadata$outboundSchema.parse(discountUpdateMetadata),
  );
}

/** @internal */
export type DiscountUpdate$Outbound = {
  metadata?: { [k: string]: string | number | number | boolean } | undefined;
  name?: string | null | undefined;
  code?: string | null | undefined;
  starts_at?: string | null | undefined;
  ends_at?: string | null | undefined;
  max_redemptions?: number | null | undefined;
  duration?: string | null | undefined;
  duration_in_months?: number | null | undefined;
  type?: string | null | undefined;
  amount?: number | null | undefined;
  currency?: string | null | undefined;
  amounts?: { [k: string]: number } | null | undefined;
  basis_points?: number | null | undefined;
  products?: Array<string> | null | undefined;
};

/** @internal */
export const DiscountUpdate$outboundSchema: z.ZodMiniType<
  DiscountUpdate$Outbound,
  DiscountUpdate
> = z.pipe(
  z.object({
    metadata: z.optional(
      z.record(
        z.string(),
        smartUnion([z.string(), z.int(), z.number(), z.boolean()]),
      ),
    ),
    name: z.optional(z.nullable(z.string())),
    code: z.optional(z.nullable(z.string())),
    startsAt: z.optional(
      z.nullable(z.pipe(z.date(), z.transform(v => v.toISOString()))),
    ),
    endsAt: z.optional(
      z.nullable(z.pipe(z.date(), z.transform(v => v.toISOString()))),
    ),
    maxRedemptions: z.optional(z.nullable(z.int())),
    duration: z.optional(z.nullable(DiscountDuration$outboundSchema)),
    durationInMonths: z.optional(z.nullable(z.int())),
    type: z.optional(z.nullable(DiscountType$outboundSchema)),
    amount: z.optional(z.nullable(z.int())),
    currency: z.optional(z.nullable(PresentmentCurrency$outboundSchema)),
    amounts: z.optional(z.nullable(z.record(z.string(), z.int()))),
    basisPoints: z.optional(z.nullable(z.int())),
    products: z.optional(z.nullable(z.array(z.string()))),
  }),
  z.transform((v) => {
    return remap$(v, {
      startsAt: "starts_at",
      endsAt: "ends_at",
      maxRedemptions: "max_redemptions",
      durationInMonths: "duration_in_months",
      basisPoints: "basis_points",
    });
  }),
);

export function discountUpdateToJSON(discountUpdate: DiscountUpdate): string {
  return JSON.stringify(DiscountUpdate$outboundSchema.parse(discountUpdate));
}
