import helpers from '@/helpers'

export default {
  getTemporaryUrl: state => {
    return state.temporaryRedirectUrl
  },

  getEformLinkData: (state, getters) => {
    let formlink = state.eFormLink.find(link => {
      return link.language_id === helpers.getValue(getters, 'getSelectedLanguage.id')
    })
    if (!formlink) {
      formlink = state.eFormLink.find(link => {
        return (
          link.language_id ===
          (
            getters.getAllLanguages.find(language => {
              return language.key.toLowerCase() === 'en'
            }) || {}
          ).value
        )
      })
    }
    return formlink
  },

  getEformLinkDataByKeyId: (state, getters) => keyId => {
    let data = state.eFormLinkByKeyId[keyId] || [{ id: '', text: '', link: '', language_id: '' }]
    let formlink = data.find(link => {
      if (getters.getSelectedLanguage.id !== null) {
        return link.language_id === getters.getSelectedLanguage.id
      }
    })

    formlink =
      formlink ||
      data.find(link => {
        return (
          link.language_id ===
          (
            getters.getAllLanguages.find(language => {
              return language.key !== undefined && language.key.toLowerCase() === 'en'
            }) || { value: '' }
          ).value
        )
      })

    formlink = formlink || data[0]

    return formlink
  },
}
