/*
 * 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 {
  DisputeStatus,
  DisputeStatus$inboundSchema,
  DisputeStatus$outboundSchema,
} from "./disputestatus.js";

/**
 * Dispute associated with a refund,
 *
 * @remarks
 * in case we prevented a dispute by issuing a refund.
 */
export type RefundDispute = {
  /**
   * Creation timestamp of the object.
   */
  createdAt: Date;
  /**
   * Last modification timestamp of the object.
   */
  modifiedAt: Date | null;
  /**
   * The ID of the object.
   */
  id: string;
  status: DisputeStatus;
  /**
   * Whether the dispute has been resolved (won or lost).
   */
  resolved: boolean;
  /**
   * Whether the dispute is closed (prevented, won, or lost).
   */
  closed: boolean;
  /**
   * Amount in cents disputed.
   */
  amount: number;
  /**
   * Tax amount in cents disputed.
   */
  taxAmount: number;
  /**
   * Currency code of the dispute.
   */
  currency: string;
  /**
   * The reason for the dispute as reported by the card network (e.g. `fraudulent`, `product_not_received`). `None` until the processor reports it.
   */
  reason: string | null;
  /**
   * Deadline to submit evidence in response to the dispute. `None` when no response is required.
   */
  evidenceDueBy: Date | null;
  /**
   * Whether the evidence submission deadline has passed.
   */
  pastDue: boolean;
  /**
   * The ID of the order associated with the dispute.
   */
  orderId: string;
  /**
   * The ID of the payment associated with the dispute.
   */
  paymentId: string;
};

/** @internal */
export const RefundDispute$inboundSchema: z.ZodMiniType<
  RefundDispute,
  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(),
    status: DisputeStatus$inboundSchema,
    resolved: z.boolean(),
    closed: z.boolean(),
    amount: z.int(),
    tax_amount: z.int(),
    currency: z.string(),
    reason: z.nullable(z.string()),
    evidence_due_by: z.nullable(
      z.pipe(z.iso.datetime({ offset: true }), z.transform(v => new Date(v))),
    ),
    past_due: z.boolean(),
    order_id: z.string(),
    payment_id: z.string(),
  }),
  z.transform((v) => {
    return remap$(v, {
      "created_at": "createdAt",
      "modified_at": "modifiedAt",
      "tax_amount": "taxAmount",
      "evidence_due_by": "evidenceDueBy",
      "past_due": "pastDue",
      "order_id": "orderId",
      "payment_id": "paymentId",
    });
  }),
);
/** @internal */
export type RefundDispute$Outbound = {
  created_at: string;
  modified_at: string | null;
  id: string;
  status: string;
  resolved: boolean;
  closed: boolean;
  amount: number;
  tax_amount: number;
  currency: string;
  reason: string | null;
  evidence_due_by: string | null;
  past_due: boolean;
  order_id: string;
  payment_id: string;
};

/** @internal */
export const RefundDispute$outboundSchema: z.ZodMiniType<
  RefundDispute$Outbound,
  RefundDispute
> = 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(),
    status: DisputeStatus$outboundSchema,
    resolved: z.boolean(),
    closed: z.boolean(),
    amount: z.int(),
    taxAmount: z.int(),
    currency: z.string(),
    reason: z.nullable(z.string()),
    evidenceDueBy: z.nullable(
      z.pipe(z.date(), z.transform(v => v.toISOString())),
    ),
    pastDue: z.boolean(),
    orderId: z.string(),
    paymentId: z.string(),
  }),
  z.transform((v) => {
    return remap$(v, {
      createdAt: "created_at",
      modifiedAt: "modified_at",
      taxAmount: "tax_amount",
      evidenceDueBy: "evidence_due_by",
      pastDue: "past_due",
      orderId: "order_id",
      paymentId: "payment_id",
    });
  }),
);

export function refundDisputeToJSON(refundDispute: RefundDispute): string {
  return JSON.stringify(RefundDispute$outboundSchema.parse(refundDispute));
}
export function refundDisputeFromJSON(
  jsonString: string,
): SafeParseResult<RefundDispute, SDKValidationError> {
  return safeParse(
    jsonString,
    (x) => RefundDispute$inboundSchema.parse(JSON.parse(x)),
    `Failed to parse 'RefundDispute' from JSON`,
  );
}
