import axios from './axios'
import type { BasePaginationData, BaseRespData } from '@/types/api'
import type { CancelOrderProcessStateGroupMap } from '@/types/api/cancelOrderProcess'
import type { operateOrderListData } from '@/types/api/factoryOrderNew'

function normalizePodOrderQueryPayload(
  data: Record<string, unknown>,
): Record<string, unknown> {
  const { receiverCountry, ...rest } = data
  if (!Array.isArray(receiverCountry)) {
    return data
  }
  return {
    ...rest,
    ...(receiverCountry.length
      ? { receiverCountry: receiverCountry.join(',') }
      : {}),
  }
}

export function getCancelOrderProcessStateGroupMapApi() {
  return axios.get<never, BaseRespData<CancelOrderProcessStateGroupMap>>(
    'factory/podOrderOperationForReclaim/findStateGroupMap',
  )
}

export function getCancelOrderProcessListApi(
  data: Record<string, unknown>,
  currentPage: number,
  pageSize: number,
  alreadyInStock: boolean,
) {
  const body = normalizePodOrderQueryPayload({ ...data })
  return axios.post<never, BasePaginationData<operateOrderListData>>(
    'factory/podOrderOperationForReclaim/list_page',
    { ...body, currentPage, pageSize, alreadyInStock },
  )
}
export function getOperationInfoByNoApi(operationNo: string) {
  return axios.get<never, BaseRespData<operateOrderListData>>(
    `factory/podOrderOperationForReclaim/getByOperationNo?operationNo=${operationNo}`,
  )
}
export function completeDeliveryByCancelOrderProcessApi(data: {
  operationNo: string
  inboundType: number
  warehouseId: number
}) {
  return axios.post<never, BaseRespData<never>>(
    'factory/podOrderOperationForReclaim/storeAfterScan',
    data,
  )
}
