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'
import { InterskuList } from '@/types/api/warehouse'

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/podOrderOperation/find-state-group-map',
  )
}

export function getCancelOrderProcessListApi(
  data: Record<string, unknown>,
  currentPage: number,
  pageSize: number,
  alreadyInStock: boolean,
) {
  const body = normalizePodOrderQueryPayload({ ...data })
  return axios.post<never, BasePaginationData<operateOrderListData>>(
    'factory/podOrderOperation/list-page',
    { ...body, currentPage, pageSize, alreadyInStock },
  )
}
export function getOperationInfoByNoApi(operationNo: string) {
  return axios.get<never, BaseRespData<operateOrderListData>>(
    `factory/podOrderOperation/get-by-operation-no?operationNo=${operationNo}`,
  )
}
export function completeDeliveryByCancelOrderProcessApi(data: {
  operationNo: string
  inboundType: number
  warehouseId: number
}) {
  return axios.post<never, BaseRespData<never>>(
    'factory/podOrderOperation/store-after-scan',
    data,
  )
}

export function getBySkuAndUserMarkAndInboundTypeApi(operationNo: string[]) {
  return axios.post<never, BaseRespData<InterskuList[]>>(
    'factory/podOrderOperation/get-product-by-operation-no',
    operationNo,
  )
}
