/**
 * 创建支付订单响应
 */
import { z } from "zod";

const paymentUrlKeys = [
  "cashierUrl",
  "cashier_url",
  "checkoutUrl",
  "checkout_url",
  "paymentUrl",
  "payment_url",
  "approvalUrl",
  "approval_url",
  "redirectUrl",
  "redirect_url",
  "url",
] as const;

export const CreatePaymentOrderResponseSchema = z.object({
  orderId: z.string(),
  payParams: z.record(z.string(), z.unknown()),
  cashierUrl: z.string().optional(),
  cashier_url: z.string().optional(),
  checkoutUrl: z.string().optional(),
  checkout_url: z.string().optional(),
  paymentUrl: z.string().optional(),
  payment_url: z.string().optional(),
  approvalUrl: z.string().optional(),
  approval_url: z.string().optional(),
  redirectUrl: z.string().optional(),
  redirect_url: z.string().optional(),
  url: z.string().optional(),
}).transform((data) => {
  const payParams = { ...data.payParams };

  for (const key of paymentUrlKeys) {
    const value = data[key];
    if (typeof value === "string" && value.length > 0 && !(key in payParams)) {
      payParams[key] = value;
    }
  }

  return {
    orderId: data.orderId,
    payParams,
  };
});

export type CreatePaymentOrderResponseInput = z.input<
  typeof CreatePaymentOrderResponseSchema
>;
export type CreatePaymentOrderResponseData = z.output<
  typeof CreatePaymentOrderResponseSchema
>;
