import { create } from 'zustand'

import type { IFlow } from '../../generated/ManagingApi'
import { WorkflowClient } from '../../generated/ManagingApi'

type FlowStore = {
  setFlows: (flows: IFlow[]) => void
  getFlows: (apiUrl: string) => void
  flows: IFlow[]
}

export const useFlowStore = create<FlowStore>((set) => ({
  flows: [] as IFlow[],
  getFlows: async (apiUrl) => {
    const client = new WorkflowClient({}, apiUrl)
    await client.workflow_GetAvailableFlows().then((data) => {
      set(() => ({
        flows: data,
      }))
    })
  },
  setFlows: (flows) => {
    set((state) => ({
      ...state,
      flows: flows,
    }))
  },
}))
