const require_runtime = require("../../../../_virtual/_rolldown/runtime.js");
let decimal_js = require("decimal.js");
decimal_js = require_runtime.__toESM(decimal_js);
//#region src/pro/skuDetailModal/components/weighing/utils.ts
const CUSTOM_TARE_STORAGE_KEY = "pisell-weighing-custom-tares";
const DEFAULT_TARE_STORAGE_KEY = "pisell-weighing-default-tare";
/** 插件返回 3 位小数精度，展示与其保持一致。 */
const formatWeight = (value) => Number(value || 0).toFixed(3);
/** @param symbol 货币符号，默认 '$' */
const formatMoney = (value, symbol = "$") => `${symbol}${Number(value || 0).toFixed(2)}`;
/** 过滤非数字字符，保证只有一个小数点。 */
const sanitizeManualWeight = (value) => {
	const [integerPart, ...decimalParts] = value.replace(/[^0-9.]/g, "").split(".");
	if (!decimalParts.length) return integerPart;
	return `${integerPart}.${decimalParts.join("")}`;
};
/**
* 为 initialValue.tare 还原项生成确定性 ID，保证 customTareOptions 初始化与
* selectedTare 初始化使用同一个 ID，不需要在两处传递中间状态。
*/
const getRestoredTareId = (tare) => `restored-${tare.label.replace(/\s+/g, "-")}-${tare.value}`;
const readCustomTares = () => {
	if (typeof window === "undefined") return [];
	try {
		const rawValue = window.localStorage.getItem(CUSTOM_TARE_STORAGE_KEY);
		if (!rawValue) return [];
		const parsedValue = JSON.parse(rawValue);
		if (!Array.isArray(parsedValue)) return [];
		return parsedValue.filter((item) => {
			const hasBasicShape = item && typeof item.id === "string" && typeof item.label === "string" && typeof item.value === "number";
			const hasValidUnit = typeof (item === null || item === void 0 ? void 0 : item.unit) === "undefined" || item.unit === "KILOGRAMS" || item.unit === "GRAMS" || item.unit === "POUNDS" || item.unit === "OUNCES";
			return hasBasicShape && hasValidUnit;
		});
	} catch (_unused) {
		return [];
	}
};
const readDefaultTareId = () => {
	if (typeof window === "undefined") return;
	try {
		return window.localStorage.getItem("pisell-weighing-default-tare") || void 0;
	} catch (_unused2) {
		return;
	}
};
const PRODUCT_TARE_MAP_KEY = "pisell-weighing-product-tare-map";
/**
* 构造商品 + 规格的皮重偏好存储 key。
* 无 variantId 时 key 形如 `{productId}:`，有 variantId 时形如 `{productId}:{variantId}`。
*/
const getProductTareKey = (productId, variantId) => `${productId}:${variantId !== null && variantId !== void 0 ? variantId : ""}`;
/**
* 读取商品皮重偏好映射表（productKey → tareId）。
*/
const readProductTareMap = () => {
	if (typeof window === "undefined") return {};
	try {
		const raw = window.localStorage.getItem(PRODUCT_TARE_MAP_KEY);
		if (!raw) return {};
		const parsed = JSON.parse(raw);
		return typeof parsed === "object" && parsed !== null ? parsed : {};
	} catch (_unused3) {
		return {};
	}
};
/**
* 保存某个商品 + 规格的皮重偏好。
*/
const saveProductTarePref = (productKey, tareId) => {
	if (typeof window === "undefined") return;
	try {
		const map = readProductTareMap();
		map[productKey] = tareId;
		window.localStorage.setItem(PRODUCT_TARE_MAP_KEY, JSON.stringify(map));
	} catch (_unused4) {}
};
/** 每种重量单位对应的展示缩写。 */
const UNIT_LABEL = {
	KILOGRAMS: "kg",
	GRAMS: "g",
	POUNDS: "lb",
	OUNCES: "oz"
};
/**
* 判断商品是否为称重商品（按重量计价，依赖秤读数结算）。
* 对应后台字段 open_sold_weight === 1。
*/
const isWeighingProduct = (productData) => {
	return Number((productData === null || productData === void 0 ? void 0 : productData.open_sold_weight) || 0) === 1;
};
/**
* 按 variantId 在 productData.variant[] 中定位当前选中的 variant，
* 若存在则返回 variant 级数据，否则降级到商品根级。
*
* 用于称重相关字段（open_sold_weight / unit / unit_value）的数据源解析，
* 确保多规格商品中每个 variant 可以独立配置称重信息。
*/
const resolveWeighingSource = (productData, variantId) => {
	var _variants$find;
	const pd = productData;
	const variants = (pd === null || pd === void 0 ? void 0 : pd.variant) || [];
	const activeVariant = variantId ? (_variants$find = variants.find((v) => Number(v.id) === Number(variantId))) !== null && _variants$find !== void 0 ? _variants$find : null : null;
	return activeVariant !== null && activeVariant !== void 0 ? activeVariant : pd;
};
const getWeighedUnitPrice = (params) => {
	const { weighedTotalAmount, productData, skuValue } = params;
	if (!productData) return 0;
	let price = new decimal_js.default(weighedTotalAmount !== null && weighedTotalAmount !== void 0 ? weighedTotalAmount : 0);
	if (Array.isArray(skuValue === null || skuValue === void 0 ? void 0 : skuValue.bundle) && skuValue.bundle.length > 0) price = skuValue.bundle.reduce((accumulator, currentValue) => {
		var _ref, _currentValue$origina;
		const currentPrice = new decimal_js.default((_ref = (_currentValue$origina = currentValue === null || currentValue === void 0 ? void 0 : currentValue.original_price) !== null && _currentValue$origina !== void 0 ? _currentValue$origina : currentValue === null || currentValue === void 0 ? void 0 : currentValue.price) !== null && _ref !== void 0 ? _ref : 0);
		const currentNum = new decimal_js.default((currentValue === null || currentValue === void 0 ? void 0 : currentValue.num) || 0);
		return accumulator.add(currentPrice.mul(currentNum));
	}, price);
	if (Array.isArray(skuValue === null || skuValue === void 0 ? void 0 : skuValue.option) && skuValue.option.length > 0) price = skuValue.option.reduce((accumulator, currentValue) => {
		var _ref2, _currentValue$origina2;
		const currentPrice = new decimal_js.default((_ref2 = (_currentValue$origina2 = currentValue === null || currentValue === void 0 ? void 0 : currentValue.original_price) !== null && _currentValue$origina2 !== void 0 ? _currentValue$origina2 : currentValue === null || currentValue === void 0 ? void 0 : currentValue.price) !== null && _ref2 !== void 0 ? _ref2 : 0);
		const currentNum = new decimal_js.default((currentValue === null || currentValue === void 0 ? void 0 : currentValue.num) || 0);
		return accumulator.add(currentPrice.mul(currentNum));
	}, price);
	return price.toNumber();
};
//#endregion
exports.CUSTOM_TARE_STORAGE_KEY = CUSTOM_TARE_STORAGE_KEY;
exports.DEFAULT_TARE_STORAGE_KEY = DEFAULT_TARE_STORAGE_KEY;
exports.UNIT_LABEL = UNIT_LABEL;
exports.formatMoney = formatMoney;
exports.formatWeight = formatWeight;
exports.getProductTareKey = getProductTareKey;
exports.getRestoredTareId = getRestoredTareId;
exports.getWeighedUnitPrice = getWeighedUnitPrice;
exports.isWeighingProduct = isWeighingProduct;
exports.readCustomTares = readCustomTares;
exports.readDefaultTareId = readDefaultTareId;
exports.readProductTareMap = readProductTareMap;
exports.resolveWeighingSource = resolveWeighingSource;
exports.sanitizeManualWeight = sanitizeManualWeight;
exports.saveProductTarePref = saveProductTarePref;
