import dayjs from 'dayjs'
import duration from 'dayjs/plugin/duration'
dayjs.extend(duration);
const systemInfo = uni.getSystemInfoSync();
 // const Language = (systemInfo.language == 'zh-CN' || systemInfo.language == 'zh_CN') ? 'zh' : 'en' // 'zh' | 'en'
const Language = 'en' // 'zh' | 'en'
const formatTime = (date) => {
	const year = date.getFullYear();
	const month = date.getMonth() + 1;
	const day = date.getDate();
	const hour = date.getHours();
	const minute = date.getMinutes();
	const second = date.getSeconds();
	return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(
		':');
};
const getLocation = async () => {
	let res = await uni.getLocation({
		type: 'wgs84',
		// success: function (res) {
		// 	console.log('当前位置的经度：' + res.longitude);
		// 	console.log('当前位置的纬度：' + res.latitude);
		// }
	});
	return res[1]
}
const msg = (str) => {
	uni.showToast({
		title: str,
		icon:'none'
	})
}
/**
 * 计算剩余时间
 * @param {string|dayjs.Dayjs} startTime - 开始时间（字符串或dayjs对象）
 * @param {string|dayjs.Dayjs} endTime - 结束时间（字符串或dayjs对象）
 * @returns {string} - 返回剩余时间，格式为 "X天Y小时Z分钟" 或 "<小于1分钟"
 */
function getRemainingTime(startTime, endTime,type = 0) {
	// 确保时间是dayjs对象
	const start = dayjs(startTime);
	const end = dayjs(endTime);
	// 计算时间差
	const diff = dayjs.duration(end.diff(start));
	// 如果小于1分钟
	if (diff.asMinutes() < 1) {
		return '<小于1分钟';
	}
	// 获取天数、小时数和分钟数
	const days = diff.days();
	const hours = diff.hours();
	const minutes = diff.minutes();
	if(type){
		return {
			days,
			hours,
			minutes,
		}
	}
	// 返回格式化后的字符串
	return `${days}天${hours}小时${minutes}分`;
}
//价格转换
function fenToYuan(fen) {
	const roundedFen = Math.round(Number(fen));
	return Number((roundedFen / 100).toFixed(2));
}

const strJoin = (str) => {
	if (!str) return []
	return JSON.parse(str)
}
const formatNumber = (n) => {
	n = n.toString();
	return n[1] ? n : '0' + n;
};

function isEmail(email) {
	if(!email) return false
    // 正则表达式用于匹配邮箱格式
    const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
    return emailRegex.test(email);
}

module.exports = {
	Language,
	isEmail,
	formatTime: formatTime,
	getLocation,
	msg,
	strJoin,
	fenToYuan,
	getRemainingTime
};