/*
 * 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 { SDKValidationError } from "../errors/sdkvalidationerror.js";
import {
  MetadataOutputType,
  MetadataOutputType$inboundSchema,
  MetadataOutputType$Outbound,
  MetadataOutputType$outboundSchema,
} from "./metadataoutputtype.js";
import {
  RefundDispute,
  RefundDispute$inboundSchema,
  RefundDispute$Outbound,
  RefundDispute$outboundSchema,
} from "./refunddispute.js";
import {
  RefundReason,
  RefundReason$inboundSchema,
  RefundReason$outboundSchema,
} from "./refundreason.js";
import {
  RefundStatus,
  RefundStatus$inboundSchema,
  RefundStatus$outboundSchema,
} from "./refundstatus.js";

export type Refund = {
  /**
   * Creation timestamp of the object.
   */
  createdAt: Date;
  /**
   * Last modification timestamp of the object.
   */
  modifiedAt: Date | null;
  /**
   * The ID of the object.
   */
  id: string;
  metadata: { [k: string]: MetadataOutputType };
  status: RefundStatus;
  reason: RefundReason;
  amount: number;
  taxAmount: number;
  currency: string;
  organizationId: string;
  orderId: string;
  subscriptionId: string | null;
  customerId: string;
  revokeBenefits: boolean;
  dispute: RefundDispute | null;
};

/** @internal */
export const Refund$inboundSchema: z.ZodMiniType<Refund, unknown> = z.pipe(
  z.object({
    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))),
    ),
    id: z.string(),
    metadata: z.record(z.string(), MetadataOutputType$inboundSchema),
    status: RefundStatus$inboundSchema,
    reason: RefundReason$inboundSchema,
    amount: z.int(),
    tax_amount: z.int(),
    currency: z.string(),
    organization_id: z.string(),
    order_id: z.string(),
    subscription_id: z.nullable(z.string()),
    customer_id: z.string(),
    revoke_benefits: z.boolean(),
    dispute: z.nullable(RefundDispute$inboundSchema),
  }),
  z.transform((v) => {
    return remap$(v, {
      "created_at": "createdAt",
      "modified_at": "modifiedAt",
      "tax_amount": "taxAmount",
      "organization_id": "organizationId",
      "order_id": "orderId",
      "subscription_id": "subscriptionId",
      "customer_id": "customerId",
      "revoke_benefits": "revokeBenefits",
    });
  }),
);
/** @internal */
export type Refund$Outbound = {
  created_at: string;
  modified_at: string | null;
  id: string;
  metadata: { [k: string]: MetadataOutputType$Outbound };
  status: string;
  reason: string;
  amount: number;
  tax_amount: number;
  currency: string;
  organization_id: string;
  order_id: string;
  subscription_id: string | null;
  customer_id: string;
  revoke_benefits: boolean;
  dispute: RefundDispute$Outbound | null;
};

/** @internal */
export const Refund$outboundSchema: z.ZodMiniType<Refund$Outbound, Refund> = z
  .pipe(
    z.object({
      createdAt: z.pipe(z.date(), z.transform(v => v.toISOString())),
      modifiedAt: z.nullable(
        z.pipe(z.date(), z.transform(v => v.toISOString())),
      ),
      id: z.string(),
      metadata: z.record(z.string(), MetadataOutputType$outboundSchema),
      status: RefundStatus$outboundSchema,
      reason: RefundReason$outboundSchema,
      amount: z.int(),
      taxAmount: z.int(),
      currency: z.string(),
      organizationId: z.string(),
      orderId: z.string(),
      subscriptionId: z.nullable(z.string()),
      customerId: z.string(),
      revokeBenefits: z.boolean(),
      dispute: z.nullable(RefundDispute$outboundSchema),
    }),
    z.transform((v) => {
      return remap$(v, {
        createdAt: "created_at",
        modifiedAt: "modified_at",
        taxAmount: "tax_amount",
        organizationId: "organization_id",
        orderId: "order_id",
        subscriptionId: "subscription_id",
        customerId: "customer_id",
        revokeBenefits: "revoke_benefits",
      });
    }),
  );

export function refundToJSON(refund: Refund): string {
  return JSON.stringify(Refund$outboundSchema.parse(refund));
}
export function refundFromJSON(
  jsonString: string,
): SafeParseResult<Refund, SDKValidationError> {
  return safeParse(
    jsonString,
    (x) => Refund$inboundSchema.parse(JSON.parse(x)),
    `Failed to parse 'Refund' from JSON`,
  );
}
