/*
 * 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 { smartUnion } from "../../types/smartUnion.js";
import { SDKValidationError } from "../errors/sdkvalidationerror.js";
import {
  CheckoutLinkProduct,
  CheckoutLinkProduct$inboundSchema,
} from "./checkoutlinkproduct.js";
import {
  DiscountFixedOnceForeverDurationBase,
  DiscountFixedOnceForeverDurationBase$inboundSchema,
} from "./discountfixedonceforeverdurationbase.js";
import {
  DiscountFixedRepeatDurationBase,
  DiscountFixedRepeatDurationBase$inboundSchema,
} from "./discountfixedrepeatdurationbase.js";
import {
  DiscountPercentageOnceForeverDurationBase,
  DiscountPercentageOnceForeverDurationBase$inboundSchema,
} from "./discountpercentageonceforeverdurationbase.js";
import {
  DiscountPercentageRepeatDurationBase,
  DiscountPercentageRepeatDurationBase$inboundSchema,
} from "./discountpercentagerepeatdurationbase.js";
import {
  MetadataOutputType,
  MetadataOutputType$inboundSchema,
} from "./metadataoutputtype.js";
import {
  PaymentProcessor,
  PaymentProcessor$inboundSchema,
} from "./paymentprocessor.js";
import { TrialInterval, TrialInterval$inboundSchema } from "./trialinterval.js";

export type CheckoutLinkDiscount =
  | DiscountFixedRepeatDurationBase
  | DiscountFixedOnceForeverDurationBase
  | DiscountPercentageRepeatDurationBase
  | DiscountPercentageOnceForeverDurationBase;

/**
 * Checkout link data.
 */
export type CheckoutLink = {
  /**
   * The ID of the object.
   */
  id: string;
  /**
   * Creation timestamp of the object.
   */
  createdAt: Date;
  /**
   * Last modification timestamp of the object.
   */
  modifiedAt: Date | null;
  /**
   * The interval unit for the trial period.
   */
  trialInterval: TrialInterval | null;
  /**
   * The number of interval units for the trial period.
   */
  trialIntervalCount: number | null;
  metadata: { [k: string]: MetadataOutputType };
  paymentProcessor: PaymentProcessor;
  /**
   * Client secret used to access the checkout link.
   */
  clientSecret: string;
  /**
   * URL where the customer will be redirected after a successful payment.
   */
  successUrl: string | null;
  /**
   * When set, a back button will be shown in the checkout to return to this URL.
   */
  returnUrl: string | null;
  /**
   * Optional label to distinguish links internally
   */
  label: string | null;
  /**
   * Whether to allow the customer to apply discount codes. If you apply a discount through `discount_id`, it'll still be applied, but the customer won't be able to change it.
   */
  allowDiscountCodes: boolean;
  /**
   * Whether to require the customer to fill their full billing address, instead of just the country. Customers in the US will always be required to fill their full address, regardless of this setting.
   */
  requireBillingAddress: boolean;
  /**
   * ID of the discount to apply to the checkout. If the discount is not applicable anymore when opening the checkout link, it'll be ignored.
   */
  discountId: string | null;
  /**
   * Preconfigured number of seats for seat-based pricing. When set, checkout sessions created from this link are locked to this number of seats and the customer won't be able to change it. All products on the link must use seat-based pricing and allow this number of seats. If the products no longer accommodate this value when the link is opened, it'll be ignored.
   */
  seats: number | null;
  /**
   * The organization ID.
   */
  organizationId: string;
  products: Array<CheckoutLinkProduct>;
  discount:
    | DiscountFixedRepeatDurationBase
    | DiscountFixedOnceForeverDurationBase
    | DiscountPercentageRepeatDurationBase
    | DiscountPercentageOnceForeverDurationBase
    | null;
  url: string;
};

/** @internal */
export const CheckoutLinkDiscount$inboundSchema: z.ZodMiniType<
  CheckoutLinkDiscount,
  unknown
> = smartUnion([
  DiscountFixedRepeatDurationBase$inboundSchema,
  DiscountFixedOnceForeverDurationBase$inboundSchema,
  DiscountPercentageRepeatDurationBase$inboundSchema,
  DiscountPercentageOnceForeverDurationBase$inboundSchema,
]);

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

/** @internal */
export const CheckoutLink$inboundSchema: z.ZodMiniType<CheckoutLink, unknown> =
  z.pipe(
    z.object({
      id: z.string(),
      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))),
      ),
      trial_interval: z.nullable(TrialInterval$inboundSchema),
      trial_interval_count: z.nullable(z.int()),
      metadata: z.record(z.string(), MetadataOutputType$inboundSchema),
      payment_processor: PaymentProcessor$inboundSchema,
      client_secret: z.string(),
      success_url: z.nullable(z.string()),
      return_url: z.nullable(z.string()),
      label: z.nullable(z.string()),
      allow_discount_codes: z.boolean(),
      require_billing_address: z.boolean(),
      discount_id: z.nullable(z.string()),
      seats: z.nullable(z.int()),
      organization_id: z.string(),
      products: z.array(CheckoutLinkProduct$inboundSchema),
      discount: z.nullable(
        smartUnion([
          DiscountFixedRepeatDurationBase$inboundSchema,
          DiscountFixedOnceForeverDurationBase$inboundSchema,
          DiscountPercentageRepeatDurationBase$inboundSchema,
          DiscountPercentageOnceForeverDurationBase$inboundSchema,
        ]),
      ),
      url: z.string(),
    }),
    z.transform((v) => {
      return remap$(v, {
        "created_at": "createdAt",
        "modified_at": "modifiedAt",
        "trial_interval": "trialInterval",
        "trial_interval_count": "trialIntervalCount",
        "payment_processor": "paymentProcessor",
        "client_secret": "clientSecret",
        "success_url": "successUrl",
        "return_url": "returnUrl",
        "allow_discount_codes": "allowDiscountCodes",
        "require_billing_address": "requireBillingAddress",
        "discount_id": "discountId",
        "organization_id": "organizationId",
      });
    }),
  );

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