const require_runtime = require("../../_virtual/_rolldown/runtime.js");
const require_objectSpread2 = require("../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js");
const require_utils = require("../booking/info/service2/utils.js");
const require_utils$1 = require("../booking/utils.js");
let _pisell_utils = require("@pisell/utils");
let decimal_js = require("decimal.js");
decimal_js = require_runtime.__toESM(decimal_js);
//#region src/components/shoppingCart/utils.ts
/**
* @title: 格式化购物车列表
* @description:
* @param {any} state
* @return {*}
* @Author: WangHan
* @Date: 2024-11-28 16:52
*/
const formatCartList = (state) => {
	const { products, holders } = state;
	return holders === null || holders === void 0 ? void 0 : holders.map((holder) => {
		return require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, holder), {}, { products: products === null || products === void 0 ? void 0 : products.filter((product) => (product === null || product === void 0 ? void 0 : product.holder_id) === (holder === null || holder === void 0 ? void 0 : holder.key)) });
	});
};
/**
* @title: 是否是空页面
* @description:
* @param {any} state
* @return {*}
* @Author: WangHan
* @Date: 2024-11-28 16:47
*/
const isEmptyStatus = (state) => {
	const { products, holders, isOpenGroup } = state;
	if (!isOpenGroup && !(products === null || products === void 0 ? void 0 : products.length)) return true;
	if (isOpenGroup && !(holders === null || holders === void 0 ? void 0 : holders.length)) return true;
	return false;
};
/**
* @title: 是否是商品列表页面
* @description:
* @param {any} state
* @return {*}
* @Author: WangHan
* @Date: 2024-11-28 17:23
*/
const isProductList = (state) => {
	const { holders, isOpenGroup } = state;
	if (isOpenGroup && (holders === null || holders === void 0 ? void 0 : holders.length)) return false;
	return true;
};
/**
* @title: 格式化数据最终结果
* @description:
* @param {any} state
* @return {*}
* @Author: WangHan
* @Date: 2024-11-28 18:35
*/
const formatState = (state) => {
	const _default = require_objectSpread2._objectSpread2({}, state);
	const _carts = formatCartList(_default);
	return {
		isEmpty: isEmptyStatus(_default),
		isProductList: isProductList(_default),
		carts: _carts
	};
};
/**
* @title: 格式化服务
* @description:
* @param {any} service
* @param {number} id holder_id
* @return {*}
* @Author: WangHan
* @Date: 2024-12-09 22:47
*/
const formatService = (service, id) => {
	return service.map((item) => {
		return require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, item), {}, { holder_id: id });
	});
};
/**
* 格式化商品列表：合并相同 product_id 的商品，将 product_variant_id 设置为数组
* @param products - 原始商品列表
* @returns 格式化后的商品列表
*/
const formatProductsByVariant = (products) => {
	try {
		if (!products || !Array.isArray(products) || products.length === 0) return [];
		const productMap = /* @__PURE__ */ new Map();
		products.forEach((product) => {
			const productId = product === null || product === void 0 ? void 0 : product.product_id;
			const variantId = product === null || product === void 0 ? void 0 : product.product_variant_id;
			if (!productId) return;
			if (productMap.has(productId)) {
				const existingProduct = productMap.get(productId);
				if (!Array.isArray(existingProduct.product_variant_id)) existingProduct.product_variant_id = [existingProduct.product_variant_id];
				if (variantId !== void 0 && !existingProduct.product_variant_id.includes(variantId)) existingProduct.product_variant_id.push(variantId);
				if (!existingProduct._variants) existingProduct._variants = [existingProduct];
				existingProduct._variants.push(product);
			} else productMap.set(productId, require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, product), {}, {
				product_variant_id: variantId !== void 0 ? [variantId] : [],
				_variants: [product]
			}));
		});
		return Array.from(productMap.values());
	} catch (error) {
		return products || [];
	}
};
/**
* @description: 赠品将商品价格设置为0
* @param {any} product
* @return {*}
*/
const isGiftProductPrice = (product) => {
	return {
		price: 0,
		origin_price: (product === null || product === void 0 ? void 0 : product.price) || 0,
		total: 0,
		origin_total: (product === null || product === void 0 ? void 0 : product.price) || 0,
		base_price: 0,
		_isGift: true
	};
};
/**
* @description: 将赠品的组合规格的价格设置为0
* @param {any} variant
* @return {*}
*/
const giftVariants = (variant) => {
	if (!(variant === null || variant === void 0 ? void 0 : variant.length) || !Array.isArray(variant)) return [];
	return variant === null || variant === void 0 ? void 0 : variant.map((item) => {
		return require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, item), {}, {
			price: 0,
			base_price: 0,
			original_price: 0
		});
	});
};
/**
* @description: 批量转换商品列表数据
* @param {any[]} products - 商品列表
* @param {boolean} isGift - 是否为赠品
* @param {any[]} selectedOriginGifts - 已选择的赠品列表（用于回填数据）
* @return {any[]} 转换后的商品列表
*/
const transformInitProductLists = (products, isGift = false, selectedOriginGifts = []) => {
	if (!products || !Array.isArray(products)) return [];
	return products === null || products === void 0 ? void 0 : products.map((item) => {
		var _selectedGift$_extend, _selectedGift$_extend2;
		const selectedGift = selectedOriginGifts === null || selectedOriginGifts === void 0 ? void 0 : selectedOriginGifts.find((gift) => (gift === null || gift === void 0 ? void 0 : gift.id) === (item === null || item === void 0 ? void 0 : item.id));
		const _skuValue = (selectedGift === null || selectedGift === void 0 || (_selectedGift$_extend = selectedGift._extend) === null || _selectedGift$_extend === void 0 ? void 0 : _selectedGift$_extend._skuValue) || (selectedGift === null || selectedGift === void 0 || (_selectedGift$_extend2 = selectedGift._extend) === null || _selectedGift$_extend2 === void 0 ? void 0 : _selectedGift$_extend2.skuValue) || {};
		let extendParams = {};
		const initExtend = {
			price: (item === null || item === void 0 ? void 0 : item.price) || 0,
			total: (item === null || item === void 0 ? void 0 : item.price) || 0,
			origin_total: (item === null || item === void 0 ? void 0 : item.origin_price) || 0,
			quantity: (item === null || item === void 0 ? void 0 : item.num) || 1
		};
		if (isGift) {
			if (selectedGift) return require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, selectedGift), {}, { _giftInfo: null });
			extendParams = require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, initExtend), {}, {
				base_price: 0,
				_extend: require_objectSpread2._objectSpread2({}, initExtend)
			});
		} else extendParams = { _extend: (item === null || item === void 0 ? void 0 : item._extend) || initExtend };
		return require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, item), extendParams), _skuValue), {}, {
			_isGift: isGift,
			options: (_skuValue === null || _skuValue === void 0 ? void 0 : _skuValue.option) || [],
			num: 1,
			image: (item === null || item === void 0 ? void 0 : item.cover) || "",
			name: (item === null || item === void 0 ? void 0 : item.title) || ""
		});
	});
};
/**
* @description: 弹窗最大可选择数量
* @param {any} currentItem
* @return {*}
*/
const getMaxAvailableCount = (currentItem) => {
	const { products, isGift, giftInfo } = currentItem;
	let count = (currentItem === null || currentItem === void 0 ? void 0 : currentItem.needQuantity) || 1;
	if (isGift) count = (giftInfo === null || giftInfo === void 0 ? void 0 : giftInfo.giftCount) || 1;
	return count;
};
/**
* @description: 计算当前商品编辑弹窗最大可选数量
* @param {any} product 当前商品
* @param {any[]} selected 已选择商品
* @param {any} currentItem 当前配置信息
* @param {boolean} isFilterCurrentProduct 是否过滤当前商品
* @return {*}
*/
const getCurrentMaxProductCount = (product, selected, currentItem, isFilterCurrentProduct = true) => {
	const _maxAvailableCount = getMaxAvailableCount(currentItem);
	if (!Array.isArray(selected) || !(selected === null || selected === void 0 ? void 0 : selected.length)) return _maxAvailableCount;
	let _selectedCount = 0;
	let _selected = selected;
	if (isFilterCurrentProduct) _selected = (selected === null || selected === void 0 ? void 0 : selected.filter((item) => (item === null || item === void 0 ? void 0 : item.value) !== (product === null || product === void 0 ? void 0 : product.id))) || [];
	_selectedCount = _selected === null || _selected === void 0 ? void 0 : _selected.reduce((total, item) => total + ((item === null || item === void 0 ? void 0 : item.quantity) || 1), 0);
	return _maxAvailableCount - _selectedCount;
};
/**
* @description: 获取商品选中数据的初始值
* @param {any} cacheItem 商品数据
* @return {*}
*/
const getProductInitValue = (product, selected, currentItem) => {
	var _extend$other, _product$_extend;
	if (!product) return null;
	if (require_utils.isNormalProductByDurationSchedule(product)) return null;
	if (!isProductNeedSku(product)) return null;
	const _extend = (product === null || product === void 0 ? void 0 : product._extend) || {};
	const _session = (_extend === null || _extend === void 0 || (_extend$other = _extend.other) === null || _extend$other === void 0 ? void 0 : _extend$other.session) || {};
	let params = require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, (_extend === null || _extend === void 0 ? void 0 : _extend.other) || {}), {}, {
		quantity: (product === null || product === void 0 || (_product$_extend = product._extend) === null || _product$_extend === void 0 ? void 0 : _product$_extend.quantity) || 1,
		maxNumber: getCurrentMaxProductCount(product, selected, currentItem)
	});
	if ((_session === null || _session === void 0 ? void 0 : _session.start_at) || (_extend === null || _extend === void 0 ? void 0 : _extend.duration)) params.startEndTime = {
		startDate: _session === null || _session === void 0 ? void 0 : _session.start_at,
		endDate: _session === null || _session === void 0 ? void 0 : _session.end_at,
		duration: (_extend === null || _extend === void 0 ? void 0 : _extend.duration) || 0
	};
	return params;
};
/**
* @description: 格式化商品列表数据
* @param {any} products 商品列表
* @param {any} currentItem 当前配置信息
* @return {*}
*/
const formatProductDatas = (products, currentItem, selected) => {
	if (!products || !Array.isArray(products)) return [];
	const { strategyMetadata, isGift } = currentItem;
	const _isGift = !!isGift;
	let giftParams = {};
	return products === null || products === void 0 ? void 0 : products.map((item) => {
		var _product$_extend2, _product$_extend4, _product$_extend5, _product$_extend6, _product$_extend7, _product$_extend8;
		let product = require_objectSpread2._objectSpread2({}, item);
		if (product === null || product === void 0 || (_product$_extend2 = product._extend) === null || _product$_extend2 === void 0 ? void 0 : _product$_extend2.other) {
			var _product$_extend3;
			product._extend.other.quantity = (product === null || product === void 0 || (_product$_extend3 = product._extend) === null || _product$_extend3 === void 0 ? void 0 : _product$_extend3.quantity) || 1;
		}
		let discount_list;
		let initValue = getProductInitValue(product, selected, currentItem);
		const isGeneral = !!((product === null || product === void 0 || (_product$_extend4 = product._extend) === null || _product$_extend4 === void 0 ? void 0 : _product$_extend4.start_date) && (product === null || product === void 0 || (_product$_extend5 = product._extend) === null || _product$_extend5 === void 0 ? void 0 : _product$_extend5.end_date));
		const _skuValue = (product === null || product === void 0 || (_product$_extend6 = product._extend) === null || _product$_extend6 === void 0 ? void 0 : _product$_extend6.skuValue) || (product === null || product === void 0 || (_product$_extend7 = product._extend) === null || _product$_extend7 === void 0 ? void 0 : _product$_extend7._skuValue) || {};
		const total = require_utils$1.getProductTotalPrice(product);
		let originTotal = product === null || product === void 0 || (_product$_extend8 = product._extend) === null || _product$_extend8 === void 0 ? void 0 : _product$_extend8.origin_total;
		if (_isGift && !originTotal) originTotal = product === null || product === void 0 ? void 0 : product._initPrice;
		if (_isGift) {
			discount_list = formatDiscountLists(product, strategyMetadata === null || strategyMetadata === void 0 ? void 0 : strategyMetadata.name);
			giftParams = {
				_initPrice: product === null || product === void 0 ? void 0 : product._initPrice,
				original_price: product === null || product === void 0 ? void 0 : product._initPrice,
				discount_list
			};
		}
		return require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, product), giftParams), {}, {
			_extend: require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, (product === null || product === void 0 ? void 0 : product._extend) || {}), {}, {
				_skuValue,
				total: Number(total) || 0,
				origin_total: originTotal || void 0
			}),
			_id: (product === null || product === void 0 ? void 0 : product._id) || (0, _pisell_utils.getUniqueId)(),
			_isGeneral: isGeneral,
			initValue,
			_isGiftPromotion: _isGift
		});
	});
};
/**
* @description: 更新商品列表中的某个商品
* @param {any[]} products - 商品列表
* @param {string} productId - 要更新的商品 ID
* @param {any} updates - 要更新的数据
* @return {any[]} 更新后的商品列表
*/
const updateProductInList = (props) => {
	try {
		const { products, productId, cacheItem } = props;
		return { originProducts: (products || []).map((item) => {
			if (item.id === productId) return require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, cacheItem), {}, { _initPrice: item === null || item === void 0 ? void 0 : item._initPrice });
			return item;
		}) };
	} catch (error) {
		console.error("updateProductInList error:", error);
		return {
			originProducts: [],
			cartProducts: []
		};
	}
};
const formatCartProducts = (props) => {
	try {
		const { products, currentItem } = props;
		const { state, translationOriginal } = currentItem;
		if (!Array.isArray(products)) return [];
		return require_utils.formatProducts(products, state, false, false, false, translationOriginal);
	} catch (error) {
		return [];
	}
};
/**
* @description: 检查商品是否需要选择规格
* @param {any} product - 商品数据
* @return {boolean} 是否需要选择规格
*/
const isProductNeedSku = (product) => {
	return !!(product === null || product === void 0 ? void 0 : product.is_eject);
};
/**
* @description: 从商品列表中查找商品
* @param {any[]} products - 商品列表
* @param {string} productId - 商品 ID
* @return {any} 找到的商品或 undefined
*/
const findProductById = (products, productId) => {
	const _id = (productId === null || productId === void 0 ? void 0 : productId.value) || productId;
	return (products || []).find((item) => item.id === _id);
};
/**
* @description: 从列表中查找匹配的商品
*/
const findMatchedProductFromList = (products, targetProduct) => {
	if (!products || !Array.isArray(products) || !targetProduct) return;
	const targetProductId = targetProduct.product_id || targetProduct.id;
	return products.find((p) => {
		return (p.product_id || p.id) === targetProductId;
	});
};
/**
* @description: 提取并标准化商品的 product_variant_id 为数组
*/
const extractVariantIds = (product) => {
	if (!(product === null || product === void 0 ? void 0 : product.product_variant_id)) return [];
	return Array.isArray(product.product_variant_id) ? product.product_variant_id : [product.product_variant_id];
};
/**
* @description: 根据允许的规格ID列表过滤商品的 variant 数据
*/
const filterProductVariants = (product, allowedVariantIds) => {
	if (!product) return product;
	if (!allowedVariantIds || allowedVariantIds.length === 0) return product;
	if (!product.variant || !Array.isArray(product.variant) || product.variant.length === 0) return product;
	const filteredVariants = product.variant.filter((variant) => allowedVariantIds.includes(variant.id));
	if (filteredVariants.length === 0) return product;
	return require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, product), {}, {
		variant: filteredVariants,
		variant_count: filteredVariants.length,
		_originalVariantCount: product.variant.length
	});
};
/**
* @description: 根据过滤后的 variant 重新生成 variant_group
*/
const filterVariantGroupByVariants = (product) => {
	if (!(product === null || product === void 0 ? void 0 : product.variant_group) || !Array.isArray(product.variant_group) || product.variant_group.length === 0) return product;
	if (!(product === null || product === void 0 ? void 0 : product.variant) || !Array.isArray(product.variant) || product.variant.length === 0) return product;
	const usedValueIds = /* @__PURE__ */ new Set();
	product.variant.forEach((variant) => {
		if (variant.sku_ids) {
			const matches = variant.sku_ids.match(/:\d+/g);
			if (matches) matches.forEach((match) => {
				const valueId = parseInt(match.substring(1));
				usedValueIds.add(valueId);
			});
		}
	});
	return product.variant_group.map((group) => {
		if (!group.variant_item || !Array.isArray(group.variant_item)) return group;
		const filteredValues = group.variant_item.filter((value) => usedValueIds.has(value.id));
		return require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, group), {}, { variant_item: filteredValues });
	}).filter((group) => group.variant_item && group.variant_item.length > 0) || [];
};
/**
* @description: 根据列表数据中找到匹配的商品，并提取允许的规格 ID
*/
const filterProductByPromotionRules = (product, currentItem) => {
	if (!product || !(currentItem === null || currentItem === void 0 ? void 0 : currentItem.products) || !Array.isArray(currentItem.products)) return product;
	const matchedProduct = findMatchedProductFromList(currentItem.products, product);
	if (!matchedProduct) return product;
	const allowedVariantIds = extractVariantIds(matchedProduct);
	if (allowedVariantIds.length === 0) return product;
	return filterProductVariants(product, allowedVariantIds);
};
/**
* @description: 根据 selected ID(s) 从 products 中查找对应的商品
* @param {any[]} products - 商品列表
* @param {string | string[]} selected - 选中的商品 ID 或 ID 数组
* @return {any[]} 匹配的商品数组
*/
const findSelectedProductsFromList = (products, selected) => {
	if (!products || !products.length || !selected) return [];
	if (Array.isArray(selected)) return products.filter((product) => selected.includes(product === null || product === void 0 ? void 0 : product.id));
	const selectedProduct = products.find((product) => (product === null || product === void 0 ? void 0 : product.id) === selected);
	return selectedProduct ? [selectedProduct] : [];
};
/**
* @description: 设置赠品discount_list数据
* @param {any} product
* @param {any} strategyName
* @return {*}
*/
const formatDiscountLists = (product, strategyName) => {
	try {
		const _amount = Number(product === null || product === void 0 ? void 0 : product._initPrice) || 0;
		return [{
			type: "promotion",
			amount: _amount,
			discount: {
				original_amount: _amount,
				fixed_amount: _amount,
				title: strategyName || "",
				resource_id: ""
			}
		}];
	} catch (error) {
		return [];
	}
};
const getPureSourceProductId = (sourceProductId) => {
	if (!sourceProductId) return "";
	if (typeof sourceProductId !== "string") return "";
	return sourceProductId.replace(/___.*/, "");
};
//#endregion
exports.filterProductByPromotionRules = filterProductByPromotionRules;
exports.filterVariantGroupByVariants = filterVariantGroupByVariants;
exports.findProductById = findProductById;
exports.findSelectedProductsFromList = findSelectedProductsFromList;
exports.formatCartProducts = formatCartProducts;
exports.formatDiscountLists = formatDiscountLists;
exports.formatProductDatas = formatProductDatas;
exports.formatProductsByVariant = formatProductsByVariant;
exports.formatService = formatService;
exports.formatState = formatState;
exports.getCurrentMaxProductCount = getCurrentMaxProductCount;
exports.getMaxAvailableCount = getMaxAvailableCount;
exports.getPureSourceProductId = getPureSourceProductId;
exports.giftVariants = giftVariants;
exports.isGiftProductPrice = isGiftProductPrice;
exports.isProductNeedSku = isProductNeedSku;
exports.transformInitProductLists = transformInitProductLists;
exports.updateProductInList = updateProductInList;
