/**
 * API
 *
 * @version 1.0
 */

import { api as request } from 'src/boot/axios'
import type { RequestOptions } from 'core/types/request'

/**
 * API Url Registration
 */
const urls: Indexable = {
  accountSecurity: '/account/security',
  aiProvider: '/ai/provider',
  auth: '/auth',
  book: '/book',
  bookAnnotation: '/book/annotation',
  bookCollection: '/book/collection',
  chat: '/chat',
  chatConversation: '/chat/conversation',
  edge: '/edge',
  tts: '/tts',
  file: '/file',
  fileMeta: '/file/meta',
  llm: '/llm',
  note: '/note',
  noteShare: '/note/share',
  noteVersion: '/note/version',
  knowledge: '/knowledge',
  provider: '/provider',
  providerCredential: '/provider/credential',
  providerDefaultModel: '/provider/default-model',
  providerModel: '/provider/model',
  system: '/system',
  systemSetting: '/system/setting',
  userBook: '/user/book',
  workspaceBook: '/workspace/book',
  workspaceBookCollection: '/workspace/book/collection',
  workspaceMember: '/workspace/member',
  user: '/user',
  workspace: '/workspace',
}

// 排序
export const orderedUrls = () => {
  const ordered = Object.keys(urls).sort().reduce(
    (obj: Indexable, key: string) => {
      obj[key] = urls[key]
      return obj
    },
    {}
  )
  console.log('ordered', ordered)
  return ordered
}

export const getUrl = (options: RequestOptions) => {
  const name = options.name
  const path = options.path
  let url = ''
  if (name.indexOf('http') === 0 || name.indexOf('/') === 0) {
    url = name
  } else {
    url = urls[name] ? urls[name] : ''
  }
  if (path) {
    url += path
  }

  return url
}

export const getRequest = (options: RequestOptions) => {
  const url = getUrl(options)

  return request({
    url: url,
    method: 'get',
    withCredentials: options.withCredentials,
    params: options.query
  })
}

export const postRequest = (options: RequestOptions) => {
  const url = getUrl(options)
  const body = options.body

  return request({
    url: url,
    method: 'post',
    withCredentials: options.withCredentials,
    params: options.query || {},
    data: body,
    headers: options.headers
  })
}

export const putRequest = (options: RequestOptions) => {
  const url = getUrl(options)
  const body = options.body

  return request({
    url: url,
    method: 'put',
    withCredentials: options.withCredentials,
    params: options.query || {},
    data: body
  })
}

export const deleteRequest = (options: RequestOptions) => {
  const url = getUrl(options)

  return request({
    url: url,
    method: 'delete',
    withCredentials: options.withCredentials,
    params: options.query
  })
}
