/**
 * @description  主题配置
 * @author       朱琦
 * @date         2022/3/1
 */
 import darkConfig from './theme-dark'
 import lightConfig from './theme-light'
 import { mapDefaultConfig } from "../config/map-default-config";
 import { businessPublicConfig } from '../config/business-config'
 import { concatArrayObject } from "../public-function";
 import mapIconConfig from '../config/map-icon-config'
 
 // 默认主题 dark
 const defaultTheme = 'dark'
 // 全部主题配置
 let allThemeConfig = null
 // 图标配置
 let iconConfig = mapIconConfig
 
 /**
  * 配置主题
  * @param config
  */
 const setThemeConfig = (config) => {
     allThemeConfig = []
 
     // 默认主题配置
     allThemeConfig[darkConfig.theme] = mixPublicThemeConfig(darkConfig)
     allThemeConfig[lightConfig.theme] = mixPublicThemeConfig(lightConfig)
 
     if (!config) return
 
     // 新增主题配置
     if (config.constructor === Object) {
         filterThemeConfig(config)
     } else if (config.constructor === Array) {
         config.forEach((item, i) => {
             filterThemeConfig(item)
         })
     }
 }
 
 /**
  * 图标配置
  * @param {Array} config 图标配置
  */
 const setIconConfig = (config) => {
     iconConfig = concatArrayObject(iconConfig, config, 'name')
 }
 
 /**
  * 混合 公共配置和主题配置
  * @param {Object} config 主题配置
  * @returns 处理过的配置
  */
 const mixPublicThemeConfig = (config) => {
     const themeConfig = { ...config }
 
     themeConfig.businessConfig = mixBusinessConfig(themeConfig.businessConfig)
 
     themeConfig.mapConfig = Object.assign({}, mapDefaultConfig, themeConfig.mapConfig)
 
     return themeConfig
 }
 
 
 /**
  * 混合 业务图例和公用图例配置
  * @param {Object} config 主题的businessConfig
  * @returns {Object}
  */
 const mixBusinessConfig = (config) => {
     let businessConfig = config;
 
     businessPublicConfig.forEach((business) => {
         businessConfig.some((item, index) => {
             if (business.type === item.type) {
                 businessConfig[index] = Object.assign({}, business, item)
                 return true
             }
         })
     })
 
     return businessConfig;
 }
 
 /**
  * 过滤主题配置
  * @param {Object} config 主题配置参数
  * @returns 过滤后的主题配置
  */
 const filterThemeConfig = (config) => {
     const theme = config.theme
     const themeArr = Object.keys(allThemeConfig)
     const themeIndex = themeArr.findIndex(i => i == theme)
     const defaultThemeConfig = allThemeConfig[defaultTheme]
 
     let themeConfig = config
 
     if (themeIndex > -1) {
         // throw new Error('不能修改原有主题')
         // 已存在的主题
         // Object.assign(allThemeConfig[theme].mapConfig, config.mapConfig)
         // Object.assign(allThemeConfig[theme].styleConfig, config.styleConfig)
         // // 业务配置合并过滤
         // allThemeConfig[theme].businessConfig = concatArrayObject(allThemeConfig[theme].businessConfig, config.businessConfig, 'type')
     } else {
         // 新增的主题
         themeConfig.styleConfig = Object.assign({}, defaultThemeConfig.styleConfig, themeConfig.styleConfig)
         themeConfig.businessConfig = concatArrayObject(defaultThemeConfig.businessConfig, themeConfig.businessConfig || [], 'type')
         themeConfig.iconConfig = concatArrayObject(defaultThemeConfig.iconConfig, themeConfig.iconConfig || [], 'name')
         themeConfig.menuImg = themeConfig.menuImg || defaultThemeConfig.menuImg
         allThemeConfig[theme] = mixPublicThemeConfig(themeConfig)
     }
 
 }
 
 /**
  * 获取主题配置
  * @param {String} theme 主题类型
  * @returns 主题配置
  */
 const getThemeConfig = (theme) => {
     return allThemeConfig[theme || defaultTheme]
 }
 
 /**
  * 获取图例配置
  * @param {String} type 图例类型
  * @param {String} theme 主题类型
  */
 const getLegendConfig = (theme, type) => {
     const businessConfig = allThemeConfig[theme].businessConfig
     const business = businessConfig.find(item => item.type === type)
 
     return business
 }
 
 /**
  * 获取主题类型, 防止主题类型没有，无则用默认主题
  * @param {String} theme 主题类型
  * @returns 主题类型
  */
 const getThemeType = (theme) => {
     return allThemeConfig[theme] ? theme : defaultTheme
 }
 
 /**
  * 根据主题类型获取图标配置
  * @param {String} theme 主题类型
  * @returns 该主题 图标配置
  */
 const getThemeIconConfig = (theme) => {
     return concatArrayObject(allThemeConfig[theme].iconConfig, iconConfig, 'name')
 }
 
 export { setThemeConfig, setIconConfig, getThemeType, getThemeConfig, getLegendConfig, getThemeIconConfig }