import processors from './processors'
import { Action, Element, Processor } from '../'
import { ValueProcessorItem } from '@/store/modules/dynamic-content/dynamic-content-processor/value-processor'

/**
 * Action Processor
 *
 * Process and element and give out the actions that need to be called for the element
 */
export default class ActionProcessor implements Processor {
  private processors: ActionProcessorItem[]

  constructor(private rootState: any, private rootGetters: any) {
    this.processors = processors.map(
      (process: any): ActionProcessorItem => {
        return new process()
      }
    )
  }

  /**
   * Process Element
   *
   * Runs the element through all the actions processors and check if an action needs to be called
   *
   * @param element
   * @param index
   */
  public processElement(element: Element, index: number) {
    let actions: Action[] = []

    this.processors.forEach((process: ActionProcessorItem) => {
      ;(action => {
        if (action !== null) {
          actions.push(action)
        }
      })(process.processValue(element, index, this.rootState, this.rootGetters))
    })

    return actions
  }
}

export interface ActionProcessorItem {
  processValue(element: Element, index: number, rootState: any, rootGetters: any): Action | null
}
