import {
	$productCategory,
	$layoutpage
} from '@/http/yskApi/pad.js'

//根据布局返回商品列表
export function returnSelCategotyGoods(arr, size) {
	// 检查size是否为正整数
	if (size <= 0) {
		throw new Error('Size must be a positive integer.');
	}

	// 计算数组的长度，并确保size不会超过数组长度
	const length = arr.length;
	size = Math.min(size, length);

	// 创建一个新数组来存储结果
	const groups = [];

	// 循环遍历原始数组，每size个元素分为一组
	for (let i = 0; i < length; i += size) {
		groups.push(arr.slice(i, i + size)); // 使用slice方法获取子数组
	}

	return groups; // 返回新的数组
}
//根据店铺信息返回是否是后付款
export function $trturnPayAfter(shop) {
	//munchies 先付  restaurant 后付
	const payAfter = shop.registerType == "munchies" ? false : true;
	return payAfter
}
//根据店铺信息返回就餐类型
export function $returnUseType(shop, useType) {
	//是否是后付款
	const payAfter = $trturnPayAfter(shop)
	let result = "takeout";
	if (useType == "takeout") {
		result = 'takeout'
	} else {
		//堂食
		result = `dine-in-${payAfter ? "after" : "before"}`;
	}
	return result
}
//判断商品是否可以下单
export function isCanBuy(goods, isStock) {
	return goods.isGrounding && goods.isPauseSale == 0 && (isStock ? goods.stockNumber > 0 : true);
}


// 一个数组是否包含另外一个数组全部元素
function arrayContainsAll(arr1, arr2) {
	for (let i = 0; i < arr2.length; i++) {
		if (!arr1.includes(arr2[i])) {
			return false;
		}
	}
	return true;
}

//n项 n-1项组合，生成全部结果
function generateCombinations(arr, k) {
	let result = [];

	function helper(index, current) {
		if (current.length === k) {
			result.push(current.slice()); // 使用slice()来避免直接修改原始数组
		} else {
			for (let i = index; i < arr.length; i++) {
				current.push(arr[i]); // 将当前元素添加到组合中
				helper(i + 1, current); // 递归调用，索引增加以避免重复选择相同的元素
				current.pop(); // 回溯，移除当前元素以便尝试其他组合
			}
		}
	}

	helper(0, []); // 从索引0开始，初始空数组作为起点
	return result;
}

export function returnReverseVal(val, isReturnString = true) {
	const isBol = typeof val === "boolean";
	const isString = typeof val === "string";
	let reverseNewval = "";
	if (isBol) {
		reverseNewval = !val;
	}
	if (isString) {
		reverseNewval = val === "true" ? "false" : "true";
	}
	return reverseNewval;
}

export function returnPrveId(par) {
	const obj = {
		prveData: [],
		nowData: [],
		nextData: [],
		page: 0
	}
	Object.assign(obj, par)
	if (par.page == 0 || par.nowData.length <= 1) {
		const prveIndex = obj.prveData.length - 1 <= 0 ? 0 : obj.prveData.length - 1
		return obj.prveData[prveIndex] ? obj.prveData[prveIndex].id : undefined
	}
	return obj.nowData[par.page - 1] ? obj.nowData[par.page - 1].id : undefined

}
export function returnNowId(par) {
	const obj = {
		prveData: [],
		nowData: [],
		nextData: [],
		page: 0
	}
	Object.assign(obj, par)
	if (!par.nowData[par.page]) {
		return undefined
	}
	return obj.nowData[par.page] ? obj.nowData[par.page].id : undefined
}
export function returnNextId(par) {
	const obj = {
		prveData: [],
		nowData: [],
		nextData: [],
		page: 0
	}
	Object.assign(obj, par)
	if (par.page >= par.nowData.length - 1 || par.nowData.length <= 1) {
		const nextIndex = obj.nextData.length - 1 <= 0 ? 0 : obj.nextData.length - 1
		return obj.nextData[nextIndex] ? obj.nextData[nextIndex].id : undefined
	}
	return obj.nextData[par.page + 1] ? obj.nextData[par.page + 1].id : undefined
}


export async function returnLayoutPage(id) {
	if (!id) {
		return {
			content: [],
			totalElements: 0
		}
	}
	return $layoutpage({
		page: 1,
		size: 999,
		productCategoryId: id,
		padLayoutId: ''
	})
}
export async function returnCategoryLayoutGoods(id,category) {
	if (!id) {
		return {
			productList: [],
			padLayoutCode: "single",
			productCategoryId:category?category.id:''
		}
	}
	return $productCategory(id)
}

export default {
	isCanBuy,
	returnNowId,
	returnCategoryLayoutGoods,
	returnLayoutPage,
	returnPrveId,
	returnNextId,
	arrayContainsAll,
	generateCombinations,
	returnReverseVal,
	$returnUseType
}