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

import { stringOrNull } from "../../nullable-defaults";

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: stringOrNull,
  cashier_url: stringOrNull,
  checkoutUrl: stringOrNull,
  checkout_url: stringOrNull,
  paymentUrl: stringOrNull,
  payment_url: stringOrNull,
  approvalUrl: stringOrNull,
  approval_url: stringOrNull,
  redirectUrl: stringOrNull,
  redirect_url: stringOrNull,
  url: stringOrNull,
}).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
>;
