import { ActionChain, actionFactory } from '@zedux/core'
import { Ecosystem } from '../classes/Ecosystem'
import { SelectorCache } from '../classes/Selectors'
import {
  LifecycleStatus,
  AnyAtomInstance,
  DependentEdge,
  EvaluationReason,
  AnyAtomTemplate,
} from '../types/index'

export const pluginActions = {
  ecosystemWiped: actionFactory<{ ecosystem: Ecosystem }, 'ecosystemWiped'>(
    'ecosystemWiped'
  ),
  edgeCreated: actionFactory<
    {
      dependency: AnyAtomInstance | SelectorCache
      // string if `edge.flags & External`:
      dependent: AnyAtomInstance | SelectorCache | string
      edge: DependentEdge
    },
    'edgeCreated'
  >('edgeCreated'),
  edgeRemoved: actionFactory<
    {
      dependency: AnyAtomInstance | SelectorCache
      dependent: AnyAtomInstance | SelectorCache | string // string if edge is External
      edge: DependentEdge
    },
    'edgeRemoved'
  >('edgeRemoved'),
  evaluationFinished: actionFactory<
    {
      node: AnyAtomInstance | SelectorCache
      time: number
    },
    'evaluationFinished'
  >('evaluationFinished'),
  instanceReused: actionFactory<{
    instance: AnyAtomInstance
    template: AnyAtomTemplate
  }>('instanceReused'),
  // either cache or instance will always be defined, depending on the node type
  stateChanged: actionFactory<
    {
      action?: ActionChain
      cache?: SelectorCache
      instance?: AnyAtomInstance
      newState: any
      oldState: any
      reasons: EvaluationReason[]
    },
    'stateChanged'
  >('stateChanged'),
  statusChanged: actionFactory<
    {
      newStatus: LifecycleStatus
      node: AnyAtomInstance | SelectorCache
      oldStatus: LifecycleStatus
    },
    'statusChanged'
  >('statusChanged'),
}
