import { requestKuaishouIndustryOpenApi } from './openapi-client.js'
import {
  normalizeOpenApiInteger,
  normalizeOpenApiLong,
  normalizeOpenApiPositiveInteger,
  normalizeStringList,
  pickDefinedBizParams,
  type JsonObject,
} from './openapi-values.js'

export type KuaishouIndustryRefundListInput = {
  sellerId?: string
  beginTime?: unknown
  endTime?: unknown
  type?: unknown
  pageSize?: unknown
  currentPage?: unknown
  sort?: unknown
  queryType?: unknown
  negotiateStatus?: unknown
  pcursor?: unknown
  status?: unknown
  option?: JsonObject
  orderId?: unknown
}

export type KuaishouIndustryRefundApproveInput = {
  sellerId?: string
  refundId?: unknown
  desc?: unknown
  refundAmount?: unknown
  status?: unknown
  negotiateStatus?: unknown
  refundHandingWay?: unknown
}

export type KuaishouIndustryRefundDisagreeInput = {
  sellerId?: string
  refundId?: unknown
  sellerDisagreeReason?: unknown
  sellerDisagreeDesc?: unknown
  sellerDisagreeImages?: unknown
  status?: unknown
  negotiateStatus?: unknown
}

export function listKuaishouIndustryRefunds(input: KuaishouIndustryRefundListInput = {}) {
  const bizParams = pickDefinedBizParams({
    beginTime: normalizeOpenApiLong(input.beginTime),
    endTime: normalizeOpenApiLong(input.endTime),
    type: normalizeOpenApiInteger(input.type, 8),
    pageSize: normalizeOpenApiPositiveInteger(input.pageSize, 50),
    currentPage: normalizeOpenApiPositiveInteger(input.currentPage, 1),
    sort:
      input.sort === undefined || input.sort === ''
        ? undefined
        : normalizeOpenApiInteger(input.sort, 1),
    queryType:
      input.queryType === undefined || input.queryType === ''
        ? undefined
        : normalizeOpenApiInteger(input.queryType, 1),
    negotiateStatus:
      input.negotiateStatus === undefined || input.negotiateStatus === ''
        ? undefined
        : normalizeOpenApiInteger(input.negotiateStatus, 0),
    pcursor: String(input.pcursor ?? ''),
    status:
      input.status === undefined || input.status === ''
        ? undefined
        : normalizeOpenApiInteger(input.status, 0),
    option: input.option && typeof input.option === 'object' ? input.option : undefined,
    orderId: normalizeOpenApiLong(input.orderId),
  })

  return requestKuaishouIndustryOpenApi({
    apiMethod: 'open.seller.order.refund.pcursor.list',
    path: '/open/seller/order/refund/pcursor/list',
    httpMethod: 'GET',
    bizParams,
    ...(input.sellerId ? { sellerId: String(input.sellerId).trim() } : {}),
  })
}

export function approveKuaishouIndustryRefund(input: KuaishouIndustryRefundApproveInput = {}) {
  const bizParams = pickDefinedBizParams({
    refundId: normalizeOpenApiLong(input.refundId),
    desc: String(input.desc ?? '').trim(),
    refundAmount: normalizeOpenApiLong(input.refundAmount),
    status:
      input.status === undefined || input.status === ''
        ? undefined
        : normalizeOpenApiInteger(input.status, 0),
    negotiateStatus:
      input.negotiateStatus === undefined || input.negotiateStatus === ''
        ? undefined
        : normalizeOpenApiInteger(input.negotiateStatus, 0),
    refundHandingWay:
      input.refundHandingWay === undefined || input.refundHandingWay === ''
        ? undefined
        : normalizeOpenApiInteger(input.refundHandingWay, 0),
  })

  return requestKuaishouIndustryOpenApi({
    apiMethod: 'open.seller.order.refund.approve',
    path: '/open/seller/order/refund/approve',
    bizParams,
    ...(input.sellerId ? { sellerId: String(input.sellerId).trim() } : {}),
  })
}

export function disagreeKuaishouIndustryRefund(input: KuaishouIndustryRefundDisagreeInput = {}) {
  const bizParams = pickDefinedBizParams({
    refundId: normalizeOpenApiLong(input.refundId),
    sellerDisagreeReason: normalizeOpenApiInteger(input.sellerDisagreeReason, 100),
    sellerDisagreeDesc: String(input.sellerDisagreeDesc ?? '').trim(),
    sellerDisagreeImages: normalizeStringList(input.sellerDisagreeImages),
    status: normalizeOpenApiInteger(input.status, 10),
    negotiateStatus: normalizeOpenApiInteger(input.negotiateStatus, 1),
  })

  return requestKuaishouIndustryOpenApi({
    apiMethod: 'open.seller.order.refund.disagree.refund',
    path: '/open/seller/order/refund/disagree/refund',
    bizParams,
    ...(input.sellerId ? { sellerId: String(input.sellerId).trim() } : {}),
  })
}
