class Language {
	constructor() {
		this.languagePackage = null;
		this.getLanguages = this.getLanguages.bind(this);
		this.languagecode = wx.getStorageSync('languagecode') || 'ZH';
		this.languageRequestInProgress = null; // 新增：用于标识请求是否正在进行
		if (this.languagecode != 'ZH') {
			setTimeout(() => {
				this.getLanguages(this.languagecode)
			}, 300)
		} else {
			this.languagePackage = {}
		}
	}

	async getLanguages(languagecode) {
		// 如果已有请求正在进行，直接返回当前请求的 Promise
		if (this.languageRequestInProgress) {
			return this.languageRequestInProgress;
		}

		return new Promise((resolve, reject) => {
			// 开始发起请求前，标记请求正在进行
			this.languageRequestInProgress = new Promise(async (resolveRequest, rejectRequest) => {
				try {
					let res = {
						data: {}
					};
					if (languagecode !== 'ZH') res = await getApp().globalData.http.base({
						id: 2024062809072801,
						content: {
							date: Date.now()
						}
					});
					// 更新语言包和语言代码
					this.languagePackage = res.data[languagecode] || {};
					this.languagecode = languagecode;

					// 保存到本地存储
					wx.removeStorageSync('languagecode');
					wx.setStorageSync('languagecode', languagecode);

					// 请求成功，解决 Promise
					resolveRequest(this.languagePackage);
					resolve(this.languagePackage);
				} catch (error) {
					// 请求失败，拒绝 Promise
					rejectRequest(error);
					reject(error);
				} finally {
					// 请求完成，清除锁定
					this.languageRequestInProgress = null;
				}
			});
		});
	}

	async getLanguagePackage(that, setNavBarTitle, count = 0) {
		if (count == 10) return;
		if (this.languagePackage == null) {
			if (count == 0) that.setData({
				language: {},
				sColors: getApp().globalData.sColors
			});
			setTimeout(() => {
				this.getLanguagePackage(that, setNavBarTitle, count + 1);
			}, 100);
		} else {
			that.setData({
				language: this.languagePackage,
				sColors: getApp().globalData.sColors
			});
			if (setNavBarTitle) this.setNavBarTitle(setNavBarTitle);
		}
	}

	getMapText(text, f = '', r = '') {
		let v = ''
		try {
			v = this.languagePackage[text] || text
		} catch (error) {
			if (wx.getStorageSync('languagecode') != 'ZH') console.log('getMapText：未获取到', text)
			v = text
		}
		return f + v + r
	}
	setNavBarTitle(title) {
		wx.setNavigationBarTitle({
			title: this.getMapText(title)
		})
	}
	showToast(list, mask = false) {
		if (list.length) wx.showToast({
			title: this.joint(list),
			icon: "none",
			mask: mask
		})
	}
	modeBoxPrompts(list = [], title = '提示', confirmText = '确定') {
		wx.showModal({
			title: this.getMapText(title),
			content: this.joint(list),
			showCancel: false,
			confirmText: this.getMapText(confirmText),
		})
	}
	/* t:translate 是否翻译 v:value 值 f:front 前面 r:rear 后面 */
	joint(list) {
		if (typeof list == 'string') return this.getMapText(list)
		let str = ''
		list.forEach(v => {
			let value = ''
			try {
				value = v.t ? this.getMapText(v.v, v.f || '', v.r || '') : ((v.f || '') + v.v + (v.r || ''))
			} catch (error) {
				value = (v.f || '') + v.v + (v.r || '')
			}
			str += value
		})
		return str
	}
}
export {
	Language
}