import { SET_MENU_ITEMS, CLEAR_MENU_ITEMS } from '../../utils/types'
import { STATIC_LINKS } from '@/config/top-menu'
import { ESHOP_COUNTRY as COUNTRY_URL, ESHOP_LANGUAGE as LANGUAGE_URL } from '@/config/url-prefix'
/**
 * Generates Menus
 *
 * see main.js file, TopMenu tree is generated from menu-tree plugin with the meta key in the routes
 * with topMenu, this class loads the static menus from @/config/top-menu and adds country list, language list
 * and user profile link to it
 *
 * @param commit
 * @param getters
 */
export const generateMenus = ({ dispatch, commit, getters }) => {
  // We are going to create the menus for safety and regenerate the menus
  commit(CLEAR_MENU_ITEMS, 'topMenuCountries')
  dispatch('topMenu/regenerateMenu')
  dispatch('bottomMenu/regenerateMenu')
  let countries = getters['getCountries']
  let languages = getters['getAllLanguages']
  // add countries to menu items this is so that the country items are hased
  // will be usefull when removing from the menu list
  commit(SET_MENU_ITEMS, [
    { name: 'topMenuCountries', items: countries },
    { name: 'topMenuLanguages', items: languages },
  ])

  // Copy the static links, referencing will produce dublicate menu entries
  let addToMenus = STATIC_LINKS.slice()

  // get menu items
  let menuItemCountries = getters['getMenuItems']('topMenuCountries')
  let menuItemLanguages = getters['getMenuItems']('topMenuLanguages')
  let countryChildren = []
  let languageChildren = []
  menuItemCountries.forEach(country => {
    countryChildren.push({
      name: country.name,
      path: COUNTRY_URL + '/' + country.value,
      topMenu: {
        displayName: country.custom_text,
      },
    })
  })
  menuItemLanguages.forEach(language => {
    languageChildren.push({
      name: language.text,
      path: LANGUAGE_URL + '/' + language.value,
      topMenu: {
        displayName: language.text,
      },
    })
  })
  // Add country & language menu dropdown
  addToMenus.push(
    {
      name: getters['getSelectedCountryName'],
      path: COUNTRY_URL,
      topMenu: {
        displayName: '--SELECTED_COUNTRY_NAME--',
        position: 'left',
        priority: 7,
      },
      children: countryChildren,
    },
    {
      name: getters['getSelectedLanguageName'] || 'Language',
      path: LANGUAGE_URL,
      topMenu: {
        displayName: '--SELECTED_LANGUAGE_NAME--',
        position: 'left',
        priority: 6,
      },
      children: languageChildren,
    }
  )
  // add profile url
  let loggedInUser = getters.getLoggedInUser
  addToMenus.push({
    name: 'Profile',
    path: '#',
    topMenu: {
      displayName: ['Eshop.Menu.UsersAccount', { user: loggedInUser.name }],
      position: 'right',
      priority: 1,
      readonly: true,
    },
    children: [],
  })
  dispatch('topMenu/addToTree', addToMenus)
}

/**
 * Clears the all menu from the local storage
 *
 * @param dispatch
 * @param commit
 * @param getters
 */
export const clearMenus = ({ dispatch, commit }) => {
  commit(CLEAR_MENU_ITEMS, 'topMenuCountries')
  commit(CLEAR_MENU_ITEMS, 'topMenuLanguages')
  dispatch('topMenu/clearMenu')
  dispatch('bottomMenu/clearMenu')
}
