//所有的接口请求
import axios from 'axios';

const isProd =
  import.meta.env.BASE_URL.indexOf('https://assets.le5lecdn.com') === 0;

export const cdn = isProd ? 'https://assets.le5lecdn.com' : '';
export const upCdn = isProd ? 'https://drive.le5lecdn.com' : '';
export const imageDrive = 'https://drive.le5lecdn.com';
//https://v.le5le.com/file/2023/0828/1/1/thumb.07c0a78f.png
export async function delImage(image: string) {
  if (image.startsWith('/file') || image.startsWith('/api')) {
    await axios.delete(`${image}`);
  } else if (image.startsWith(upCdn) || image.startsWith(imageDrive)) {
    await axios.delete('/api/file' + image.replace(upCdn || imageDrive, ''));
  } else {
    await axios.delete(`${image}`);
  }
  return true;
}

export async function getFolders(query: any) {
  const folder: any = await axios.post('/api/data/folders/get', {
    query,
  });
  if (folder.error) {
    return;
  } else {
    return folder;
  }
}

export async function updateFolders(data: any) {
  const folder: any = await axios.post('/api/data/folders/update', data);
  if (folder.error) {
    return;
  } else {
    return folder;
  }
}

export async function addCollection(collection: string, data: any) {
  return await axios.post(`/api/data/${collection}/add`, data);
}

export async function updateCollection(collection: string, data: any) {
  return await axios.post(`/api/data/${collection}/update`, data);
}

export async function getCollectionList(
  collection: string,
  data: any,
  config: any
) {
  return await axios.post(`/api/data/${collection}/list`, data, config);
}

export async function getLe5leV(id: string, projection?: any, historyId?: string) {
  return await axios.post('/api/data/v/get', {
    id,
    projection,
    historyId
  });
}

export async function getComponents(id: string) {
  return await axios.post(`/api/data/v.component/get`, {
    id,
  });
}

export async function getComponentsList(data: any, config: any) {
  return await axios.post('/api/data/v.component/list', data, config);
}

export async function getLe5le3d(id: string, projection?: any) {
  return await axios.post(`/api/data/3d/get`, {
    id,
    projection,
  });
}

export async function getProjectsList(data: any, config: any) {
  return await axios.post('/api/data/v/list', data, config);
}

export async function getLe5le2d(id: string, projection?: any) {
  return await axios.post('/api/data/2d/get', {
    id,
    projection,
  });
}
