import { getServiceTime } from "./formatTime.js";
import { formatAmount, locales } from "@pisell/utils";
//#region src/components/ticketBooking/utils/formatProductMessage.ts
const UNIT_LABEL = {
	KILOGRAMS: "kg",
	GRAMS: "g",
	POUNDS: "lb",
	OUNCES: "oz"
};
const DEFAULT_WEIGHT_PRECISION = 3;
const formatWeightValue = (value) => {
	const parsedValue = Number(value);
	return (Number.isFinite(parsedValue) ? parsedValue : 0).toFixed(DEFAULT_WEIGHT_PRECISION);
};
const formatProductMessage = (product, utils) => {
	var _product$_origin;
	const { product_option_string, options, bundle, currency_symbol, start_date, end_date, holder_id, holders, multiResources } = product;
	let str = "";
	if (start_date && end_date) str += `${getServiceTime(product, start_date, utils)} \n`;
	if (holder_id && Array.isArray(holders)) {
		const targetHolder = holders.find((holder) => holder.id === holder_id);
		if (targetHolder === null || targetHolder === void 0 ? void 0 : targetHolder.label) str += `${targetHolder.label} \n`;
	}
	if (Array.isArray(multiResources) && multiResources.length > 0) multiResources.forEach((resource) => {
		const { id: ids, lists } = resource;
		if (Array.isArray(ids)) ids.forEach((id) => {
			const targetRes = lists.find((l) => l.id === id);
			if (targetRes === null || targetRes === void 0 ? void 0 : targetRes.label) str += `${targetRes === null || targetRes === void 0 ? void 0 : targetRes.label} \n`;
		});
		else {
			const targetRes = lists.find((l) => l.id === ids);
			if (targetRes === null || targetRes === void 0 ? void 0 : targetRes.label) str += `${targetRes === null || targetRes === void 0 ? void 0 : targetRes.label} \n`;
		}
	});
	const weighing = product === null || product === void 0 || (_product$_origin = product._origin) === null || _product$_origin === void 0 || (_product$_origin = _product$_origin._extend) === null || _product$_origin === void 0 ? void 0 : _product$_origin.weighing;
	if (weighing) {
		const { mode, unit_price, unit, weight, tare, net_weight } = weighing;
		const unitLabel = UNIT_LABEL[unit] || unit;
		const weightText = formatWeightValue(weight);
		const tareText = formatWeightValue(tare === null || tare === void 0 ? void 0 : tare.value);
		const netWeightText = formatWeightValue(net_weight);
		const tareValue = Number((tare === null || tare === void 0 ? void 0 : tare.value) || 0);
		const isManualWeight = mode === "manual";
		const unitPriceText = `${currency_symbol || ""}${unit_price !== null && unit_price !== void 0 ? unit_price : "0.00"}`;
		if (isManualWeight) str += `${locales.getText("pisell2.cart.sku-card.weighing.manual")} \n`;
		if (tareValue > 0) str += `${locales.getText("pisell2.cart.sku-card.weighing.gross")}: ${weightText} ${unitLabel} \n`;
		if (tareValue > 0) str += `${locales.getText("pisell2.cart.sku-card.weighing.tare")}: ${tareText} ${UNIT_LABEL[tare === null || tare === void 0 ? void 0 : tare.unit] || (tare === null || tare === void 0 ? void 0 : tare.unit) || unitLabel} \n`;
		str += `${netWeightText} ${unitLabel} ${locales.getText("pisell2.cart.sku-card.weighing.net")} @ ${unitPriceText}/${unitLabel} \n`;
	}
	if (product_option_string) str += `${product_option_string}\n`;
	if (Array.isArray(options) && options.length) options.forEach((opt) => {
		str += `${opt.name}  ${formatAmount(opt === null || opt === void 0 ? void 0 : opt.price, 2, currency_symbol)} x ${opt.num}\n`;
	});
	if (Array.isArray(bundle) && bundle.length) bundle.forEach((item) => {
		str += `${item.title}  ${formatAmount(item.price, 2, currency_symbol)} x ${item.num}\n`;
		const bundleOptions = item === null || item === void 0 ? void 0 : item.options;
		if (Array.isArray(bundleOptions) && bundleOptions.length) bundleOptions.forEach((opt) => {
			str += `  ${opt === null || opt === void 0 ? void 0 : opt.name}  ${formatAmount(opt === null || opt === void 0 ? void 0 : opt.price, 2, currency_symbol)} x ${opt === null || opt === void 0 ? void 0 : opt.num}\n`;
		});
	});
	return str;
};
//#endregion
export { formatProductMessage as default };
