import _ from 'lodash'
import Vue from 'vue'
import helpers from '@/helpers'

export default {
  getMasterDataByKey: state => key => {
    return state.master_data[key]
  },
  getMasterDataKeysByKeysTitles: state => (key, titles) => {
    let masterData = state.master_data[key]
    let keys = []
    if (masterData !== undefined) {
      keys = masterData.filter(function(data) {
        return titles.map(x => x.toLowerCase()).includes(data.title.toLowerCase())
      })
    }
    return keys
  },
  getMasterDataIdsByKeysTitlesMapped: state => (key, map) => {
    let masterData = state.master_data[key]
    return _.zipObject(
      _.keys(map),
      _.map(_.keys(map), function(item) {
        if (masterData !== undefined) {
          let masterValue = masterData.find(function(data) {
            return map[item].toLowerCase() === data.title.toLowerCase()
          })
          if (masterValue) {
            return masterValue.id
          } else {
            return false
          }
        }
        return false
      })
    )
  },
  getMasterDataOptionsByKey: state => key => {
    let options = []
    if (state.master_data[key] !== undefined) {
      options = state.master_data[key].map(opt => ({
        key: opt.key,
        value: opt.id,
        text: helpers.getValue(opt, 'translated_title.content', opt.title),
      }))
    }
    return options
  },
  getTranslatedMasterDataOptionsByKey: (state, getters) => key => {
    return getters.getMasterDataOptionsByKey(key).map(object => {
      object.text = Vue.prototype.$t('Master.' + object.text)
      return object
    })
  },
  getEnrolmentMasterDataOptionsByKey: state => key => {
    let options = []
    if (state.master_data_enrolement[key] !== undefined) {
      options = state.master_data_enrolement[key].map(opt => ({
        text: helpers.getValue(opt, 'translated_title.content', opt.title),
        value: opt.id,
        key: opt.key,
      }))
    }
    return options
  },
  getTranslatedEnrolmentMasterDataOptionsByKey: (state, getters) => key => {
    return getters.getEnrolmentMasterDataOptionsByKey(key).map(object => {
      object.text = Vue.prototype.$t('Master.' + object.text)
      return object
    })
  },
  getMasterDataIdByKeyAndTitle: state => (key, title) => {
    let data = state.master_data[key]
    if (typeof data !== 'undefined' && data.length > 0) {
      let row = data.find(x => x.title.toLowerCase() === title.toLowerCase())
      if (typeof row !== 'undefined' && Object.keys(row).length > 0) {
        return row.id
      }
    }
    return false
  },
  getEnrolmentMasterDataIdByKeyAndTitle: state => (key, title) => {
    let data = state.master_data_enrolement[key]
    if (typeof data !== 'undefined' && data.length > 0) {
      let row = data.find(x => x.title.toLowerCase() === title.toLowerCase())
      if (typeof row !== 'undefined' && Object.keys(row).length > 0) {
        return row.id
      }
    }
    return false
  },

  getEnrolmentMasterDataIdByKey: state => key => {
    let data = state.master_data_enrolement[key]
    return data
  },
  getMasterDataTitleByKeyAndId: state => (key, id) => {
    if (typeof id !== 'undefined') {
      let data = state.master_data[key]
      if (typeof data !== 'undefined' && data.length > 0) {
        let row = data.find(x => x.id === id)
        if (typeof row !== 'undefined' && Object.keys(row).length > 0) {
          return row.title
        }
      }
    }
    return false
  },
  getMasterDataItemKeyByKeyAndId: state => (key, id) => {
    if (typeof id !== 'undefined') {
      let data = state.master_data[key]
      if (typeof data !== 'undefined' && data.length > 0) {
        let row = data.find(x => x.id === id)
        if (typeof row !== 'undefined' && Object.keys(row).length > 0) {
          return row.key
        }
      }
    }
    return false
  },
}
