import { client } from "./api"
import { customEvent } from "./event";

export const resetuserinfocache = (that?: any) => {
  userInfo().then((res: any) => {
    if (!res.code) {
      wx.setStorageSync('userInfo', res.data);
      if (that) {
        that.setData({ userInfo: res.Data });
      }
      customEvent.emmit('update:userinfo', [res.data]);
    }
  })
}
export const userInfo = () => {
  return client.request('api/info', 'GET');
}
export const login = () => {
  return client.request('oauth', 'GET');
}

export const diamonds = () => {
  return client.request('api/diamond/confs', 'GET');
}
export const vips = () => {
  return client.request('api/vip/confs', 'GET');
}
export const updateuserinfo = (nickname: string, avatar: string) => {
  return client.request('api/update', 'POST', { nickname, avatar });
}
export const analysis = (content: string) => {
  return client.request('api/analysis', 'POST', { content });
}
export const tasksearch = (taskId: string) => {
  return client.request('api/task', 'GET', { taskId });
}
export const createTask = (parseId: string, content: string) => {
  return client.request('api/task/create', 'POST', { parseId, content });
}
export const polishScript = (taskId: string, content: string, style = 'optimize') => {
  return client.request('api/script/polish', 'POST', { taskId, content, style });
}
export const saveScript = (taskId: string, content: string) => {
  return client.request('api/script/save', 'POST', { taskId, content });
}
export const diamondVirtualPayment = (configId: number) => {
  return client.request('api/virtual/diamond', 'POST', { configId });
}
export const vipVirtualPayment = (configId: number) => {
  return client.request('api/virtual/vip', 'POST', { configId });
}
export const taskinfo = (taskId: string) => {
  return client.request('api/task/search', 'GET', { taskId });
}
export const feedback = (data: any) => {
  return client.request('api/feedback', 'POST', { data });
}
export const updateTaskAvatar = (taskId: string, avatarId: number) => {
  return client.request('api/task/avatar', 'POST', { taskId, avatarId });
}
export const updateTaskVoice = (taskId: string, voiceId: number) => {
  return client.request('api/task/voice', 'POST', { taskId, voiceId });
}
export const updatesessionkey = (code: string) => {
  return client.request('api/update/session-key', 'POST', { code });
}
export const uploadavatar = (file: any, data: any, path: string) => {
  return client.upload(file, data, path);
}
export const checkavatar = (url: string) => {
  return client.request('api/upload/avatar/check', 'POST', { url });
}
export const checkvoice = (url: string, asrTaskId = '') => {
  return client.request('api/upload/voice/check', 'POST', { url, asrTaskId });
}
export const saveHumanAssets = (data: { taskId: string, avatarUrl: string, voiceUrl: string, voiceText: string, durationMs?: number, asrTaskId?: string }) => {
  return client.request('api/human-assets/save', 'POST', data);
}
export const backgroundTemplates = () => {
  return client.request('api/background/templates', 'GET');
}
export const updateTaskBackground = (taskId: string, backgroundId: number) => {
  return client.request('api/task/background', 'POST', { taskId, backgroundId });
}
export const historyList = (status = 'all', page = 1, limit = 10) => {
  return client.request('api/history', 'GET', { status, page, limit });
}
export const historyDetail = (taskId: string) => {
  return client.request('api/history/detail', 'GET', { taskId });
}
export const historyDelete = (taskId: string) => {
  return client.request('api/history/delete', 'POST', { taskId });
}
export const historyDownload = (taskId: string) => {
  return client.request('api/history/download', 'POST', { taskId });
}
export const historyCancel = (taskId: string) => {
  return client.request('api/history/cancel', 'POST', { taskId });
}
export const sign = () => {
  return client.request('api/sign', 'POST', {});
}
export const uploadrecord = (file: string) => {
  return client.upload(file, {}, '/api/upload/voice');
}





