import type { AxiosResponse } from 'axios'

import request from '@/utils/request'
import type { Result, ProcessDefinition, CommentTemplate, ProcessDescriptor, TypeDescriptor } from '@cacp/ui'

const contextPath = '/process-definition'

export async function getDefinition(
  code: string,
  version: number
): Promise<Result<ProcessDescriptor>> {
  const res: AxiosResponse<Promise<Result<ProcessDescriptor>>> = await request.get(`${contextPath}/get-definition`, {
    params: { code: code, version: version }
  })
  return res.data
}

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

export async function saveDefinition(descriptor: ProcessDescriptor): Promise<Result<number>> {
  const res: AxiosResponse<Promise<Result<number>>> = await request.post(`${contextPath}/save-definition`, descriptor)
  return res.data
}

export async function deleteDefinition(code: string): Promise<Result<number>> {
  const res: AxiosResponse<Promise<Result<number>>> = await request.post(`${contextPath}/delete-definition`, null, {
    params: { code: code }
  })
  return res.data
}

export async function getCommentTemplateList(
  processCode: string,
  activityCode: string
): Promise<Result<Array<CommentTemplate>>> {
  const res: AxiosResponse<Promise<Result<Array<CommentTemplate>>>> = await request.get(
    `${contextPath}/get-comment-template-list`,
    {
      params: { processCode: processCode, activityCode: activityCode }
    }
  )
  return res.data
}

export async function insertCommentTemplate(info: CommentTemplate): Promise<Result<number>> {
  const res: AxiosResponse<Promise<Result<number>>> = await request.post(`${contextPath}/insert-comment-template`, info)
  return res.data
}
export async function updateCommentTemplate(info: CommentTemplate): Promise<Result<number>> {
  const res: AxiosResponse<Promise<Result<number>>> = await request.post(`${contextPath}/update-comment-template`, info)
  return res.data
}
export async function deleteCommentTemplate(commentId: string): Promise<Result<number>> {
  const res: AxiosResponse<Promise<Result<number>>> = await request.post(`${contextPath}/delete-comment-template`, null, {
    params: { commentId: commentId }
  })
  return res.data
}

export async function getAssignmentTypeList(): Promise<Result<Array<TypeDescriptor>>> {
  const res: AxiosResponse<Result<Array<TypeDescriptor>>> = await request.get(`${contextPath}/get-assignment-type-list`)
  return res.data
}
export async function getStrategyList(): Promise<Result<Array<TypeDescriptor>>> {
  const res: AxiosResponse<Result<Array<TypeDescriptor>>> = await request.get(`${contextPath}/get-strategy-list`)
  return res.data
}
export async function getFlowTypeList(): Promise<Result<Array<TypeDescriptor>>> {
  const res: AxiosResponse<Result<Array<TypeDescriptor>>> = await request.get(`${contextPath}/get-flow-type-list`)
  return res.data
}
export async function getNodeTypeList(): Promise<Result<Array<TypeDescriptor>>> {
  const res: AxiosResponse<Result<Array<TypeDescriptor>>> = await request.get(`${contextPath}/get-node-type-list`)
  return res.data
}
