import { _objectSpread2 } from "../../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js";
import { locales } from "@pisell/utils";
//#region src/plus/pisellReservation/components/bookingDetailModal/bookingDetailHostActions.ts
/**
* 宿主 action / NativePage 用的订单主键：优先 order.id，否则顶层 order_id。
*/
function resolveOrderIdForHostActions(bookingRaw) {
	var _ref, _order$id;
	const order = bookingRaw.order;
	const id = (_ref = (_order$id = order === null || order === void 0 ? void 0 : order.id) !== null && _order$id !== void 0 ? _order$id : order === null || order === void 0 ? void 0 : order.order_id) !== null && _ref !== void 0 ? _ref : bookingRaw.order_id;
	if (id == null || id === "") return void 0;
	if (typeof id === "number" && Number.isFinite(id)) return id;
	const s = String(id).trim();
	if (!s) return void 0;
	const n = Number(s);
	return Number.isFinite(n) && String(n) === s ? n : s;
}
function resolveBookingIdForHostActions(bookingRaw) {
	const id = bookingRaw.id;
	if (id == null || id === "") return void 0;
	if (typeof id === "number" && Number.isFinite(id)) return id;
	const s = String(id).trim();
	if (!s) return void 0;
	const n = Number(s);
	return Number.isFinite(n) && String(n) === s ? n : s;
}
function resolveBookingDateTime(bookingRaw, atKey, dateKey, timeKey) {
	const at = bookingRaw[atKey];
	if (at != null && String(at).trim() !== "") return String(at).trim();
	const date = bookingRaw[dateKey];
	const time = bookingRaw[timeKey];
	if (date == null || time == null) return void 0;
	const dateText = String(date).trim();
	const timeText = String(time).trim();
	if (!dateText || !timeText) return void 0;
	return `${dateText} ${timeText}`;
}
function resolveResourceIdFromRow(row) {
	if (!(row === null || row === void 0 ? void 0 : row.id)) return void 0;
	return String(row.id).trim() || void 0;
}
function buildEditBookingResourceSnapshot(selected) {
	var _selected$resource, _selected$resource$id, _selected$resource2, _selected$resource$fo, _selected$resource3, _selected$resource$co, _selected$resource4, _selected$resource$ma, _selected$resource5;
	const raw = ((_selected$resource = selected.resource) === null || _selected$resource === void 0 ? void 0 : _selected$resource.raw) && typeof selected.resource.raw === "object" ? selected.resource.raw : selected.row;
	const id = (_selected$resource$id = (_selected$resource2 = selected.resource) === null || _selected$resource2 === void 0 ? void 0 : _selected$resource2.id) !== null && _selected$resource$id !== void 0 ? _selected$resource$id : raw === null || raw === void 0 ? void 0 : raw.id;
	if (id == null || String(id).trim() === "") return void 0;
	return _objectSpread2(_objectSpread2({}, raw), {}, {
		id,
		form_id: (_selected$resource$fo = (_selected$resource3 = selected.resource) === null || _selected$resource3 === void 0 ? void 0 : _selected$resource3.form_id) !== null && _selected$resource$fo !== void 0 ? _selected$resource$fo : raw === null || raw === void 0 ? void 0 : raw.form_id,
		code: (_selected$resource$co = (_selected$resource4 = selected.resource) === null || _selected$resource4 === void 0 ? void 0 : _selected$resource4.code) !== null && _selected$resource$co !== void 0 ? _selected$resource$co : raw === null || raw === void 0 ? void 0 : raw.code,
		main_field: (_selected$resource$ma = (_selected$resource5 = selected.resource) === null || _selected$resource5 === void 0 ? void 0 : _selected$resource5.main_field) !== null && _selected$resource$ma !== void 0 ? _selected$resource$ma : raw === null || raw === void 0 ? void 0 : raw.main_field
	});
}
function getResourceFormId(resource) {
	if ((resource === null || resource === void 0 ? void 0 : resource.form_id) == null) return void 0;
	return String(resource.form_id).trim() || void 0;
}
function isSameResourceType(source, target) {
	const sourceFormId = getResourceFormId(source);
	const targetFormId = getResourceFormId(target);
	if (!sourceFormId || !targetFormId) return true;
	return sourceFormId === targetFormId;
}
function getSalesHostModule(pisellos) {
	var _getModule;
	return pisellos === null || pisellos === void 0 || (_getModule = pisellos.getModule) === null || _getModule === void 0 ? void 0 : _getModule.call(pisellos, "sales");
}
function updateLocalOrder(action, orderId) {
	if (orderId == null || typeof action !== "function") return;
	action({
		type: "webpos.updateLocalOrderApi",
		data: { order_id: orderId }
	});
}
/** 预约是否已结束（`status` / `appointment_status` 为 completed）。 */
function isCompletedBookingRaw(bookingRaw) {
	var _bookingRaw$status, _bookingRaw$appointme;
	const s = String((_bookingRaw$status = bookingRaw.status) !== null && _bookingRaw$status !== void 0 ? _bookingRaw$status : "").trim().toLowerCase();
	const a = String((_bookingRaw$appointme = bookingRaw.appointment_status) !== null && _bookingRaw$appointme !== void 0 ? _bookingRaw$appointme : "").trim().toLowerCase();
	return s === "completed" || a === "completed";
}
function parseBookingProducts(bookingRaw) {
	const p = bookingRaw.products;
	if (!Array.isArray(p)) return [];
	return p.filter((x) => x && typeof x === "object");
}
function orderCurrencySymbol(order) {
	if (!order) return "$";
	const s = order.currency_symbol;
	if (s != null && String(s).trim() !== "") return String(s).trim();
	return "$";
}
function resolveAppointmentStatusTag(bookingRaw) {
	var _bookingRaw$appointme2, _statusMap$rawStatus;
	const rawStatus = String((_bookingRaw$appointme2 = bookingRaw === null || bookingRaw === void 0 ? void 0 : bookingRaw.appointment_status) !== null && _bookingRaw$appointme2 !== void 0 ? _bookingRaw$appointme2 : "").trim();
	const statusMap = {
		new: {
			label: locales.getText("pisell2.text.status.new"),
			tone: "new"
		},
		confirmed: {
			label: locales.getText("pisell2.text.status.confirmed"),
			tone: "confirmed"
		},
		arrived: {
			label: locales.getText("pisell2.text.status.arrived"),
			tone: "arrived"
		},
		started: {
			label: locales.getText("pisell2.text.status.started"),
			tone: "started"
		},
		cancelled: {
			label: locales.getText("pisell2.text.status.cancelled"),
			tone: "cancelled"
		},
		no_show: {
			label: locales.getText("pisell2.text.status.no_show"),
			tone: "no-show"
		},
		completed: {
			label: locales.getText("pisell2.text.status.completed"),
			tone: "completed"
		}
	};
	return (_statusMap$rawStatus = statusMap[rawStatus]) !== null && _statusMap$rawStatus !== void 0 ? _statusMap$rawStatus : statusMap.new;
}
//#endregion
export { buildEditBookingResourceSnapshot, getSalesHostModule, isCompletedBookingRaw, isSameResourceType, orderCurrencySymbol, parseBookingProducts, resolveAppointmentStatusTag, resolveBookingDateTime, resolveBookingIdForHostActions, resolveOrderIdForHostActions, resolveResourceIdFromRow, updateLocalOrder };
