//#region src/plus/pisellReservation/components/bookingDetailModal/bookingProductToProductCard.ts
function str(v) {
	if (v == null) return "";
	return String(v).trim();
}
/** 与宿主多语言对象一致：优先 auto / zh-CN / en 等 */
function pickLocalizedText(obj) {
	if (obj == null) return "";
	if (typeof obj === "string") return obj.trim();
	if (typeof obj !== "object") return "";
	const o = obj;
	for (const k of [
		"auto",
		"zh-CN",
		"zh-HK",
		"en",
		"ja",
		"pt",
		"original",
		"kitchen"
	]) {
		const v = o[k];
		if (v != null && str(v)) return str(v);
	}
	for (const v of Object.values(o)) {
		if (v != null && typeof v === "string" && str(v)) return str(v);
		if (v != null && typeof v === "object" && v !== null && !Array.isArray(v)) {
			const inner = pickLocalizedText(v);
			if (inner) return inner;
		}
	}
	return "";
}
function parseMoneyNumber(v) {
	if (v == null || v === "") return 0;
	if (typeof v === "number" && Number.isFinite(v)) return v;
	const n = Number(String(v).replace(/,/g, "").trim());
	return Number.isFinite(n) ? n : 0;
}
function stableLineId(line, index) {
	const od = line.order_detail_id;
	if (od != null && String(od) !== "") return od;
	const bu = line.booking_uid;
	if (bu != null && String(bu) !== "") return String(bu);
	const pid = line.product_id;
	if (pid != null && String(pid) !== "") return `${String(pid)}-${index}`;
	return `line-${index}`;
}
/** product_sku.option[] → ProductCard 单规格 optionType */
function mapSkuOptionRowToOptionType(opt) {
	var _opt$option_group_ite;
	const idRaw = (_opt$option_group_ite = opt.option_group_item_id) !== null && _opt$option_group_ite !== void 0 ? _opt$option_group_ite : opt.id;
	const idNum = typeof idRaw === "number" && Number.isFinite(idRaw) ? idRaw : Number(String(idRaw !== null && idRaw !== void 0 ? idRaw : 0).replace(/\D/g, "")) || 0;
	const namePart = pickLocalizedText(opt.name);
	const groupPart = pickLocalizedText(opt.group_name);
	const label = groupPart && namePart ? `${groupPart}：${namePart}` : namePart || groupPart || "—";
	const num = Math.max(1, Math.round(parseMoneyNumber(opt.num) || 1));
	const price = parseMoneyNumber(opt.add_price);
	return {
		id: idNum,
		name: label,
		num,
		price,
		total: price * num
	};
}
/** product_sku.variant[] → 一行展示文案 */
function variantRowsToSubtitle(variant) {
	if (!Array.isArray(variant)) return "";
	const parts = [];
	for (const row of variant) {
		if (!row || typeof row !== "object") continue;
		const r = row;
		const g = pickLocalizedText(r.group);
		const it = pickLocalizedText(r.item);
		if (g && it) parts.push(`${g}：${it}`);
		else if (it) parts.push(it);
		else if (g) parts.push(g);
	}
	return parts.join(" · ");
}
/**
* 副标题：subtitle → variant 拼接 → option 名称摘要（无 add 价时）
*/
function buildProductOptionString(sku) {
	if (!sku) return void 0;
	const sub = pickLocalizedText(sku.subtitle);
	if (sub) return sub;
	const varStr = variantRowsToSubtitle(sku.variant);
	if (varStr) return varStr;
	const opts = sku.option;
	if (!Array.isArray(opts) || opts.length === 0) return void 0;
	const names = opts.filter((x) => x && typeof x === "object").map((x) => {
		const o = x;
		const gn = pickLocalizedText(o.group_name);
		const nn = pickLocalizedText(o.name);
		if (gn && nn) return `${gn}：${nn}`;
		return nn || gn;
	}).filter(Boolean);
	return names.length ? names.join(" · ") : void 0;
}
/** product_sku.option[] → 主商品 Header 下 Options 列表 */
function mapProductSkuOptions(sku) {
	if (!sku) return void 0;
	const opts = sku.option;
	if (!Array.isArray(opts) || opts.length === 0) return void 0;
	const out = opts.filter((x) => x && typeof x === "object").map((x) => mapSkuOptionRowToOptionType(x));
	return out.length ? out : void 0;
}
/** product_bundle[] 子项 → bundleType（套餐折叠行） */
function mapBundleRowToBundleType(b, bi) {
	var _b$custom_price;
	const bundleId = b.bundle_id;
	const idNum = typeof bundleId === "number" && Number.isFinite(bundleId) ? bundleId : Number(String(bundleId !== null && bundleId !== void 0 ? bundleId : bi).replace(/\D/g, "")) || bi;
	const num = Math.max(1, Math.round(parseMoneyNumber(b.num) || 1));
	const unit = parseMoneyNumber((_b$custom_price = b.custom_price) !== null && _b$custom_price !== void 0 ? _b$custom_price : b.price);
	const payLine = parseMoneyNumber(b.bundle_payment_price);
	const total = payLine > 0 ? payLine : unit * num;
	const optRaw = b.option;
	const nestedOpts = Array.isArray(optRaw) ? optRaw.filter((x) => x && typeof x === "object").map((x) => mapSkuOptionRowToOptionType(x)) : void 0;
	return {
		id: idNum,
		name: pickLocalizedText(b.bundle_title) || "—",
		num,
		price: num > 0 ? total / num : unit,
		total,
		origin_total: void 0,
		price_type: str(b.price_type) || void 0,
		options: nestedOpts && nestedOpts.length ? nestedOpts : void 0
	};
}
function mapProductBundles(line) {
	const raw = line.product_bundle;
	if (!Array.isArray(raw) || raw.length === 0) return void 0;
	const out = raw.filter((x) => x && typeof x === "object").map((x, i) => mapBundleRowToBundleType(x, i));
	return out.length ? out : void 0;
}
/** 主行金额：直接透传接口字段，不做乘除换算 */
function resolveMainLineAmounts(line) {
	const price = parseMoneyNumber(line.payment_price);
	const total = parseMoneyNumber(line.selling_price);
	const origin = parseMoneyNumber(line.original_price);
	return {
		price,
		total,
		origin_total: origin > 0 ? origin : void 0
	};
}
function resolveProductNumericId(line, index) {
	const pid = line.product_id;
	if (typeof pid === "number" && Number.isFinite(pid)) return pid;
	const n = Number(String(pid !== null && pid !== void 0 ? pid : "").replace(/\D/g, ""));
	return Number.isFinite(n) && n > 0 ? n : index;
}
/**
* @param line booking.products 或 order.products 同源结构（与 api-bookings-demo 一致）
*/
function bookingProductLineToProductCardData(line, index, setting) {
	var _setting$app, _setting$app$translat, _line$resource_id, _ref, _line$image;
	const titleObj = line.product_title;
	const name = (setting === null || setting === void 0 || (_setting$app = setting.app) === null || _setting$app === void 0 || (_setting$app = _setting$app.locales) === null || _setting$app === void 0 || (_setting$app$translat = _setting$app.translation) === null || _setting$app$translat === void 0 ? void 0 : _setting$app$translat.call(_setting$app, titleObj)) || pickLocalizedText(titleObj) || str(line.name) || str(line.title) || "—";
	const sku = line.product_sku;
	const qty = Math.max(1, Math.round(parseMoneyNumber(line.product_quantity) || 1));
	const { price, total, origin_total } = resolveMainLineAmounts(line);
	const product_option_string = buildProductOptionString(sku);
	const options = mapProductSkuOptions(sku);
	const bundle = mapProductBundles(line);
	const noteRaw = line.note;
	const note = str(noteRaw) || void 0;
	const likeRaw = line.like_status;
	const like_status = likeRaw != null && str(likeRaw) ? str(likeRaw) : void 0;
	const resId = (_line$resource_id = line.resource_id) !== null && _line$resource_id !== void 0 ? _line$resource_id : line.relation_id;
	const resource_id = resId != null && String(resId) !== "" ? resId : void 0;
	const rfn = line.relation_form_name;
	const relation_form_name = rfn != null && str(rfn) ? str(rfn) : void 0;
	const imageRaw = (_ref = (_line$image = line.image) !== null && _line$image !== void 0 ? _line$image : line.product_image) !== null && _ref !== void 0 ? _ref : sku === null || sku === void 0 ? void 0 : sku.image;
	const image = imageRaw != null && str(imageRaw) ? str(imageRaw) : void 0;
	return {
		_id: stableLineId(line, index),
		id: resolveProductNumericId(line, index),
		name,
		price,
		total,
		num: qty,
		origin_total,
		image,
		isShowNote: Boolean(note),
		note,
		like_status,
		bundle,
		options,
		isShowAction: false,
		product_option_string,
		discount_reason: void 0,
		relation_products: void 0,
		resource_id,
		relation_form_name,
		holder_title: void 0,
		start_date: str(line.start_date) || void 0,
		end_date: str(line.end_date) || void 0,
		is_show_duration: void 0,
		is_show_week: void 0
	};
}
//#endregion
exports.bookingProductLineToProductCardData = bookingProductLineToProductCardData;
