import { defineStore } from 'pinia'

import { SuccessResultCode, type TypeDescriptor } from '@cacp/ui'
import * as apis from '@/apis/workflow/definition'

interface ProcessDesignState {
  isInited: boolean
  assignmentTypeList: Array<TypeDescriptor>
  strategyList: Array<TypeDescriptor>
  flowTypeList: Array<TypeDescriptor>
  nodeTypeList: Array<TypeDescriptor>
}

export const useDesignerStore = defineStore('designer', {
  state: (): ProcessDesignState => ({
    isInited: false,
    assignmentTypeList: [],
    strategyList: [],
    flowTypeList: [],
    nodeTypeList: []
  }),
  actions: {
    async init() {
      if (this.isInited) {
        return
      }

      let res = await apis.getAssignmentTypeList()
      if (res.code === SuccessResultCode && res.data) {
        this.assignmentTypeList = res.data
      }
      res = await apis.getStrategyList()
      if (res.code === SuccessResultCode && res.data) {
        this.strategyList = res.data
      }
      res = await apis.getFlowTypeList()
      if (res.code === SuccessResultCode && res.data) {
        this.flowTypeList = res.data
      }
      res = await apis.getNodeTypeList()
      if (res.code === SuccessResultCode && res.data) {
        this.nodeTypeList = res.data
      }
      
      this.isInited = true
    }
  }
})
