import axios from 'axios';

// 公共路径
const urlPrefix = '/api/v1/bpms-workbench';

// 我的组件
export function getMyComponentList(params) {
  return axios.get(`${urlPrefix}/componentBase/list`, { params });
}

// 协作组件
export function getCooperationComponentList(params) {
  return axios.get(`${urlPrefix}/componentBase/page/collaborator`, { params });
}

// 获取标签列表
export function getTagList() {
  return axios.get(`${urlPrefix}/componentBase/tags`);
}

// 校验项目标识
export function compProjectValidate(projectId) {
  return axios.post(`${urlPrefix}/componentBase/validate?projectId=${projectId}`);
}

// 组件信息提交/更新
export function compSubmit(params) {
  return axios.post(`${urlPrefix}/componentBase/submit`, params);
}

// 组件删除
export const remove = (ids) => {
  return axios.post(`${urlPrefix}/componentBase/remove?ids=${ids}`);
};

// 获取导入组件的信息
export const getImportComponentInfo = (params) => {
  const formData = new FormData();
  formData.append('files', params.file);
  return axios.post(`${urlPrefix}/componentBase/import/check`, formData, {
    headers: {
      'Content-Type': 'multipart/form-data'
    }
  });
};

// 组件导入
export const importComponent = (params) => {
  const formData = new FormData();
  // 支持多个文件上传
  if (Array.isArray(params.file)) {
    params.file.forEach(file => {
      formData.append('files', file);
    });
  }
  else {
    formData.append('files', params.file);
  }
  return axios.post(`${urlPrefix}/componentBase/import`, formData, {
    headers: {
      'Content-Type': 'multipart/form-data'
    }
  });
};

// 组件导出
export const exportComponent = (id) => {
  return axios.get(`${urlPrefix}/componentBase/export?id=${id}`, { responseType: 'blob' });
};

// 复制代码和设计
export const copyAll = (params) => {
  return axios.post(`${urlPrefix}/componentBase/copy`, params);
};