import * as T from '../../utils/types.js'
import _ from 'lodash'

export const updateDynamicByIndexValue = (
  { commit }: { commit: any },
  data: {
    name: string
    elementIndex: number
    fieldIndex: number
    fieldValue: any
  }
) => {
  commit(T.DYNAMIC_CONTENT_SET_BY_INDEX, data)
}

export const updateDynamicByIdValue = (
  { commit, getters }: { commit: any; getters: any },
  data: {
    name: string
    elementIndex: number
    fieldIndex: number
    fieldValue: any
  }
) => {
  let elementIndex = _.findIndex(getters.getDynamicContent(data.name), { id: data.elementIndex })
  if (elementIndex != -1) {
    commit(T.DYNAMIC_CONTENT_SET_BY_INDEX, { ...data, elementIndex: elementIndex })
  }
}
