import axios from 'axios';
import { apiResData, queryParams, applicationModel, publishApi, paramsT } from '@/api/interface/index';

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

// 个人分页
export function getMyAppList(data: queryParams) {
  return axios.post<apiResData>(`${urlPrefix}/apps/minePage`, data);
}

// 公共分页
export function getPubAppList(data: queryParams) {
  return axios.post<apiResData>(`${urlPrefix}/apps/publicPage`, data);
}

// 协调分页
export function getTeamAppList(data: queryParams) {
  return axios.post<apiResData>(`${urlPrefix}/apps/teamPage`, data);
}

// 根据场景id分页
export function getAppListBySceneId(data: queryParams) {
  return axios.post<apiResData>(`${urlPrefix}/apps/page`, data);
}

// 新增应用
export function addApp(data: applicationModel) {
  return axios.post(`${urlPrefix}/apps`, data);
}

// 编辑应用
export function editApp(data: applicationModel) {
  return axios.put(`${urlPrefix}/apps`, data);
}

// 删除应用
export function deleteApp(id: string) {
  return axios.delete(`${urlPrefix}/apps/${id}`);
}

// 复制应用
export function copyApp(data: any) {
  return axios.post(`${urlPrefix}/apps/copy`, data);
}

// 导入应用
export function importApp(data: any) {
  return axios.post(`${urlPrefix}/apps/import`, data);
}

// 导出应用
export function exportApp(data: any) {
  return axios({
    method: 'post',
    url: `${urlPrefix}/apps/export`,
    responseType: 'blob',
    data: data
  });
}

// 运行主流程
export function runMainFlow(data: any) {
  return axios.post<apiResData>(`${runPrefix}/apps/run`, data);
}

// 运行子流程
export function runSubFlow(data: any) {
  return axios.post<apiResData>(`${runPrefix}/apps/runSubflow`, data);
}

// 重运行
export function reRunApp(data: any) {
  return axios.post<apiResData>(`${runPrefix}/apps/rerun`, data);
}

// 暂停
export function pauseApp(data: any) {
  return axios.post<apiResData>(`${runPrefix}/apps/${data.id}/pause`);
}

// 恢复
export function resumeApp(data: any) {
  return axios.post<apiResData>(`${runPrefix}/apps/${data.id}/resume`);
}

// 停止
export function stopApp(data: any) {
  return axios.post<apiResData>(`${runPrefix}/apps/${data.id}/stop`);
}

// APi发布
export function apiPublish(data: publishApi) {
  return axios.post<paramsT>(`${urlPrefix}/apps/apiPublish`, data);
}

// 获取api信息
export function getPublishApi(appId: string) {
  return axios.get<paramsT>(`${urlPrefix}/apps/getPublishApi/${appId}`);
}

// 刷新Api
export function refreshToken(data: publishApi) {
  return axios.post<paramsT>(`${urlPrefix}/apps/apiTokenRefresh`, data);
}

// 获取当前工程下的所有应用及引用下的事件名称和组件名称
export function getProjectEnv(id) {
  return axios.get<paramsT>(`${urlPrefix}/apps/env/${id}`);
}
