import {
  ENV_LIST,
  APPID,
  LOGO_DEFAULT,
  APPID_MAP,
  LOGO_KO_DEFAULT,
} from '@/constants.js'
import uni from './uniHooks'
export default {
  init() {
    const href = `${window.location.href}`
    // dev
    if (/8080|dev-tparking/g.test(href)) {
      this.setEnv('qa')
      return
    }
    // qa
    if (/qa-tparking/g.test(href)) {
      this.setEnv('qa')
      return
    }
    // prod
    this.setEnv('prod')
  },
  setEnv(env) {
    // console.log(2020202020, env)
    uni.setStorageSync("env", env);
  },
  getEnv() {
    const env = uni.getStorageSync("env");
    if (!env) {
      const env = ENV_LIST[1];
      uni.setStorageSync("env", env.name);
      return env.name
    }
    return env;
  },
  cleanAllCache() {
    uni.clearStorageSync()
  },
  getCurEnvConst(key) {
    const curEnvName = this.getEnv();
    let curEnvConst = ENV_LIST.find(item => item.name == curEnvName);
    // 如果是来自 window.injectConfig
    if (window?.injectConfig) {
      console.log(464646, window?.injectConfig);
      console.log(474747, key);
      if (key === 'KIP_API') {
        // return window?.injectConfig?.profileApi
        return window.profileApi
      }
      if (curEnvConst?.constants) {
        curEnvConst.constants = {
          ...curEnvConst?.constants,
          ...window?.injectConfig
        }
      } else {
        curEnvConst = {
          ...curEnvConst,
          constants: {
            ...window?.injectConfig
          }
        }
      }

    }
    if (key) {
      return curEnvConst?.constants[key];
    } else {
      return curEnvConst?.constants;
    }
  },
  setMiniAppOptions(options) {
    if (!options) {
      return;
    }
    uni.setStorageSync('options', options);
    const query = options.query
    if (!query.scene) {
      this.setMiniAppOptionsQuery(query)
    }

  },
  setMiniAppOptionsQuery(query) {
    if (!query || JSON.stringify(query) == '{}') {
      // 直接进入的utm_lbs是空的
      // const mallid = uni.getStorageSync("mallid")
      const defaultOptionsQuery = {
        channel: APPID,
        utm_channel: APPID,
        utm_method: 'dr',
        // utm_lbs: mallid,
      }
      query = defaultOptionsQuery;
    }
    // channel 肯定是当前小程序的appid
    query.channel = APPID;
    uni.setStorageSync("options_query", query);
  },
  getLogoByMallid(mallid) {
    console.log(90, mallid);
    const defaultLogo = isKerryOnAppid() ? LOGO_KO_DEFAULT : LOGO_DEFAULT
    if (!mallid) {
      console.error("=>[getLogoByMallid]mallid is null")
      return defaultLogo
    }
    const marketList = this.getMarketList()
    if (!marketList) {
      console.error("=>[setMarketList]marketList is null")
      return defaultLogo;
    }
    const market = marketList.find(item => item.mallid == mallid)
    if (!market) {
      console.error("=>[setMarketList]market is null")
      return defaultLogo;
    }
    return market?.logoPicture || defaultLogo;
  },
  getMarketList() {
    const marketList = uni.getStorageSync("marketList");
    if (!marketList || marketList.length === 0) {
      console.error("=>[getMarketList]marketList is empty")
      return []
    }
    return marketList;
  },
}

function isKerryOnAppid() {
  if (APPID == APPID_MAP.kerryMall) {
    return true;
  } else {
    return false;
  }
}