import store from "../store/store.js";
//#region src/plus/skuOptionsSelection/utils/checkSkuStatus.ts
/**
* @title 检查SKU库存是否可以售卖
* @description
* 首先受到 SKU 的 is_track 字段的影响，如果 is_track 为 0，表示不跟踪库存，则不检查库存
* 其次受到 SKU 的 over_sold 字段 和 config.overSold 配置的影响，如果 over_sold 为 1 或 config.overSold 为 true，表示允许超卖，则不检查库存
* 最后受到 SKU 的 stock_quantity 字段的影响，如果 stock_quantity 小于等于 0，表示库存不足
*/
const checkSkuStockCanSell = (sku) => {
	let canSell = true;
	let hasStock = true;
	if ((sku === null || sku === void 0 ? void 0 : sku.is_track) === 1) {
		var _store$getState$confi;
		if ((sku === null || sku === void 0 ? void 0 : sku.over_sold) === 1 || ((_store$getState$confi = store.getState().config) === null || _store$getState$confi === void 0 ? void 0 : _store$getState$confi.overSold)) {
			canSell = true;
			hasStock = Number((sku === null || sku === void 0 ? void 0 : sku.stock_quantity) || 0) > 0;
		} else if (Number((sku === null || sku === void 0 ? void 0 : sku.stock_quantity) || 0) <= 0) {
			canSell = false;
			hasStock = false;
		}
	} else {
		canSell = true;
		hasStock = true;
	}
	return {
		canSell,
		hasStock
	};
};
/**
* @title 检查SKU是否禁用
* @description 如果SKU被禁用，或者库存不足不能售卖，则返回 true
*/
const checkSkuDisabled = (sku, canSell = true) => {
	return (sku === null || sku === void 0 ? void 0 : sku.is_enabled) === 0 || !canSell;
};
/**
* @title 组合规格库存检查（初始化时使用）
* @description 检查包含该规格选项的所有组合商品的库存状态
*/
const checkVariantStockCanSell = (sku) => {
	const dataSource = store.getState().dataSource;
	const variant = (dataSource === null || dataSource === void 0 ? void 0 : dataSource.variant) || [];
	const variantSkuId = `${sku === null || sku === void 0 ? void 0 : sku.variant_group_id}:${sku === null || sku === void 0 ? void 0 : sku.id}`;
	const matchedVariants = variant.filter((item) => {
		var _item$sku_ids;
		return ((item === null || item === void 0 || (_item$sku_ids = item.sku_ids) === null || _item$sku_ids === void 0 ? void 0 : _item$sku_ids.split("_")) || []).includes(variantSkuId);
	});
	if (matchedVariants.length === 0) return {
		canSell: false,
		hasStock: false
	};
	const results = matchedVariants.map((item) => checkSkuStockCanSell(item));
	return {
		canSell: results.some((r) => r.canSell),
		hasStock: results.some((r) => r.hasStock)
	};
};
/**
* @title 根据已经选择的规格选项，筛选出包含对应规格的商品并进行库存检查
* @description
* 1. 收集当前判断的规格 + 其他规格组已选的规格（排除当前规格组）
* 2. 根据收集的规格，筛选出包含对应规格的组合商品
* 3. 对筛选出的商品进行库存检查
* 4. 只要有一个匹配商品能售卖，则该选项可选
* 5. 只要有一个匹配商品有库存，则显示有库存
*/
const checkVariantStockByValue = (sku) => {
	var _dataSource$variant_g;
	const dataSource = store.getState().dataSource;
	const variant = (dataSource === null || dataSource === void 0 ? void 0 : dataSource.variant) || [];
	const selectorValue = store.getState().selectorValue;
	const selectedVariantIds = [`${sku === null || sku === void 0 ? void 0 : sku.variant_group_id}:${sku === null || sku === void 0 ? void 0 : sku.id}`];
	dataSource === null || dataSource === void 0 || (_dataSource$variant_g = dataSource.variant_group) === null || _dataSource$variant_g === void 0 || _dataSource$variant_g.forEach((group) => {
		var _selectorValue$group$;
		const _selectedData = selectorValue === null || selectorValue === void 0 || (_selectorValue$group$ = selectorValue[group.id]) === null || _selectorValue$group$ === void 0 ? void 0 : _selectorValue$group$.data;
		if (_selectedData && group.id !== (sku === null || sku === void 0 ? void 0 : sku.variant_group_id)) selectedVariantIds.push(`${_selectedData === null || _selectedData === void 0 ? void 0 : _selectedData.variant_group_id}:${_selectedData === null || _selectedData === void 0 ? void 0 : _selectedData.id}`);
	});
	const filteredVariant = variant.filter((item) => {
		var _item$sku_ids2;
		const ids = (item === null || item === void 0 || (_item$sku_ids2 = item.sku_ids) === null || _item$sku_ids2 === void 0 ? void 0 : _item$sku_ids2.split("_")) || [];
		return selectedVariantIds.every((selectedId) => ids.includes(selectedId));
	});
	const _canSellResult = [];
	const _hasStockResult = [];
	filteredVariant.forEach((item) => {
		const { canSell, hasStock } = checkSkuStockCanSell(item);
		_canSellResult.push(canSell);
		_hasStockResult.push(hasStock);
	});
	return {
		canSell: _canSellResult.some((item) => item === true),
		hasStock: _hasStockResult.some((item) => item === true)
	};
};
//#endregion
export { checkSkuDisabled, checkSkuStockCanSell, checkVariantStockByValue, checkVariantStockCanSell };
