import type { AxiosResponse } from 'axios'

import request from '@/utils/request'
import type { Result, TypeDescriptor, RuntimeProcess, RuntimeTask, TraceInfo, TaskType } from '@cacp/ui'

const contextPath = '/process-runtime'

export async function getRunningProcessList(): Promise<Result<Array<RuntimeProcess>>> {
  const res: AxiosResponse<Result<Array<RuntimeProcess>>> = await request.get(`${contextPath}/get-running-process-list`)
  return res.data
}

export async function getUserTaskList(type: TaskType): Promise<Result<Array<RuntimeTask>>> {
  const res: AxiosResponse<Result<Array<RuntimeTask>>> = await request.get(`${contextPath}/get-user-task-list`, {
    params: { type: type }
  })
  return res.data
}

export async function rollback(processId: string): Promise<Result<boolean>> {
  const res: AxiosResponse<Result<boolean>> = await request.get(`${contextPath}/rollback`, {
    params: { processId: processId }
  })
  return res.data
}

export async function trace(processId: string): Promise<Result<TraceInfo>> {
  const res: AxiosResponse<Promise<Result<TraceInfo>>> = await request.get(`${contextPath}/trace`, {
    params: { processId: processId }
  })
  return res.data
}

export async function traceBranch(assignId: string): Promise<Result<TraceInfo>> {
  const res: AxiosResponse<Promise<Result<TraceInfo>>> = await request.get(`${contextPath}/trace-branch`, {
    params: { assignId: assignId }
  })
  return res.data
}

export async function getFormRuntimeStatusList(): Promise<Result<Array<TypeDescriptor>>> {
  const res: AxiosResponse<Result<Array<TypeDescriptor>>> = await request.get(`${contextPath}/get-form-runtime-status-list`)
  return res.data
}

export async function getProcessRuntimeStatusList(): Promise<Result<Array<TypeDescriptor>>> {
  const res: AxiosResponse<Result<Array<TypeDescriptor>>> = await request.get(`${contextPath}/get-process-runtime-status-list`)
  return res.data
}

export async function getTaskTypeList(): Promise<Result<Array<TypeDescriptor>>> {
  const res: AxiosResponse<Result<Array<TypeDescriptor>>> = await request.get(`${contextPath}/get-task-type-list`)
  return res.data
}
