import { _objectSpread2 } from "../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js";
import { RESERVATION_TIMELINE_BUSINESS_RANGE } from "../reservationConstants.js";
import { bookingTimeWindowForCalendarRange, bookingTimeWindowForReservationView } from "../hooks/usePisellReservationBookingData.js";
import dayjs from "dayjs";
//#region src/plus/pisellReservation/data/reservationDataUtils.ts
/**
* 预约页与 RecordBoard 共用的纯函数：内置列表判定、筛参合并、按营业日改 booking 窗口、持久化 bodyView 读取。
*/
/**
* 比较两个 `booking_time_start_between` 窗口的**值**是否一致；
* 支持 Dayjs / Date / string / number 混合（统一经 `dayjs()` 归一再取 `valueOf()`）。
* 任一端不是长度为 2 的数组或某项不可解析时，保守返回 false（视为"有变化"，对应原行为）。
*/
function isSameBookingTimeWindow(a, b) {
	if (!Array.isArray(a) || !Array.isArray(b)) return false;
	if (a.length !== 2 || b.length !== 2) return false;
	try {
		for (let i = 0; i < 2; i++) {
			const av = dayjs(a[i]).valueOf();
			const bv = dayjs(b[i]).valueOf();
			if (!Number.isFinite(av) || !Number.isFinite(bv)) return false;
			if (av !== bv) return false;
		}
		return true;
	} catch (_unused) {
		return false;
	}
}
/**
* 是否使用内置预约/资源请求：
* - 未传 `data` / `null`；
* - 或低代码只绑了空数组作占位，且未同时受控 total / onPageChange。
*   （仅有 pagination 占位而无 onPageChange 时仍走内置，否则会永不请求列表。）
*/
function shouldUseBuiltinReservationList(dataProp, totalProp, paginationProp, onPageChangeProp) {
	if (dataProp == null) return true;
	return Array.isArray(dataProp) && dataProp.length === 0 && totalProp === void 0 && onPageChangeProp === void 0;
}
/**
* 表格筛选若未带支付态默认值，接口可能返回空；与 QuickFilter/高级筛选项合并时保留 monitor 默认。
*/
function mergeGridBookingSearchParams(params) {
	const p = params && typeof params === "object" ? _objectSpread2({}, params) : {};
	const filterRaw = p.filter;
	const filter = typeof filterRaw === "object" && filterRaw !== null && !Array.isArray(filterRaw) ? _objectSpread2({}, filterRaw) : {};
	const valuesRaw = filter.values;
	const values = typeof valuesRaw === "object" && valuesRaw !== null && !Array.isArray(valuesRaw) ? _objectSpread2({}, valuesRaw) : {};
	if (!Array.isArray(values.order_payment_status) || values.order_payment_status.length === 0) values.order_payment_status = void 0;
	return _objectSpread2(_objectSpread2({}, p), {}, { filter: _objectSpread2(_objectSpread2({}, filter), {}, { values }) });
}
/** 列表请求：平面图 / 资源墙 / 非日历视图下，按顶栏日程与时间轴营业窗更新 booking_time_start_between */
function mergeSearchParamsBookingWindow(prev, anchorDay, operatingDayBoundary, timelineRange = RESERVATION_TIMELINE_BUSINESS_RANGE, wallClock = dayjs()) {
	const prevFilter = typeof prev.filter === "object" && prev.filter ? prev.filter : {};
	const prevValues = typeof prevFilter.values === "object" && prevFilter.values ? prevFilter.values : {};
	const nextWindow = bookingTimeWindowForReservationView(anchorDay, operatingDayBoundary, timelineRange, wallClock);
	/**
	* 引用稳定化：若新旧 `booking_time_start_between` 值完全一致，返回原 `prev`，
	* 避免 `patchSearchParams` 误判为"参数变化"后触发不必要的 SSE 重连 / 列表重拉
	* （常见于 calendar day 视图切回 floorMap / resourceWall 时——值未变但每次都产生新对象引用）。
	*/
	if (isSameBookingTimeWindow(prevValues.booking_time_start_between, nextWindow)) return prev;
	return _objectSpread2(_objectSpread2({}, prev), {}, { filter: _objectSpread2(_objectSpread2({}, prevFilter), {}, { values: _objectSpread2(_objectSpread2({}, prevValues), {}, { booking_time_start_between: nextWindow }) }) });
}
/**
* 清理仅在日历周/月视图下设置的请求 flag：
* - `compact_fields`：为 true 时，osServer 远端 SSE 走精简 with，不返回 booking.resources；
* - `enable_remote_memory_cache`：开启后 osServer 会写入 `bookingRemoteCache`，Ably 推送时按 compact with 同步。
*
* 切回平面图 / 资源墙 / 表格时必须先清掉，否则非今日锚定下平面图会拿到无 resources 的 booking，
* 导致按桌合并全部落空、画布空白。
*/
function stripCalendarOnlyFlags(prev) {
	const prevFilter = typeof prev.filter === "object" && prev.filter ? prev.filter : {};
	const prevValuesRaw = typeof prevFilter.values === "object" && prevFilter.values ? prevFilter.values : {};
	if (!("compact_fields" in prevValuesRaw) && !("enable_remote_memory_cache" in prevValuesRaw)) return prev;
	const nextValues = _objectSpread2({}, prevValuesRaw);
	delete nextValues.compact_fields;
	delete nextValues.enable_remote_memory_cache;
	return _objectSpread2(_objectSpread2({}, prev), {}, { filter: _objectSpread2(_objectSpread2({}, prevFilter), {}, { values: nextValues }) });
}
/** 日历导航：按视口自然日范围扩展 `booking_time_start_between` */
function mergeSearchParamsBookingRange(prev, rangeStart, rangeEnd, operatingDayBoundary) {
	const prevFilter = typeof prev.filter === "object" && prev.filter ? prev.filter : {};
	const prevValues = typeof prevFilter.values === "object" && prevFilter.values ? prevFilter.values : {};
	return _objectSpread2(_objectSpread2({}, prev), {}, { filter: _objectSpread2(_objectSpread2({}, prevFilter), {}, { values: _objectSpread2(_objectSpread2({}, prevValues), {}, { booking_time_start_between: bookingTimeWindowForCalendarRange(rangeStart, rangeEnd, operatingDayBoundary) }) }) });
}
/**
* 按任意 key 读取表格/平面图偏好（与 PisellRecordBoard 内 read 规则一致）。
*/
function readBodyViewFromStorageKey(key) {
	if (typeof window === "undefined" || !key) return "floorMap";
	try {
		const raw = localStorage.getItem(key);
		if (raw === "grid" || raw === "floorMap" || raw === "calendar" || raw === "resourceWall") return raw;
	} catch (_unused2) {}
	return "floorMap";
}
//#endregion
export { mergeGridBookingSearchParams, mergeSearchParamsBookingRange, mergeSearchParamsBookingWindow, readBodyViewFromStorageKey, shouldUseBuiltinReservationList, stripCalendarOnlyFlags };
