import config from '@/config.js'
import defaultConfig from './defaultConfig.js'

// 合并用户配置和默认配置
const options = Object.assign({}, defaultConfig, config)

/**
 * 配置文件工具类
 * @module Config
 * mix: 如需在项目中获取配置项, 请使用本工具类的方法, 不要直接import根目录的config.js
 */
export default {

  /**
   * 获取全部配置
   */
  all() {
    return options
  },

  /**
   * 获取指定配置
   * @param {string} key
   * @param {mixed} def
   */
  get(key, def = undefined) {
    if (options.hasOwnProperty(key)) {
      return options[key]
    }
    console.error(`检测到不存在的配置项: ${key}`)
    return def
  },

  /**
   * 获取当前商城ID
   */
  getStoreId() {

    // #ifdef H5
    // 判断是否启用H5端多开
    // 启用后将通过获取子域名中的ID作为storeId
    if (this.get('enabledH5Multi')) {
      const domain = window.location.hostname
      const regex = this.get('domainIdRegex')
      if (domain.match(regex)) {
        const storeId = regex.exec(domain)[1].trim()
        return storeId
      }
    }
    // #endif
    return this.get('storeId')
  },

}