import { defineStore } from 'pinia'

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

interface GovernmentState {
  isInited: boolean
  formRuntimeStatusList: Array<TypeDescriptor>
  processRuntimeStatusList: Array<TypeDescriptor>
  taskTypeList: Array<TypeDescriptor>
}

export const useGovernmentStore = defineStore('government', {
  state: (): GovernmentState => ({
    isInited: false,
    formRuntimeStatusList: [],
    processRuntimeStatusList: [],
    taskTypeList: []
  }),
  actions: {
    async init() {
      if (this.isInited) {
        return
      }

      let res = await apis.getFormRuntimeStatusList()
      if (res.code === SuccessResultCode && res.data) {
        this.formRuntimeStatusList = res.data
      }
      res = await apis.getProcessRuntimeStatusList()
      if (res.code === SuccessResultCode && res.data) {
        this.processRuntimeStatusList = res.data
      }
      res = await apis.getTaskTypeList()
      if (res.code === SuccessResultCode && res.data) {
        this.taskTypeList = res.data
      }
      
      this.isInited = true
    }
  }
})
