import { resolveBookingStripIdForUi } from "../../floorRoomCard/mapRowToFloorRoomCardData.js";
import dayjs from "dayjs";
import "@pisell/utils";
//#region src/plus/pisellReservation/components/bookingDetailModal/bookingDetailFromRaw.ts
/**
* 将平面图点击得到的宿主 booking 原始行解析为详情弹窗视图模型（防御性读取，缺字段则省略对应区块）。
* 人数、起止时刻、迟到/超时、倒计时、进度等与 {@link rawToBooking} / RoomCard 一致。
*/
function str(v) {
	if (v == null) return "";
	return String(v).trim();
}
/** 预约 raw 是否已为 fully paid（优先 `order.payment_status`，否则顶层 `payment_status`） */
function isBookingRawOrderPaid(raw) {
	const order = raw.order;
	const fromOrder = order === null || order === void 0 ? void 0 : order.payment_status;
	const fromTop = raw.payment_status;
	const v = fromOrder != null && String(fromOrder).trim() !== "" ? fromOrder : fromTop;
	return String(v !== null && v !== void 0 ? v : "").trim().toLowerCase() === "paid";
}
function pickTitle(raw, order) {
	var _ref, _order$name;
	const rf = str(raw.relation_form_name);
	if (rf) return rf;
	const on = str((_ref = (_order$name = order.name) !== null && _order$name !== void 0 ? _order$name : order.title) !== null && _ref !== void 0 ? _ref : order.order_name);
	if (on) return on;
	const meta = raw.metadata;
	if (meta && typeof meta === "object") {
		var _meta$title;
		const t = str((_meta$title = meta.title) !== null && _meta$title !== void 0 ? _meta$title : meta.product_name);
		if (t) return t;
	}
	return "";
}
function formatMoney(v) {
	if (v == null || v === "") return "";
	if (typeof v === "number" && Number.isFinite(v)) return String(v);
	return str(v);
}
function buildTimeRange(raw, order) {
	var _raw$start_at, _raw$end_at;
	const startAt = (_raw$start_at = raw.start_at) !== null && _raw$start_at !== void 0 ? _raw$start_at : order.start_at;
	const endAt = (_raw$end_at = raw.end_at) !== null && _raw$end_at !== void 0 ? _raw$end_at : order.end_at;
	if (startAt || endAt) {
		const a = startAt ? dayjs(String(startAt)) : null;
		const b = endAt ? dayjs(String(endAt)) : null;
		if ((a === null || a === void 0 ? void 0 : a.isValid()) && (b === null || b === void 0 ? void 0 : b.isValid())) {
			if (a.isSame(b, "day")) return `${a.format("HH:mm")} – ${b.format("HH:mm")} · ${a.format("D MMM")}`;
			return `${a.format("HH:mm")} – ${b.format("HH:mm")} · ${a.format("D MMM YYYY")}`;
		}
		if (a === null || a === void 0 ? void 0 : a.isValid()) return a.format("YYYY-MM-DD HH:mm");
	}
	const sd = str(raw.start_date);
	const st = str(raw.start_time);
	const ed = str(raw.end_date);
	const et = str(raw.end_time);
	if (sd && st && ed && et) return `${st} – ${et} ${sd}`;
	if (sd && st) return `${sd} ${st}`;
	const sched = str(order.schedule_date);
	if (sched) return sched;
	return "—";
}
function parseQuantity(v) {
	if (v == null || v === "") return null;
	if (typeof v === "number" && Number.isFinite(v)) return Math.max(0, Math.round(v));
	const n = Number(String(v).replace(/,/g, "").trim());
	return Number.isFinite(n) ? Math.max(0, Math.round(n)) : null;
}
function orderCurrencySymbol(order) {
	const s = str(order.currency_symbol);
	if (s) return s;
	return "$";
}
function moneyWithSymbol(symbol, amount) {
	const f = formatMoney(amount);
	if (!f) return `${symbol}—`;
	if (f.startsWith(symbol) || f.startsWith("$") || f.startsWith("¥") || f.startsWith("€")) return f;
	return `${symbol}${f}`;
}
/**
* 与截图一致：`15:10 31 Mar ~ 15:40 31 Mar`；无完整字段时回退 {@link buildTimeRange}。
*/
function buildStripScheduleLine(raw, order) {
	var _raw$start_at2, _raw$end_at2;
	const startAt = (_raw$start_at2 = raw.start_at) !== null && _raw$start_at2 !== void 0 ? _raw$start_at2 : order.start_at;
	const endAt = (_raw$end_at2 = raw.end_at) !== null && _raw$end_at2 !== void 0 ? _raw$end_at2 : order.end_at;
	if (startAt || endAt) {
		const a = startAt ? dayjs(String(startAt)) : null;
		const b = endAt ? dayjs(String(endAt)) : null;
		if ((a === null || a === void 0 ? void 0 : a.isValid()) && (b === null || b === void 0 ? void 0 : b.isValid())) return `${a.format("HH:mm")} ${a.format("D MMM")} ~ ${b.format("HH:mm")} ${b.format("D MMM")}`;
		if (a === null || a === void 0 ? void 0 : a.isValid()) return `${a.format("HH:mm")} ${a.format("D MMM")}`;
	}
	const sd = str(raw.start_date);
	const st = str(raw.start_time);
	const ed = str(raw.end_date);
	const et = str(raw.end_time);
	if (sd && st && ed && et) {
		const padTime = (t) => t.length === 5 ? `${t}:00` : t;
		const a = dayjs(`${sd}T${padTime(st)}`);
		const b = dayjs(`${ed}T${padTime(et)}`);
		if (a.isValid() && b.isValid()) return `${a.format("HH:mm")} ${a.format("D MMM")} ~ ${b.format("HH:mm")} ${b.format("D MMM")}`;
	}
	return buildTimeRange(raw, order);
}
/** 与弹窗商品列表同源：顶层 `products[]` 有效元素个数 */
function countTopLevelProductLines(raw) {
	const p = raw.products;
	if (!Array.isArray(p)) return null;
	return p.filter((x) => x && typeof x === "object").length;
}
/**
* 预约详情正文顶部信息条：字段来自 order.summary / order 与 raw 起止时间。
*/
function bookingDetailOrderStripFromRaw(raw) {
	var _ref2, _countTopLevelProduct, _order$payment_status, _sum$expect_amount;
	const order = (_ref2 = raw.order) !== null && _ref2 !== void 0 ? _ref2 : {};
	const sum = order.summary;
	const sym = orderCurrencySymbol(order);
	const eyebrow = str(raw.relation_form_name) || pickTitle(raw, order) || str(raw.booking_id) || "—";
	const stripBookingId = resolveBookingStripIdForUi(raw);
	const itemsCount = (_countTopLevelProduct = countTopLevelProductLines(raw)) !== null && _countTopLevelProduct !== void 0 ? _countTopLevelProduct : parseQuantity(sum === null || sum === void 0 ? void 0 : sum.product_quantity);
	const orderTotalWithSymbol = moneyWithSymbol(sym, sum === null || sum === void 0 ? void 0 : sum.total_amount);
	const payRaw = (_order$payment_status = order.payment_status) !== null && _order$payment_status !== void 0 ? _order$payment_status : raw.payment_status;
	const paymentStatusRaw = payRaw != null && String(payRaw).trim() !== "" ? String(payRaw).trim() : void 0;
	const dueWithSymbol = moneyWithSymbol(sym, (_sum$expect_amount = sum === null || sum === void 0 ? void 0 : sum.expect_amount) !== null && _sum$expect_amount !== void 0 ? _sum$expect_amount : sum === null || sum === void 0 ? void 0 : sum.total_amount);
	return {
		eyebrow,
		stripBookingId,
		scheduleLine: buildStripScheduleLine(raw, order),
		itemsCount,
		orderTotalWithSymbol,
		paymentStatusRaw,
		dueWithSymbol
	};
}
//#endregion
export { bookingDetailOrderStripFromRaw, isBookingRawOrderPaid };
