import {Result} from '@/request/Result'
import {get, post, del, put} from '@/request/index'
import useStore from '@/stores'
import {type Ref} from 'vue'

const prefix: any = {_value: '/workspace/'}
Object.defineProperty(prefix, 'value', {
  get: function () {
    const {user} = useStore()
    return this._value + user.getWorkspaceId() + '/application'
  },
})
/**
 * API_KEY列表
 * @param 参数 application_id
 */
const getAPIKey: (application_id: string, current_page: number, page_size: number, params: any, loading?: Ref<boolean>) => Promise<Result<any>> = (
  application_id,
  current_page,
  page_size,
  params,
  loading,
) => {
  return get(`${prefix.value}/${application_id}/application_key/${current_page}/${page_size}`, params, loading)
}

/**
 * 新增API_KEY
 * @param 参数 application_id
 */
const postAPIKey: (application_id: string, loading?: Ref<boolean>) => Promise<Result<any>> = (
  application_id,
  loading,
) => {
  return post(`${prefix.value}/${application_id}/application_key`, {}, undefined, loading)
}

/**
 * 删除API_KEY
 * @param 参数 application_id api_key_id
 */
const delAPIKey: (
  application_id: string,
  api_key_id: string,
  loading?: Ref<boolean>,
) => Promise<Result<boolean>> = (application_id, api_key_id, loading) => {
  return del(
    `${prefix.value}/${application_id}/application_key/${api_key_id}`,
    undefined,
    undefined,
    loading,
  )
}

/**
 * 修改API_KEY
 * @param 参数 application_id,api_key_id
 * data {
 *   is_active: boolean
 * }
 */
const putAPIKey: (
  application_id: string,
  api_key_id: string,
  data: any,
  loading?: Ref<boolean>,
) => Promise<Result<any>> = (application_id, api_key_id, data, loading) => {
  return put(
    `${prefix.value}/${application_id}/application_key/${api_key_id}`,
    data,
    undefined,
    loading,
  )
}

export default {
  getAPIKey,
  postAPIKey,
  delAPIKey,
  putAPIKey,
}
