import type { HttpDefine } from '@/http/type'
import { oapiClient } from './oapi-client'

export type DataWrapped<T> = { data: T }

async function dataInterceptor<T>(promise: Promise<T>): Promise<DataWrapped<T>> {
  const result = await promise
  return { data: result }
}

export const oapiDataClient = {
  request<T = unknown>(config: HttpDefine): Promise<DataWrapped<T>> {
    return dataInterceptor(oapiClient.request<T>(config))
  },

  get<T = unknown>(
    url: string,
    query?: Record<string, unknown>,
    config?: HttpDefine,
  ): Promise<DataWrapped<T>> {
    return dataInterceptor(oapiClient.get<T>(url, query, config))
  },

  post<T = unknown>(
    url: string,
    body?: Record<string, unknown> | FormData,
    config?: HttpDefine,
  ): Promise<DataWrapped<T>> {
    return dataInterceptor(oapiClient.post<T>(url, body, config))
  },

  put<T = unknown>(
    url: string,
    body?: Record<string, unknown> | FormData,
    config?: HttpDefine,
  ): Promise<DataWrapped<T>> {
    return dataInterceptor(oapiClient.put<T>(url, body, config))
  },

  patch<T = unknown>(
    url: string,
    body?: Record<string, unknown> | FormData,
    config?: HttpDefine,
  ): Promise<DataWrapped<T>> {
    return dataInterceptor(oapiClient.patch<T>(url, body, config))
  },

  delete<T = unknown>(
    url: string,
    query?: Record<string, unknown>,
    config?: HttpDefine,
  ): Promise<DataWrapped<T>> {
    return dataInterceptor(oapiClient.delete<T>(url, query, config))
  },
}
