import type { ProcessDefinition } from './model';

import type { ID, IDS, PageQuery, PageResult } from '#/api/common';

import { requestClient } from '#/api/request';
import { commonExport } from '#/api/helper';

// API路径常量枚举
enum Api {
  baseApi = '/flowDefinition/definition',
  export = '/flowDefinition/definition/export',
  list = '/flowDefinition/definition/list',
  unPublishList = '/flowDefinition/definition/unPublishList',
}

/**
 * 全部的流程定义
 * @param params 查询参数
 * @returns 分页
 */
export function workflowDefinitionList(params?: PageQuery) {
  return requestClient.get<PageResult<ProcessDefinition>>(
    '/flowDefinition/definition/list',
    { params },
  );
}

/**
 * 未发布的流程定义
 * @param params 查询参数
 * @returns 分页
 */
export function unPublishList(params?: PageQuery) {
  return requestClient.get<PageResult<ProcessDefinition>>(
    '/flowDefinition/definition/unPublishList', 
    { params },
  );
}

/**
 * 获取历史流程定义列表
 * @param flowCode
 * @returns ProcessDefinition[]
 */
export function getHisListByKey(flowCode: string) {
  return requestClient.get<ProcessDefinition[]>(
    `/flowDefinition/definition/getHisListByKey/${flowCode}`,
  );
}

/**
 * 获取流程定义详细信息
 * @param id id
 * @returns ProcessDefinition
 */
export function workflowDefinitionInfo(id: ID) {
  return requestClient.get<ProcessDefinition>(`/flowDefinition/definition/${id}`);
}

/**
 * 新增流程定义
 * @param data
 */
export function workflowDefinitionAdd(data: any) {
  return requestClient.post<void>('/flowDefinition/definition', data);
}

/**
 * 更新流程定义
 * @param data
 */
export function workflowDefinitionUpdate(data: any) {
  return requestClient.put<void>('/flowDefinition/definition', data);
}

/**
 * 发布流程定义
 * @param id id
 * @returns boolean
 */
export function workflowDefinitionPublish(id: ID) {
  return requestClient.put<boolean>(
    `/flowDefinition/definition/publish/${id}`,
  );
}

/**
 * 取消发布流程定义
 * @param id id
 * @returns boolean
 */
export function workflowDefinitionUnPublish(id: ID) {
  return requestClient.put<boolean>(
    `/flowDefinition/definition/unPublish/${id}`,
  );
}

/**
 * 删除流程定义
 * @param ids idList
 */
export function workflowDefinitionDelete(ids: IDS) {
  return requestClient.delete<void>(`/flowDefinition/definition/${ids}`);
}

/**
 * 复制流程定义
 * @param id id
 */
export function workflowDefinitionCopy(id: ID) {
  return requestClient.put<void>(`/flowDefinition/definition/copy/${id}`);
}

/**
 * 导入流程定义
 * @returns boolean
 */
export function workflowDefinitionImport(data: {
  category: ID;
  file: Blob | File;
}) {
  return requestClient.post<boolean>(
    '/flowDefinition/definition/importDef',
    data,
    { headers: { 'Content-Type': 'multipart/form-data' } },
  );
}

/**
 * 导出流程定义
 * @param data 请求参数，包含id
 * @returns blob
 */
export function workflowDefinitionExport(data: { id: ID }) {
  return commonExport(`${Api.export}/${data.id}`, {});
}

/**
 * 获取流程定义xml字符串
 * @param id id
 * @returns xml
 */
export function workflowDefinitionXml(id: ID) {
  return requestClient.get<string>(`/flowDefinition/definition/xmlString/${id}`);
}

/**
 * 激活/挂起流程定义
 * @param id 流程定义id
 * @param active 激活/挂起
 * @returns boolean
 */
export function workflowDefinitionActive(id: ID, active: boolean) {
  return requestClient.put<boolean>(
    `/flowDefinition/definition/active/${id}?active=${active}`,
  );
}



