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") || window.env;
    if (!env) {
      const env = ENV_LIST[0];
      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) {
      if (key === 'KIP_API') {
        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;
  },
  isCurMallidByEnvConstKey (curMallid, cacheKey) {
    if (!curMallid || curMallid.trim() == '') {
      console.error("=>[isCurMallidByEnvConstKey]curMallid is null")
      return false;
    }
    if (!cacheKey || cacheKey.trim() == '') {
      console.error("=>[isCurMallidByEnvConstKey]cacheKey is null")
      return false;
    }
    const curEnvConst = this.getCurEnvConst();
    if (!curEnvConst) {
      console.error("=>[isCurMallidByEnvConstKey]curEnvConst is null")
      return false;
    }
    const result = curMallid == curEnvConst[cacheKey];
    return result;
  },
  isTianJinMallid (curMallid) {
    const result = this.isCurMallidByEnvConstKey(curMallid,'TIANJIN_LBS_ID');
    return result;
  },
  isBeiJingMallid (curMallid) {
    const result = this.isCurMallidByEnvConstKey(curMallid,'BEIJING_LBS_ID');
    return result;
  },
  isJingAnMallid (curMallid) {
    const result = this.isCurMallidByEnvConstKey(curMallid,'JINGAN_LBS_ID');
    return result;
  },
  isShenZhenMallid (curMallid) {
    const result = this.isCurMallidByEnvConstKey(curMallid,'SHENZHEN_LBS_ID');
    return result;
  },
  isShenYangMallid(curMallid) {
    const result = this.isCurMallidByEnvConstKey(curMallid,'SHENYANG_LBS_ID');
    return result;
  },
}

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