import _ from 'lodash'
import { Element, ValueProcessor, ActionProcessor } from './dynamic-content-processor'

export default {
  /**
   * getDynamicContentActions
   *
   * Process though the content and get the actions needed to call
   *
   * @param state
   * @param getters
   * @param rootState
   * @param rootGetter
   */
  getDynamicContentActions: (state: any, getters: any, rootState: any, rootGetter: any) => (name: any) => {
    let content: Array<any> = state.dynamicContent[name] || []

    let output: Array<any> = []

    if (content.length && content.length > 0) {
      let actionProcessor = new ActionProcessor(rootState, rootGetter)

      content.forEach((element: Element, index: number) => {
        output = [...output, ...actionProcessor.processElement(element, index)]

        if (element.fields && element.fields.length) {
          for (let i in element.fields) {
            output = [...output, ...actionProcessor.processElement(element.fields[i], parseInt(i))]
          }
        }
      })
    }

    return output
  },
  /**
   * getDynamicContentComputed
   *
   * Process through the dynamic content and compute the value
   * and return in a format where components can render
   *
   * @param state
   * @param getters
   * @param rootState
   * @param rootGetter
   */
  getDynamicContentComputed: (state: any, getters: any, rootState: any, rootGetter: any) => (name: any) => {
    let content: Array<any> = state.dynamicContent[name] || []
    let valueProcessor = new ValueProcessor(rootState, rootGetter)

    let output: { elements: Array<any> } = {
      elements: [],
    }

    if (content.length && content.length > 0) {
      content.forEach((element: Element, index: number) => {
        output.elements.push(
          (function(el: any) {
            if (el.fields && el.fields.length) {
              el.fields = _.map(el.fields, (field: Element, fieldIndex: number) => {
                return valueProcessor.processElement(field, fieldIndex)
              })
            }

            return el
          })(valueProcessor.processElement(element, index))
        )
      })
    }

    // Process triggers
    output.elements.forEach((element: Element, index: number) => {
      output.elements[index].fields.forEach((field: Element, fieldIndex: number) => {
        if (
          output.elements[index].fields[fieldIndex].inherit_column &&
          output.elements[index].fields[fieldIndex].inherit_index
        ) {
          let inheritIndex = output.elements[index].fields[fieldIndex].inherit_index
          let inheritColumn = output.elements[index].fields[fieldIndex].inherit_column
          let currentFieldIndex = output.elements[index].fields[fieldIndex].index || fieldIndex
          let lookupIndex = _.findIndex(output.elements[index].fields, { index: inheritIndex })
          if (lookupIndex !== -1) {
            output.elements[index].fields[lookupIndex] = {
              ...output.elements[index].fields[lookupIndex],
              triggerIndex: currentFieldIndex,
              triggerColumn: inheritColumn,
            }
          }
        }
      })

      output.elements[index].fields = _.orderBy(output.elements[index].fields, ['order'])
    })

    return output
  },
  getDynamicContent: (state: any, getters: any, rootState: any, rootGetter: any) => (name: any) => {
    let content: Array<any> = state.dynamicContent[name] || []
    return content
  },
  getDynamicContentForApi: (state: any) => (name: any) => {
    let content: any = state.dynamicContent[name] || []

    let output: any = {}

    let groupedData = state.dynamicContentGroupConfig[name]

    if (groupedData) {
      output[groupedData['name']] = content

      if (groupedData.ungroupPlace) {
        for (let key in groupedData.ungroupPlace) {
          let matchedGroup: any = _.find(content, groupedData.ungroupPlace[key].match)

          if (matchedGroup !== undefined) {
            if (groupedData.ungroupPlace[key].placeValue) {
              output[key] = matchedGroup[groupedData.ungroupPlace[key].placeValue]
            }
          }
        }
      }
    } else {
      output = content
    }

    return output
  },
}
