const require_runtime = require("../../../_virtual/_rolldown/runtime.js");
const require_objectSpread2 = require("../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js");
const require_asyncToGenerator = require("../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/asyncToGenerator.js");
let dayjs = require("dayjs");
dayjs = require_runtime.__toESM(dayjs);
//#region src/plus/salesSdk/utils/buildDefaultLoadProductsParams.ts
/** 与 SalesSdkProvider bootstrap 对齐的 board config 默认查询参数。 */
const DEFAULT_BOOKING_CONFIG_PARAMS = { item_type: "appointment_booking" };
/** 从 tempOrder.bookings 提取编辑态容量排除用的 booking id。 */
function collectBookingIdsFromTempOrder(tempOrder) {
	const bookings = tempOrder === null || tempOrder === void 0 ? void 0 : tempOrder.bookings;
	if (!Array.isArray(bookings)) return [];
	return bookings.map((b) => b === null || b === void 0 ? void 0 : b.id).filter((id) => typeof id === "number" && Number.isFinite(id));
}
/** 构造与 Provider bootstrap 一致的 initBookingContext 入参。 */
function buildInitBookingContextParams(bookingConfigParams, options = {}) {
	var _options$date, _options$bookingIds;
	return require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({
		bookingConfigParams: require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, DEFAULT_BOOKING_CONFIG_PARAMS), bookingConfigParams || {}),
		date: (_options$date = options.date) !== null && _options$date !== void 0 ? _options$date : (0, dayjs.default)()
	}, ((_options$bookingIds = options.bookingIds) === null || _options$bookingIds === void 0 ? void 0 : _options$bookingIds.length) ? { bookingIds: options.bookingIds } : {}), options.loadResources !== void 0 ? { loadResources: options.loadResources } : {});
}
/** 将 board `menu_list_tab.menu_list` 规范为 number[]。 */
function normalizeMenuListIds(raw) {
	if (!Array.isArray(raw)) return [];
	const out = [];
	for (const item of raw) {
		const n = Number(item);
		if (Number.isFinite(n) && n > 0) out.push(Math.floor(n));
	}
	return [...new Set(out)];
}
function resolveScheduleFromDate(raw) {
	const date = raw && (0, dayjs.default)(raw).isValid() ? (0, dayjs.default)(raw) : (0, dayjs.default)();
	const schedule_date = date.format("YYYY-MM-DD");
	return {
		schedule_date,
		schedule_datetime: date.format(`${schedule_date} HH:mm:ss`)
	};
}
/**
* 从 BookingContext / boardConfig 推导 loadProducts 默认参数。
* 与 ticketBooking `_getProducts` 对齐：日期取自 BookingContext，餐牌取自 board config。
*/
function buildDefaultLoadProductsParams(bookingTicket) {
	var _bookingTicket$getBoo, _bookingTicket$getBoo2, _bookingTicket$getBoo3, _bookingTicket$getBoo4, _bookingConfig$config;
	const schedule = resolveScheduleFromDate((_bookingTicket$getBoo = bookingTicket === null || bookingTicket === void 0 || (_bookingTicket$getBoo2 = bookingTicket.getBookingDate) === null || _bookingTicket$getBoo2 === void 0 ? void 0 : _bookingTicket$getBoo2.call(bookingTicket)) !== null && _bookingTicket$getBoo !== void 0 ? _bookingTicket$getBoo : null);
	const bookingConfig = (_bookingTicket$getBoo3 = bookingTicket === null || bookingTicket === void 0 || (_bookingTicket$getBoo4 = bookingTicket.getBookingConfig) === null || _bookingTicket$getBoo4 === void 0 ? void 0 : _bookingTicket$getBoo4.call(bookingTicket)) !== null && _bookingTicket$getBoo3 !== void 0 ? _bookingTicket$getBoo3 : null;
	const menu_list_ids = normalizeMenuListIds(bookingConfig === null || bookingConfig === void 0 || (_bookingConfig$config = bookingConfig.config) === null || _bookingConfig$config === void 0 || (_bookingConfig$config = _bookingConfig$config.menu_list_tab) === null || _bookingConfig$config === void 0 ? void 0 : _bookingConfig$config.menu_list);
	return require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, schedule), menu_list_ids.length > 0 ? { menu_list_ids } : {});
}
/** 外部 override 覆盖默认值；undefined 字段不冲掉 default。 */
function mergeLoadProductsParams(defaults, override = {}) {
	const cleaned = Object.fromEntries(Object.entries(override).filter(([, value]) => value !== void 0));
	return require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, defaults), cleaned);
}
/**
* loadProducts 前确保 boardConfig 已就绪；缺省时补拉 config（不强制拉 resources）。
* 须传入与 Provider bootstrap 等价的 init 入参，避免裸调 OS 时使用错误 bookingConfigParams。
*/
function ensureBookingConfigForLoad(_x, _x2) {
	return _ensureBookingConfigForLoad.apply(this, arguments);
}
function _ensureBookingConfigForLoad() {
	_ensureBookingConfigForLoad = require_asyncToGenerator._asyncToGenerator(function* (bookingTicket, initParams) {
		var _bookingTicket$isBook;
		if ((_bookingTicket$isBook = bookingTicket.isBookingContextReady) === null || _bookingTicket$isBook === void 0 ? void 0 : _bookingTicket$isBook.call(bookingTicket, initParams)) return;
		if (typeof bookingTicket.initBookingContext === "function") {
			yield bookingTicket.initBookingContext(initParams);
			return;
		}
		if (typeof bookingTicket.loadBookingConfig === "function") yield bookingTicket.loadBookingConfig(initParams.bookingConfigParams);
	});
	return _ensureBookingConfigForLoad.apply(this, arguments);
}
//#endregion
exports.buildDefaultLoadProductsParams = buildDefaultLoadProductsParams;
exports.buildInitBookingContextParams = buildInitBookingContextParams;
exports.collectBookingIdsFromTempOrder = collectBookingIdsFromTempOrder;
exports.ensureBookingConfigForLoad = ensureBookingConfigForLoad;
exports.mergeLoadProductsParams = mergeLoadProductsParams;
