/**
 * @desc 应用门户配置
 * @author linyuanjie
 * @date 2023/11/10
 */

import router from '@/router/router'
import store from '@/store'
import { changeThemeColor } from '@/util/themeColorClient'

// 修改favicon
export const changeFavicon = (link, specialPath) => {
  let $favicon = document.querySelector('link[rel="icon"]')
  if ($favicon !== null) {
    if (specialPath === 'default') {
      $favicon.href = '/favicon.ico'
    } else {
      $favicon.href = link
    }
  } else {
    $favicon = document.createElement('link')
    $favicon.rel = 'icon'
    if (specialPath === 'default') {
      $favicon.href = '/assets/favicon.ico'
    } else {
      $favicon.href = link
    }
    document.head.appendChild($favicon)
  }
}

const applyPortalConfigFn = () => {
  store.commit('SET_IS_PAGE_LOADING', true)
  // 设置title
  const title = store.getters.tag.label
  const currentPortalConfig = store.getters.currentPortalConfig
  const specialPath = store.getters.specialPath
  router.$avueRouter.setTitle(title)
  // 设置favico
  changeFavicon(currentPortalConfig.portalIcon, specialPath)
  // store.commit('SET_THEME', currentPortalConfig.themeColor.hex) // 这种方式通过混入改变主题色, 刷新之后不生效
  // 设置主题色
  changeThemeColor(currentPortalConfig.themeColor.hex)
}

export default applyPortalConfigFn
