require("../../../_virtual/_rolldown/runtime.js");
const require_asyncToGenerator = require("../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/asyncToGenerator.js");
const require_index = require("../../../utils/index.js");
const require_index$1 = require("../components/bookingChangeModal/index.js");
let antd = require("antd");
let _pisell_materials = require("@pisell/materials");
//#region src/plus/pisellReservation/data/bookingCalendarMoveIntegration.ts
/**
* 日历拖拽改期 → {@link bookingChangeModal} + child-booking 接口。
*/
function isHourInTimelineSlots(hour, hourSlots) {
	if (!hourSlots.length) return false;
	return hourSlots.some((x) => Math.abs(x - hour) < 1e-6);
}
function calendarBookingLikeToModalItem(like) {
	return {
		id: like.id,
		courtId: like.courtId,
		date: like.date,
		startHour: like.startHour,
		endHour: like.endHour,
		name: like.name,
		kind: like.kind,
		phone: like.phone,
		raw: like.raw
	};
}
function calendarBookingsToModalItems(list) {
	return list.map(calendarBookingLikeToModalItem);
}
function resourcesToCourts(resources) {
	return resources.map((r) => {
		var _resource$form_id;
		const resource = r;
		return {
			id: String(resource.id),
			name: resource.name,
			form_id: String((_resource$form_id = resource.form_id) !== null && _resource$form_id !== void 0 ? _resource$form_id : resource.id),
			relation_id: String(resource.id),
			resource_id: String(resource.id),
			relation_type: "form"
		};
	});
}
function resolveChildBookingOrderPathId(item) {
	const raw = item.raw;
	if (!raw) return null;
	const candidates = [
		raw.order_id,
		raw.booking_uid,
		raw.booking_id,
		raw.id
	];
	for (const c of candidates) if (c !== void 0 && c !== null && String(c).trim() !== "") return typeof c === "number" ? c : String(c);
	return null;
}
function openBookingChangeModalAfterCalendarDrag(_x2) {
	return _openBookingChangeModalAfterCalendarDrag.apply(this, arguments);
}
function _openBookingChangeModalAfterCalendarDrag() {
	_openBookingChangeModalAfterCalendarDrag = require_asyncToGenerator._asyncToGenerator(function* (options) {
		var _hourSlots$;
		const { calendarItem, draft, calendarResources, calendarBookings, effectiveTimelineRange, anchorDate, slotStepMinutes, onSuccessRefresh } = options;
		if (calendarItem.kind !== "booking") return false;
		const item = calendarBookingLikeToModalItem(calendarItem);
		const courts = resourcesToCourts(calendarResources);
		const modalBookings = calendarBookingsToModalItems(calendarBookings);
		const hourSlots = (0, _pisell_materials.buildCalendarTimelineSlotStartsFromRange)(effectiveTimelineRange, anchorDate, slotStepMinutes);
		const slotStepHours = slotStepMinutes / 60;
		const firstTimelineHour = (_hourSlots$ = hourSlots[0]) !== null && _hourSlots$ !== void 0 ? _hourSlots$ : 0;
		const endTimelineExclusive = hourSlots.length ? hourSlots[hourSlots.length - 1] + slotStepHours : 24;
		try {
			yield require_index$1.default.open({
				item,
				targetCourtId: draft.targetCourtId,
				targetDate: draft.targetDate,
				targetHour: draft.targetHour,
				courts,
				visibleCourts: courts,
				bookings: modalBookings,
				hourSlots,
				firstTimelineHour,
				endTimelineExclusive,
				theme: "dark",
				title: "确认调整预约",
				description: "拖拽后请确认场地、日期与时段，提交后生效。",
				confirmText: "确认改约",
				cancelText: "取消",
				getBookingDetailData: (b, courtList) => {
					var _courtList$find;
					return {
						customer: {
							name: b.name || "—",
							phone: typeof b.phone === "string" ? b.phone : "",
							email: ""
						},
						headerSummary: {
							customerName: b.name || "",
							resourceLabel: ((_courtList$find = courtList.find((c) => c.id === b.courtId)) === null || _courtList$find === void 0 ? void 0 : _courtList$find.name) || b.courtId
						}
					};
				},
				isBusinessHour: (h) => isHourInTimelineSlots(h, hourSlots),
				hasConflict: (allBookings, targetCourtId, targetDate, startHour, endHour, excludeId) => allBookings.some((booking) => {
					if (booking.id === excludeId) return false;
					if (booking.courtId !== targetCourtId || booking.date !== targetDate) return false;
					return !(endHour <= booking.startHour || startHour >= booking.endHour);
				}),
				onSubmit: function() {
					var _ref = require_asyncToGenerator._asyncToGenerator(function* (ctx) {
						const pathId = resolveChildBookingOrderPathId(ctx.item);
						if (pathId == null) throw new Error("无法解析订单编号，请从预约详情改约");
						yield require_index.request.getRequest().put(`/shop/v1.1/order/appointment/child-booking/${pathId}`, ctx.submitPayload);
						yield onSuccessRefresh();
						antd.message.success("改约已保存");
					});
					return function onSubmit(_x) {
						return _ref.apply(this, arguments);
					};
				}()
			});
			return true;
		} catch (_unused) {
			return false;
		}
	});
	return _openBookingChangeModalAfterCalendarDrag.apply(this, arguments);
}
//#endregion
exports.openBookingChangeModalAfterCalendarDrag = openBookingChangeModalAfterCalendarDrag;
