require("../../_virtual/_rolldown/runtime.js");
let _pisell_utils = require("@pisell/utils");
//#region src/pro/skuDetailModal/utils.ts
const checkSkuStockCanSell = (sku) => {
	let canSell = true;
	let hasStock = true;
	if ((sku === null || sku === void 0 ? void 0 : sku.is_track) === 1) {
		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
	};
};
const checkProductStock = (params) => {
	var _value$bundle;
	const value = params === null || params === void 0 ? void 0 : params.value;
	const data = params === null || params === void 0 ? void 0 : params.data;
	if (!data) return true;
	if (value === null || value === void 0 ? void 0 : value.product_variant_id) {
		var _variant;
		if (!checkSkuStockCanSell(data === null || data === void 0 || (_variant = data.variant) === null || _variant === void 0 ? void 0 : _variant.find((item) => Number(item === null || item === void 0 ? void 0 : item.id) === Number(value.product_variant_id))).hasStock) return false;
	}
	if ((value === null || value === void 0 || (_value$bundle = value.bundle) === null || _value$bundle === void 0 ? void 0 : _value$bundle.length) > 0) {
		if (!(value === null || value === void 0 ? void 0 : value.bundle.every((item) => {
			return checkSkuStockCanSell(item).hasStock;
		}))) return false;
	}
	if (value === null || value === void 0 ? void 0 : value.product_variant_id) return true;
	if (!checkSkuStockCanSell(data).hasStock) return false;
	return true;
};
/**
* @description 将已选 SKU 信息格式化为可展示的多行文本
*/
const formatSelectedSkuMessage = (skuValue, currencySymbol = "") => {
	if (!skuValue) return "";
	const lines = [];
	const optionIndent = "\xA0\xA0";
	const optionList = Array.isArray(skuValue === null || skuValue === void 0 ? void 0 : skuValue.option) ? skuValue.option : Array.isArray(skuValue === null || skuValue === void 0 ? void 0 : skuValue.options) ? skuValue.options : [];
	if (skuValue === null || skuValue === void 0 ? void 0 : skuValue.product_option_string) lines.push(String(skuValue.product_option_string));
	optionList.forEach((item) => {
		if (!(item === null || item === void 0 ? void 0 : item.name)) return;
		const quantity = Number((item === null || item === void 0 ? void 0 : item.num) || 1);
		const priceText = (item === null || item === void 0 ? void 0 : item.price) !== void 0 && (item === null || item === void 0 ? void 0 : item.price) !== null && (item === null || item === void 0 ? void 0 : item.price) !== "" ? `  ${(0, _pisell_utils.formatAmount)(item === null || item === void 0 ? void 0 : item.price, 2, currencySymbol)}` : "";
		lines.push(`${item.name}${priceText} x ${quantity}`);
	});
	if (Array.isArray(skuValue === null || skuValue === void 0 ? void 0 : skuValue.bundle) && skuValue.bundle.length > 0) skuValue.bundle.forEach((item) => {
		if (!(item === null || item === void 0 ? void 0 : item.title)) return;
		const quantity = Number((item === null || item === void 0 ? void 0 : item.num) || 1);
		const priceText = (item === null || item === void 0 ? void 0 : item.price) !== void 0 && (item === null || item === void 0 ? void 0 : item.price) !== null && (item === null || item === void 0 ? void 0 : item.price) !== "" ? `  ${(0, _pisell_utils.formatAmount)(item === null || item === void 0 ? void 0 : item.price, 2, currencySymbol)}` : "";
		lines.push(`${item.title}${priceText} x ${quantity}`);
		(Array.isArray(item === null || item === void 0 ? void 0 : item.options) ? item.options : Array.isArray(item === null || item === void 0 ? void 0 : item.option) ? item.option : []).forEach((opt) => {
			if (!(opt === null || opt === void 0 ? void 0 : opt.name)) return;
			const optionQuantity = Number((opt === null || opt === void 0 ? void 0 : opt.num) || 1);
			const optionPriceText = (opt === null || opt === void 0 ? void 0 : opt.price) !== void 0 && (opt === null || opt === void 0 ? void 0 : opt.price) !== null && (opt === null || opt === void 0 ? void 0 : opt.price) !== "" ? `  ${(0, _pisell_utils.formatAmount)(opt === null || opt === void 0 ? void 0 : opt.price, 2, currencySymbol)}` : "";
			lines.push(`${optionIndent}${opt.name}${optionPriceText} x ${optionQuantity}`);
		});
	});
	return lines.join("\n");
};
/**
* @description 将已选 SKU 信息格式化为结构化列表，便于逐项渲染样式
*/
const formatSelectedSkuItems = (skuValue, currencySymbol = "") => {
	if (!skuValue) return [];
	const items = [];
	const optionList = Array.isArray(skuValue === null || skuValue === void 0 ? void 0 : skuValue.option) ? skuValue.option : Array.isArray(skuValue === null || skuValue === void 0 ? void 0 : skuValue.options) ? skuValue.options : [];
	if (skuValue === null || skuValue === void 0 ? void 0 : skuValue.product_option_string) items.push({
		key: `variant-${skuValue.product_option_string}`,
		type: "variant",
		label: String(skuValue.product_option_string)
	});
	optionList.forEach((item, index) => {
		var _item$id;
		if (!(item === null || item === void 0 ? void 0 : item.name)) return;
		const quantity = Number((item === null || item === void 0 ? void 0 : item.num) || 1);
		const hasPrice = (item === null || item === void 0 ? void 0 : item.price) !== void 0 && (item === null || item === void 0 ? void 0 : item.price) !== null && (item === null || item === void 0 ? void 0 : item.price) !== "";
		items.push({
			key: `option-${(_item$id = item === null || item === void 0 ? void 0 : item.id) !== null && _item$id !== void 0 ? _item$id : index}-${item === null || item === void 0 ? void 0 : item.name}`,
			type: "option",
			label: item.name,
			meta: `${hasPrice ? `${(0, _pisell_utils.formatAmount)(item === null || item === void 0 ? void 0 : item.price, 2, currencySymbol)} ` : ""}x ${quantity}`
		});
	});
	if (Array.isArray(skuValue === null || skuValue === void 0 ? void 0 : skuValue.bundle) && skuValue.bundle.length > 0) skuValue.bundle.forEach((item, index) => {
		var _item$id2;
		if (!(item === null || item === void 0 ? void 0 : item.title)) return;
		const quantity = Number((item === null || item === void 0 ? void 0 : item.num) || 1);
		const hasPrice = (item === null || item === void 0 ? void 0 : item.price) !== void 0 && (item === null || item === void 0 ? void 0 : item.price) !== null && (item === null || item === void 0 ? void 0 : item.price) !== "";
		items.push({
			key: `bundle-${(_item$id2 = item === null || item === void 0 ? void 0 : item.id) !== null && _item$id2 !== void 0 ? _item$id2 : index}-${item === null || item === void 0 ? void 0 : item.title}`,
			type: "bundle",
			label: item.title,
			meta: `${hasPrice ? `${(0, _pisell_utils.formatAmount)(item === null || item === void 0 ? void 0 : item.price, 2, currencySymbol)} ` : ""}x ${quantity}`,
			children: []
		});
		const bundleDisplayItem = items[items.length - 1];
		(Array.isArray(item === null || item === void 0 ? void 0 : item.options) ? item.options : Array.isArray(item === null || item === void 0 ? void 0 : item.option) ? item.option : []).forEach((opt, optionIndex) => {
			var _bundleDisplayItem$ch, _item$id3, _opt$id;
			if (!(opt === null || opt === void 0 ? void 0 : opt.name)) return;
			const optionQuantity = Number((opt === null || opt === void 0 ? void 0 : opt.num) || 1);
			const optionHasPrice = (opt === null || opt === void 0 ? void 0 : opt.price) !== void 0 && (opt === null || opt === void 0 ? void 0 : opt.price) !== null && (opt === null || opt === void 0 ? void 0 : opt.price) !== "";
			(_bundleDisplayItem$ch = bundleDisplayItem.children) === null || _bundleDisplayItem$ch === void 0 || _bundleDisplayItem$ch.push({
				key: `bundle-option-${(_item$id3 = item === null || item === void 0 ? void 0 : item.id) !== null && _item$id3 !== void 0 ? _item$id3 : index}-${(_opt$id = opt === null || opt === void 0 ? void 0 : opt.id) !== null && _opt$id !== void 0 ? _opt$id : optionIndex}-${opt === null || opt === void 0 ? void 0 : opt.name}`,
				type: "bundleOption",
				label: opt.name,
				meta: `${optionHasPrice ? `${(0, _pisell_utils.formatAmount)(opt === null || opt === void 0 ? void 0 : opt.price, 2, currencySymbol)} ` : ""}x ${optionQuantity}`
			});
		});
	});
	return items;
};
const getStockValidationResult = (params) => {
	var _params$bookingConfig, _params$bookingConfig2, _params$bookingConfig3, _params$bookingConfig4, _params$bookingConfig5, _params$bookingConfig6, _params$data, _params$data2;
	const stockValidation = Number((_params$bookingConfig = params === null || params === void 0 || (_params$bookingConfig2 = params.bookingConfig) === null || _params$bookingConfig2 === void 0 || (_params$bookingConfig2 = _params$bookingConfig2.stock_setting) === null || _params$bookingConfig2 === void 0 ? void 0 : _params$bookingConfig2.stock_validation) !== null && _params$bookingConfig !== void 0 ? _params$bookingConfig : 0);
	const allowAddToCartWhenOutStock = Number((_params$bookingConfig3 = params === null || params === void 0 || (_params$bookingConfig4 = params.bookingConfig) === null || _params$bookingConfig4 === void 0 || (_params$bookingConfig4 = _params$bookingConfig4.stock_setting) === null || _params$bookingConfig4 === void 0 ? void 0 : _params$bookingConfig4.allow_add_to_cart_when_out_stock) !== null && _params$bookingConfig3 !== void 0 ? _params$bookingConfig3 : 0);
	const enableDontShowAgainToday = Number((_params$bookingConfig5 = params === null || params === void 0 || (_params$bookingConfig6 = params.bookingConfig) === null || _params$bookingConfig6 === void 0 || (_params$bookingConfig6 = _params$bookingConfig6.stock_setting) === null || _params$bookingConfig6 === void 0 ? void 0 : _params$bookingConfig6.do_not_show_again_today) !== null && _params$bookingConfig5 !== void 0 ? _params$bookingConfig5 : 0);
	const productId = Number((params === null || params === void 0 || (_params$data = params.data) === null || _params$data === void 0 ? void 0 : _params$data.product_id) || (params === null || params === void 0 || (_params$data2 = params.data) === null || _params$data2 === void 0 ? void 0 : _params$data2.id) || 0);
	if (stockValidation !== 1) return {
		stockValidation,
		allowAddToCartWhenOutStock,
		enableDontShowAgainToday,
		productId,
		hasStock: true,
		shouldBlock: false,
		shouldShowAlert: false
	};
	const hasStock = checkProductStock({
		value: params === null || params === void 0 ? void 0 : params.value,
		data: params === null || params === void 0 ? void 0 : params.data
	});
	if (hasStock) return {
		stockValidation,
		allowAddToCartWhenOutStock,
		enableDontShowAgainToday,
		productId,
		hasStock,
		shouldBlock: false,
		shouldShowAlert: false
	};
	const shouldSkipAlert = allowAddToCartWhenOutStock === 1 && enableDontShowAgainToday === 1 && hasHiddenStockAlertForToday({ productId });
	return {
		stockValidation,
		allowAddToCartWhenOutStock,
		enableDontShowAgainToday,
		productId,
		hasStock,
		shouldBlock: allowAddToCartWhenOutStock !== 1,
		shouldShowAlert: allowAddToCartWhenOutStock === 1 && !shouldSkipAlert
	};
};
function getLocalDayKey() {
	const now = /* @__PURE__ */ new Date();
	return `${String(now.getFullYear())}-${String(now.getMonth() + 1).padStart(2, "0")}-${String(now.getDate()).padStart(2, "0")}`;
}
function getStockAlertStorageKey(params) {
	return `pisell2_stock_alert_hidden_${params.productId}`;
}
function hasHiddenStockAlertForToday(params) {
	try {
		if (!params.productId) return false;
		if (typeof window === "undefined") return false;
		const key = getStockAlertStorageKey(params);
		return window.localStorage.getItem(key) === getLocalDayKey();
	} catch (error) {
		return false;
	}
}
function setHiddenStockAlertForToday(params) {
	try {
		if (!params.productId) return;
		if (typeof window === "undefined") return;
		const key = getStockAlertStorageKey(params);
		window.localStorage.setItem(key, getLocalDayKey());
	} catch (error) {}
}
//#endregion
exports.formatSelectedSkuItems = formatSelectedSkuItems;
exports.formatSelectedSkuMessage = formatSelectedSkuMessage;
exports.getStockValidationResult = getStockValidationResult;
exports.setHiddenStockAlertForToday = setHiddenStockAlertForToday;
