import MemberCacheTool from './member-cache-tool.js'
import { getCustTypeId, isKOMiniApp } from './utils.js'
import { ENV_LIST } from '@/constants.js'
import uni from './uniHooks'

export function getKipMarketListByCities(result) {
	let marketList = [];
	if (!result || result.length == 0) {
		return marketList;
	}
	const accountInfo = uni.getAccountInfoSync()
	const { miniProgram: { envVersion }} = accountInfo;

	result.forEach(item => {
		if (item.lbs && item.lbs.length > 0) {
			item.lbs.forEach(e => {
				// CRM 筛选出所有的商场
				if (e.format === 'RETAIL') {
					if (envVersion == "release" ) {
						if (e.enable == 'ENABLE') {
							marketList.push({
								groupId: e.brand.id,
								mallid: e.id,
								latitude: e.lat,
								longitude: e.lng,
								address: e.address,
								city: item.city.name,
								name: e.name,
								defaultFlag: e.defaultFlag,
								projectId: e.projectId
							});
						}
					} else {
						marketList.push({
							groupId: e.brand.id,
							mallid: e.id,
							latitude: e.lat,
							longitude: e.lng,
							address: e.address,
							city: item.city.name,
							name: e.name,
							defaultFlag: e.defaultFlag,
							projectId: e.projectId
						});
					}

				}
			});
		}
	});
	return marketList;
}
export function setDefaultMarket(app, store, marketList, newMallid, isQrcode) {
	if (!marketList) {
		console.info('===> 没有传商场列表，从缓存中获取')
		marketList = uni.getStorageSync("marketList");
	}

	if (!Array.isArray(marketList) || marketList.length == 0) {
		console.error("===> 没有商场")
		if (app.$isResolve && !isQrcode) {
			app.$isResolve()
		}
		return
	}
	// 根据小程序appid筛选商场
	// ko小程序
	const arr = [];
	if (isKOMiniApp()) {
		marketList.map(item => {
			if (item.groupId == ENV_LIST[0].constants.KERRY_ON_BRAND_ID || item.groupId == ENV_LIST[1].constants.KERRY_ON_BRAND_ID){
				arr.push(item);
			}
		});
		uni.setStorageSync("marketList", arr);
		marketList = arr;
	}
	let curMarket = null;

	if (newMallid) {
		console.info(`===> 主动设置商场 ${newMallid}`)
		curMarket = uni.getStorageSync('curMarket');
		if (!curMarket) {
			curMarket = marketList.find(item => {
				return item.mallid == newMallid
			})
		} else {
			if (curMarket.mallid == newMallid) {
				console.info(`===> 将要进入的商场与当前商场相同`)
			} else {
				curMarket = marketList.find(item => {
					return item.mallid == newMallid
				})
			}
		}

	} else {
		console.warn("===> 没有主动设置商场，获取默认商场")
		const list = marketList.filter(e => e.defaultFlag)
		curMarket = list.length > 0 ? list[0] : marketList[0]
	}

	if (!curMarket) {
		curMarket = marketList[0]
		console.warn("===> 没有主动设置商场，也没有默认商场，获取第一个商场")
	}

	app.globalData.marketList = marketList;
	app.globalData.mallid = curMarket.mallid
	app.globalData.marketName = curMarket.name
	app.globalData.marketAddress = curMarket.address
	app.globalData.groupId = curMarket.groupId
	app.globalData.curMarket = curMarket

	uni.setStorageSync("groupId", curMarket.groupId);
	uni.setStorageSync("mallid", curMarket.mallid);
	uni.setStorageSync("marketList", marketList);
	uni.setStorageSync("curMarket", curMarket);

	console.info(`===> 设置当前商场: ${curMarket.mallid} ${curMarket.name}`)
	store.commit('changeCurMarket', curMarket)

	// 切换商场是同时切换custTypeId
	// TODO 现在是qa的数据，如果上线需要根据brnad中的code
	let curMallid = curMarket.mallid;
	const custTypeId = getCustTypeId(curMallid);
	store.commit('changeCustTypeId', custTypeId);

	if (custTypeId == '0') {
		MemberCacheTool.setCurMemberCacheByGroupId(app, curMarket.groupId)
		console.log('===>location-util setCurMemberCacheByGroupId')
	}

	if (app.$isResolve && !isQrcode) {
		app.$isResolve()
		console.log('===>location-util $isResolve')
	}
}

export function isCruMarketByKey(key) {
	if (!key) {
		return false;
	}
	const curMarket = uni.getStorageSync("curMarket")
	if (!curMarket) {
		console.warn(`===> 没有关键字 ${key} 的商场`)
		return false;
	}
	if ( (curMarket.name && curMarket.name.indexOf(key) > -1)
		|| ( curMarket.address && curMarket.address.indexOf(key) > -1)
		|| ( curMarket.city && curMarket.city.indexOf(key) > -1) ) {
		return true;
	}
	return false;
}
