/*
 * 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 {
  AttachedCustomField,
  AttachedCustomField$inboundSchema,
  AttachedCustomField$Outbound,
  AttachedCustomField$outboundSchema,
} from "./attachedcustomfield.js";
import {
  Benefit,
  Benefit$inboundSchema,
  Benefit$Outbound,
  Benefit$outboundSchema,
} from "./benefit.js";
import {
  LegacyRecurringProductPrice,
  LegacyRecurringProductPrice$inboundSchema,
  LegacyRecurringProductPrice$Outbound,
  LegacyRecurringProductPrice$outboundSchema,
} from "./legacyrecurringproductprice.js";
import {
  MetadataOutputType,
  MetadataOutputType$inboundSchema,
  MetadataOutputType$Outbound,
  MetadataOutputType$outboundSchema,
} from "./metadataoutputtype.js";
import {
  ProductMediaFileRead,
  ProductMediaFileRead$inboundSchema,
  ProductMediaFileRead$Outbound,
  ProductMediaFileRead$outboundSchema,
} from "./productmediafileread.js";
import {
  ProductPrice,
  ProductPrice$inboundSchema,
  ProductPrice$Outbound,
  ProductPrice$outboundSchema,
} from "./productprice.js";
import {
  ProductVisibility,
  ProductVisibility$inboundSchema,
  ProductVisibility$outboundSchema,
} from "./productvisibility.js";
import {
  RecurringInterval,
  RecurringInterval$inboundSchema,
  RecurringInterval$outboundSchema,
} from "./recurringinterval.js";
import {
  TrialInterval,
  TrialInterval$inboundSchema,
  TrialInterval$outboundSchema,
} from "./trialinterval.js";

export type Prices = LegacyRecurringProductPrice | ProductPrice;

/**
 * A product.
 */
export type Product = {
  /**
   * 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;
  metadata: { [k: string]: MetadataOutputType };
  /**
   * List of prices for this product.
   */
  prices: Array<LegacyRecurringProductPrice | ProductPrice>;
  /**
   * List of benefits granted by the product.
   */
  benefits: Array<Benefit>;
  /**
   * List of medias associated to the product.
   */
  medias: Array<ProductMediaFileRead>;
  /**
   * List of custom fields attached to the product.
   */
  attachedCustomFields: Array<AttachedCustomField>;
};

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

/** @internal */
export const Prices$outboundSchema: z.ZodMiniType<Prices$Outbound, Prices> =
  smartUnion([
    LegacyRecurringProductPrice$outboundSchema,
    ProductPrice$outboundSchema,
  ]);

export function pricesToJSON(prices: Prices): string {
  return JSON.stringify(Prices$outboundSchema.parse(prices));
}
export function pricesFromJSON(
  jsonString: string,
): SafeParseResult<Prices, SDKValidationError> {
  return safeParse(
    jsonString,
    (x) => Prices$inboundSchema.parse(JSON.parse(x)),
    `Failed to parse 'Prices' from JSON`,
  );
}

/** @internal */
export const Product$inboundSchema: z.ZodMiniType<Product, 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(),
    metadata: z.record(z.string(), MetadataOutputType$inboundSchema),
    prices: z.array(
      smartUnion([
        LegacyRecurringProductPrice$inboundSchema,
        ProductPrice$inboundSchema,
      ]),
    ),
    benefits: z.array(Benefit$inboundSchema),
    medias: z.array(ProductMediaFileRead$inboundSchema),
    attached_custom_fields: z.array(AttachedCustomField$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",
      "attached_custom_fields": "attachedCustomFields",
    });
  }),
);
/** @internal */
export type Product$Outbound = {
  id: string;
  created_at: string;
  modified_at: string | null;
  trial_interval: string | null;
  trial_interval_count: number | null;
  name: string;
  description: string | null;
  visibility: string;
  recurring_interval: string | null;
  recurring_interval_count: number | null;
  meter_interval: string | null;
  meter_interval_count: number | null;
  is_recurring: boolean;
  is_archived: boolean;
  organization_id: string;
  metadata: { [k: string]: MetadataOutputType$Outbound };
  prices: Array<LegacyRecurringProductPrice$Outbound | ProductPrice$Outbound>;
  benefits: Array<Benefit$Outbound>;
  medias: Array<ProductMediaFileRead$Outbound>;
  attached_custom_fields: Array<AttachedCustomField$Outbound>;
};

/** @internal */
export const Product$outboundSchema: z.ZodMiniType<Product$Outbound, Product> =
  z.pipe(
    z.object({
      id: z.string(),
      createdAt: z.pipe(z.date(), z.transform(v => v.toISOString())),
      modifiedAt: z.nullable(
        z.pipe(z.date(), z.transform(v => v.toISOString())),
      ),
      trialInterval: z.nullable(TrialInterval$outboundSchema),
      trialIntervalCount: z.nullable(z.int()),
      name: z.string(),
      description: z.nullable(z.string()),
      visibility: ProductVisibility$outboundSchema,
      recurringInterval: z.nullable(RecurringInterval$outboundSchema),
      recurringIntervalCount: z.nullable(z.int()),
      meterInterval: z.nullable(RecurringInterval$outboundSchema),
      meterIntervalCount: z.nullable(z.int()),
      isRecurring: z.boolean(),
      isArchived: z.boolean(),
      organizationId: z.string(),
      metadata: z.record(z.string(), MetadataOutputType$outboundSchema),
      prices: z.array(
        smartUnion([
          LegacyRecurringProductPrice$outboundSchema,
          ProductPrice$outboundSchema,
        ]),
      ),
      benefits: z.array(Benefit$outboundSchema),
      medias: z.array(ProductMediaFileRead$outboundSchema),
      attachedCustomFields: z.array(AttachedCustomField$outboundSchema),
    }),
    z.transform((v) => {
      return remap$(v, {
        createdAt: "created_at",
        modifiedAt: "modified_at",
        trialInterval: "trial_interval",
        trialIntervalCount: "trial_interval_count",
        recurringInterval: "recurring_interval",
        recurringIntervalCount: "recurring_interval_count",
        meterInterval: "meter_interval",
        meterIntervalCount: "meter_interval_count",
        isRecurring: "is_recurring",
        isArchived: "is_archived",
        organizationId: "organization_id",
        attachedCustomFields: "attached_custom_fields",
      });
    }),
  );

export function productToJSON(product: Product): string {
  return JSON.stringify(Product$outboundSchema.parse(product));
}
export function productFromJSON(
  jsonString: string,
): SafeParseResult<Product, SDKValidationError> {
  return safeParse(
    jsonString,
    (x) => Product$inboundSchema.parse(JSON.parse(x)),
    `Failed to parse 'Product' from JSON`,
  );
}
