import { post } from '../../utils/api'
import * as T from '../../utils/types'
import * as U from '../../utils/apiUrls'

export const getEformLink = ({ getters, commit, dispatch }, query) => {
  commit(T.PROCESSING_STARTED, 'eFormLoading')
  dispatch('removeError', 'eFormLoadingError')
  let country_ids = [getters.getAppLocationCountry.countryId]
  return post(U.SMART_LIBRARY_EFORM_LIST, {
    key_ids: query,
    country_ids: country_ids,
  })
    .then(response => {
      commit(T.SET_EFORM_LINK, response.data.total > 0 ? response.data.data : [])
      commit(T.PROCESSING_FINISHED, 'eFormLoading')
    })
    .catch(e => {
      dispatch('logError', { eFormLoadingError: e.data })
      commit(T.PROCESSING_FINISHED, 'eFormLoading')
    })
}

export const loadEformActLink = ({ getters, commit, dispatch }, query) => {
  commit(T.PROCESSING_STARTED, 'eFormLoading')
  dispatch('removeError', 'eFormLoadingError')
  let country_ids = [getters.getAppLocationCountry.countryId]
  return post(U.SMART_LIBRARY_EFORM_LIST, {
    key_ids: query,
    country_ids: country_ids,
  })
    .then(response => {
      commit(T.PROCESSING_FINISHED, 'eFormLoading')
      commit(T.SET_EFORM_LINK_BY_KEY_ID, response.data.total > 0 ? response.data.data : [])
    })
    .catch(e => {
      commit(T.PROCESSING_FINISHED, 'eFormLoading')
      dispatch('logError', { eFormLoadingError: e.data })
    })
}
