/*
 * 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 { BenefitPublic, BenefitPublic$inboundSchema } from "./benefitpublic.js";
import {
  CustomerOrganization,
  CustomerOrganization$inboundSchema,
} from "./customerorganization.js";
import {
  LegacyRecurringProductPrice,
  LegacyRecurringProductPrice$inboundSchema,
} from "./legacyrecurringproductprice.js";
import {
  ProductMediaFileRead,
  ProductMediaFileRead$inboundSchema,
} from "./productmediafileread.js";
import { ProductPrice, ProductPrice$inboundSchema } from "./productprice.js";
import {
  ProductVisibility,
  ProductVisibility$inboundSchema,
} from "./productvisibility.js";
import {
  RecurringInterval,
  RecurringInterval$inboundSchema,
} from "./recurringinterval.js";
import { TrialInterval, TrialInterval$inboundSchema } from "./trialinterval.js";

export type CustomerOrderProductPrices =
  | LegacyRecurringProductPrice
  | ProductPrice;

export type CustomerOrderProduct = {
  /**
   * 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;
  /**
   * The name of the product.
   */
  name: string;
  /**
   * The description of the product.
   */
  description: string | null;
  visibility: ProductVisibility;
  /**
   * The recurring interval of the product. If `None`, the product is a one-time purchase.
   */
  recurringInterval: RecurringInterval | null;
  /**
   * Number of interval units of the subscription. If this is set to 1 the charge will happen every interval (e.g. every month), if set to 2 it will be every other month, and so on. None for one-time products.
   */
  recurringIntervalCount: number | null;
  /**
   * The meter cycle of the product, independent of the billing interval. If `None`, metered concerns follow the billing interval.
   */
  meterInterval: RecurringInterval | null;
  /**
   * Number of meter interval units. None when no meter cycle is set.
   */
  meterIntervalCount: number | null;
  /**
   * Whether the product is a subscription.
   */
  isRecurring: boolean;
  /**
   * Whether the product is archived and no longer available.
   */
  isArchived: boolean;
  /**
   * The ID of the organization owning the product.
   */
  organizationId: string;
  /**
   * List of prices for this product.
   */
  prices: Array<LegacyRecurringProductPrice | ProductPrice>;
  /**
   * List of benefits granted by the product.
   */
  benefits: Array<BenefitPublic>;
  /**
   * List of medias associated to the product.
   */
  medias: Array<ProductMediaFileRead>;
  organization: CustomerOrganization;
};

/** @internal */
export const CustomerOrderProductPrices$inboundSchema: z.ZodMiniType<
  CustomerOrderProductPrices,
  unknown
> = smartUnion([
  LegacyRecurringProductPrice$inboundSchema,
  ProductPrice$inboundSchema,
]);

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

/** @internal */
export const CustomerOrderProduct$inboundSchema: z.ZodMiniType<
  CustomerOrderProduct,
  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()),
    name: z.string(),
    description: z.nullable(z.string()),
    visibility: ProductVisibility$inboundSchema,
    recurring_interval: z.nullable(RecurringInterval$inboundSchema),
    recurring_interval_count: z.nullable(z.int()),
    meter_interval: z.nullable(RecurringInterval$inboundSchema),
    meter_interval_count: z.nullable(z.int()),
    is_recurring: z.boolean(),
    is_archived: z.boolean(),
    organization_id: z.string(),
    prices: z.array(
      smartUnion([
        LegacyRecurringProductPrice$inboundSchema,
        ProductPrice$inboundSchema,
      ]),
    ),
    benefits: z.array(BenefitPublic$inboundSchema),
    medias: z.array(ProductMediaFileRead$inboundSchema),
    organization: CustomerOrganization$inboundSchema,
  }),
  z.transform((v) => {
    return remap$(v, {
      "created_at": "createdAt",
      "modified_at": "modifiedAt",
      "trial_interval": "trialInterval",
      "trial_interval_count": "trialIntervalCount",
      "recurring_interval": "recurringInterval",
      "recurring_interval_count": "recurringIntervalCount",
      "meter_interval": "meterInterval",
      "meter_interval_count": "meterIntervalCount",
      "is_recurring": "isRecurring",
      "is_archived": "isArchived",
      "organization_id": "organizationId",
    });
  }),
);

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