import CacheKeys from './CacheKeys'

/**
 * 本地存储KEY前缀
 */
const _KEY_PREFIX = 'HL_'

/**
 * 用户级缓存定义
 */
const CACHE_LEVEL_USER = []

/**
 * 构造缓存KEY
 * @param {*} key
 */
const buildKey = (key) => {
	let newKey = _KEY_PREFIX
	newKey += CACHE_LEVEL_USER.indexOf(key) !== -1 ? (newKey.endsWith('_') ? '' : '_') + this.$AppContext.$store.getters.sysUser
		.username : ''
	return newKey + (newKey.endsWith('_') ? '' : '_') + key
}

/**
 * 缓存对象API
 */
const cache = {
	$Keys: CacheKeys,
	_KEY_PREFIX: _KEY_PREFIX,
	set: (object) => uni.setStorage(Object.assign(object, {
		key: buildKey(object.key)
	})),
	get: (object) => uni.getStorage(Object.assign(object, {
		key: buildKey(object.key)
	})),
	setSync: (key, data) => uni.setStorageSync(buildKey(key), data),
	getSync: (key) => uni.getStorageSync(buildKey(key)),
	remove: (object) => uni.removeStorage(Object.assign(object, {
		key: buildKey(object.key)
	})),
	removeSync: (key) => uni.removeStorageSync(buildKey(key)),
	clear: (object) => uni.clearStorage(object),
	clearSync: () => uni.clearStorageSync(),
	getInfo: (object) => uni.getStorageInfo(object),
	getInfoSync: () => uni.getStorageInfoSync()
}

export default cache
