const require_runtime = require("../../../_virtual/_rolldown/runtime.js");
const require_objectSpread2 = require("../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js");
const require_objectWithoutProperties = require("../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectWithoutProperties.js");
let dayjs = require("dayjs");
dayjs = require_runtime.__toESM(dayjs);
//#region src/components/ticketBooking/utils/edit.ts
const _excluded = ["detail"], _excluded2 = ["detail", "product"];
const _isMultiDayProduct = (item) => {
	var _item$metadata;
	return item === null || item === void 0 || (_item$metadata = item.metadata) === null || _item$metadata === void 0 ? void 0 : _item$metadata.groupId;
};
const _formatHolder = (detail) => {
	var _detail$holder;
	return (detail === null || detail === void 0 || (_detail$holder = detail.holder) === null || _detail$holder === void 0 || (_detail$holder = _detail$holder.form_record) === null || _detail$holder === void 0 ? void 0 : _detail$holder[0]) || [];
};
const _formatBookingMetadata = (item) => {
	var _item$holder;
	let holder_id = 0;
	if ((_item$holder = item.holder) === null || _item$holder === void 0 || (_item$holder = _item$holder.form_record) === null || _item$holder === void 0 ? void 0 : _item$holder.length) holder_id = item.holder.form_record[0];
	if (!holder_id) {
		var _item$metadata2;
		holder_id = (_item$metadata2 = item.metadata) === null || _item$metadata2 === void 0 ? void 0 : _item$metadata2.holder_id;
	}
	return require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, item.metadata || {}), {}, { holder_id });
};
const _getDisabledEdit = ({ shop_discount, bookings, payment_status }) => {
	const bookingList = Array.isArray(bookings === null || bookings === void 0 ? void 0 : bookings.list) ? bookings.list : [];
	const appointmentStatus = bookings === null || bookings === void 0 ? void 0 : bookings.appointment_status;
	if (bookingList.some((d) => !d.detail)) return true;
	if (shop_discount && Number(shop_discount)) {}
	if (appointmentStatus === "cancelled" || ![
		"unfulfilled",
		"paid",
		"partially_paid",
		"unpaid",
		"payment_processing"
	].includes(payment_status)) return true;
	return false;
};
const _formatKey = (data) => {
	var _data$product_sku, _data$product_bundle;
	const _data = require_objectSpread2._objectSpread2({}, data);
	let key = `${_data.product_id}`;
	let rowKey = key;
	if (_data.product_variant_id) {
		let str = `_${_data.product_variant_id}`;
		rowKey += str;
	}
	if (_data === null || _data === void 0 || (_data$product_sku = _data.product_sku) === null || _data$product_sku === void 0 || (_data$product_sku = _data$product_sku.option) === null || _data$product_sku === void 0 ? void 0 : _data$product_sku.length) {
		var _data$product_sku2;
		for (const item of _data === null || _data === void 0 || (_data$product_sku2 = _data.product_sku) === null || _data$product_sku2 === void 0 ? void 0 : _data$product_sku2.option) {
			let str = `_${item.option_group_id}_${item.product_option_item_id || item.option_group_item_id}`;
			rowKey += `${str}_${item.quantity || item.num}`;
		}
	}
	if (_data === null || _data === void 0 || (_data$product_bundle = _data.product_bundle) === null || _data$product_bundle === void 0 ? void 0 : _data$product_bundle.length) for (const item of _data.product_bundle) {
		let str = `_${item.bundle_group_id}_${item.bundle_product_id}`;
		rowKey += `${str}_${item.quantity || item.num}`;
		if (item === null || item === void 0 ? void 0 : item.option) for (const opt of item.option) {
			let str = `_${opt.option_group_id}_${opt.product_option_item_id}`;
			key += str;
			rowKey += `${str}_${opt.quantity || opt.num}`;
		}
	}
	if (_data === null || _data === void 0 ? void 0 : _data.session) {
		var _data$session;
		let str = `_${_data === null || _data === void 0 || (_data$session = _data.session) === null || _data$session === void 0 ? void 0 : _data$session.key}`;
		rowKey += str;
	}
	if (_data === null || _data === void 0 ? void 0 : _data.schedule) {
		var _data$schedule;
		let str = `_${_data === null || _data === void 0 || (_data$schedule = _data.schedule) === null || _data$schedule === void 0 ? void 0 : _data$schedule.key}`;
		rowKey += str;
	}
	return rowKey;
};
const _formatOptions = (detail) => {
	var _detail$product_sku;
	return ((detail === null || detail === void 0 || (_detail$product_sku = detail.product_sku) === null || _detail$product_sku === void 0 ? void 0 : _detail$product_sku.option) || []).map((item) => {
		return require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, item), {}, { price: item.price || item.add_price });
	});
};
const _restoreProductOtherData = (data) => {
	let { detail } = data;
	require_objectWithoutProperties._objectWithoutProperties(data, _excluded);
	if (!detail) return;
	const { discount_list } = detail;
	return {
		rowKey: _formatKey(data),
		product_id: detail.product_id,
		option: _formatOptions(detail),
		bundle: (detail.product_bundle || []).map((bundleItem) => {
			var _bundleItem$bundle_se, _bundleItem$bundle_se2, _bundleItem$bundle_se3, _bundleItem$bundle_se4;
			const price = (bundleItem === null || bundleItem === void 0 ? void 0 : bundleItem.bundle_sum_type) === "markdown" ? `-${Number(bundleItem.bundle_sum_price)}` : Number(bundleItem.bundle_sum_price);
			const discountList = (discount_list || []).filter((item) => {
				var _item$metadata3, _bundleItem$metadata;
				return (item === null || item === void 0 || (_item$metadata3 = item.metadata) === null || _item$metadata3 === void 0 ? void 0 : _item$metadata3.custom_product_bundle_map_id) === ((_bundleItem$metadata = bundleItem.metadata) === null || _bundleItem$metadata === void 0 ? void 0 : _bundleItem$metadata.custom_product_bundle_map_id);
			});
			return require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, bundleItem), {}, {
				booking_id: data.id,
				title: bundleItem.bundle_title,
				discount_list: discountList,
				group_id: bundleItem.bundle_group_id,
				id: bundleItem.bundle_id,
				product_id: bundleItem.bundle_product_id,
				_bundle_product_id: bundleItem.bundle_product_id,
				bundle_variant_id: bundleItem.bundle_variant_id,
				quantity: bundleItem.num,
				option: bundleItem.option.map((optionItem) => {
					return require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, optionItem), {}, {
						name: optionItem.name["auto"],
						quantity: bundleItem.num,
						product_option_item_id: optionItem.option_group_item_id
					});
				}),
				price: (_bundleItem$bundle_se = bundleItem.bundle_selling_price) !== null && _bundleItem$bundle_se !== void 0 ? _bundleItem$bundle_se : price,
				original_total: (_bundleItem$bundle_se2 = bundleItem.bundle_selling_price) !== null && _bundleItem$bundle_se2 !== void 0 ? _bundleItem$bundle_se2 : price,
				original_price: (_bundleItem$bundle_se3 = bundleItem.bundle_selling_price) !== null && _bundleItem$bundle_se3 !== void 0 ? _bundleItem$bundle_se3 : price,
				product_price: (_bundleItem$bundle_se4 = bundleItem.bundle_selling_price) !== null && _bundleItem$bundle_se4 !== void 0 ? _bundleItem$bundle_se4 : price
			});
		}),
		product_variant_id: detail.product_variant_id || 0,
		quantity: Number(detail.product_quantity)
	};
};
const formatProductValues = (data) => {
	return data.map((d) => {
		var _d$metadata;
		return require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, d), {}, {
			_id: (Math.random() + "").replace(/\D/g, "").substring(0, 11),
			id: d.product_id,
			title: d.product_title,
			quantity: d.product_quantity,
			cover: d.product_cover,
			price: Number(d.source_product_price),
			booking_id: d.order_id,
			isEditBooking: true,
			_extend: {
				main_product_selling_price: (_d$metadata = d.metadata) === null || _d$metadata === void 0 ? void 0 : _d$metadata.main_product_selling_price,
				discount_reason: d.product_discount_reason,
				order_detail_id: d.order_detail_id,
				discount_per: d.discount_per,
				discount_type: d.discount_type,
				discountway: d.discountway,
				product_name: d.product_title,
				total: Number(d.selling_price),
				origin_total: Number(d.original_price),
				payment_price: Number(d.payment_price),
				price: Number(d.source_product_price),
				quantity: d.product_quantity,
				bundle_edit: 0,
				other: _restoreProductOtherData(d),
				startDate: (0, dayjs.default)(),
				endDate: (0, dayjs.default)(),
				note: d.note
			},
			payment_price: Number(d.payment_price)
		});
	});
};
const formatBookingDetail = (data, modalState) => {
	var _bookings$list, _bookings$list2;
	let { customer_id, holder, customer_name, email, phone, country_calling_code, bookings, customer, note, payment_status, is_other_entrance_edited, shop_discount, contacts_info, relation_details = [] } = data;
	if (!bookings) bookings = { list: [] };
	const name = ((customer === null || customer === void 0 ? void 0 : customer.display_name) || "").trim() || ((customer === null || customer === void 0 ? void 0 : customer.nickname) || "").trim() || customer_name || "";
	const clientValue = {
		name,
		nickname: name,
		id: customer_id || (customer === null || customer === void 0 ? void 0 : customer.id) || (customer === null || customer === void 0 ? void 0 : customer.customer_id) || 1,
		email: customer === null || customer === void 0 ? void 0 : customer.email,
		phone: (customer === null || customer === void 0 ? void 0 : customer.phone) || "",
		country_calling_code: (customer === null || customer === void 0 ? void 0 : customer.country_calling_code) || "",
		cover: (customer === null || customer === void 0 ? void 0 : customer.cover) || "",
		waiver_form: (customer === null || customer === void 0 ? void 0 : customer.waiver_form) || [],
		contacts_info: { value: contacts_info }
	};
	const start_date = bookings === null || bookings === void 0 || (_bookings$list = bookings.list) === null || _bookings$list === void 0 || (_bookings$list = _bookings$list[0]) === null || _bookings$list === void 0 ? void 0 : _bookings$list.start_date;
	const dateValue = (0, dayjs.default)(start_date ? start_date + " " + (bookings === null || bookings === void 0 || (_bookings$list2 = bookings.list) === null || _bookings$list2 === void 0 || (_bookings$list2 = _bookings$list2[0]) === null || _bookings$list2 === void 0 ? void 0 : _bookings$list2.start_time) : /* @__PURE__ */ new Date());
	const getCapacity = (resetD) => {
		var _resetD$metadata;
		return (_resetD$metadata = resetD.metadata) === null || _resetD$metadata === void 0 ? void 0 : _resetD$metadata.capacity;
	};
	const getResource = (resetD) => {
		if (!resetD.relation_type) try {
			var _resetD$event_resourc;
			if ((resetD === null || resetD === void 0 ? void 0 : resetD.event_resources) && (resetD === null || resetD === void 0 || (_resetD$event_resourc = resetD.event_resources) === null || _resetD$event_resourc === void 0 ? void 0 : _resetD$event_resourc.length)) return resetD.event_resources.reduce((pre, cur) => {
				if (pre[cur.form_id]) pre[cur.form_id].push(cur);
				else pre[cur.form_id] = [cur];
				return pre;
			}, {});
		} catch (err) {
			debugger;
		}
		return resetD.relation_id;
	};
	let serviceValue = bookings.list.map((d) => {
		var _detail$metadata;
		let { detail, product } = d, resetD = require_objectWithoutProperties._objectWithoutProperties(d, _excluded2);
		let _detail = detail || mockProduct;
		let start_date = (0, dayjs.default)(resetD.start_date + " " + resetD.start_time);
		let end_date = (0, dayjs.default)(resetD.end_date + " " + resetD.end_time);
		const endDate = (0, dayjs.default)(end_date).add(1, "minute");
		const multiObj = {};
		if (_isMultiDayProduct(d)) multiObj.duration = endDate.diff(start_date, "days");
		return require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({ _id: (Math.random() + "").replace(/\D/g, "").substring(0, 11) }, _detail), {}, {
			duration: (_detail === null || _detail === void 0 ? void 0 : _detail.duration) || (product === null || product === void 0 ? void 0 : product.duration),
			"schedule.ids": (_detail === null || _detail === void 0 ? void 0 : _detail["schedule.ids"]) || (product === null || product === void 0 ? void 0 : product["schedule.ids"]),
			booking_id: resetD.id,
			id: _detail.product_id,
			title: _detail.product_title,
			quantity: Number(_detail.product_quantity),
			cover: _detail.product_cover,
			price: Number(_detail.source_product_price),
			relation_form_name: resetD.relation_form_name,
			product_resource: product === null || product === void 0 ? void 0 : product.product_resource,
			capacity: product === null || product === void 0 ? void 0 : product.capacity,
			_extend: require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, resetD), {}, {
				quantity: Number(_detail.product_quantity),
				startDate: start_date,
				start_date,
				endDate: end_date,
				end_date,
				holder_id: _formatHolder(resetD),
				price: Number(_detail.source_product_price),
				product_name: _detail.product_title,
				discount_reason: _detail.product_discount_reason,
				order_detail_id: _detail.order_detail_id,
				total: Number(_detail.selling_price),
				origin_total: Number(_detail.original_price),
				payment_price: Number(_detail.payment_price),
				main_product_selling_price: (_detail$metadata = _detail.metadata) === null || _detail$metadata === void 0 ? void 0 : _detail$metadata.main_product_selling_price,
				other: _restoreProductOtherData(d),
				bundle_edit: 0,
				discount_per: _detail.discount_per,
				discount_type: _detail.discount_type,
				discountway: _detail.discountway,
				capacity: getCapacity(resetD),
				resource: getResource(resetD)
			}, multiObj), _formatBookingMetadata(d))
		});
	});
	serviceValue = [...serviceValue || [], ...formatProductValues(relation_details || [])];
	let disabledEdit = _getDisabledEdit(data);
	return require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, modalState), {}, {
		client: require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, modalState.client), {}, {
			value: clientValue,
			edit: false,
			showModal: false
		}),
		contacts_info: require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, (modalState === null || modalState === void 0 ? void 0 : modalState.contacts_info) || {}), {}, {
			edit: false,
			value: contacts_info,
			client_id: clientValue === null || clientValue === void 0 ? void 0 : clientValue.id
		}),
		date: require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, modalState.date), {}, {
			value: dateValue,
			edit: false,
			showModal: false
		}),
		addons: require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, modalState.addons), {}, { load: false }),
		pet: require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, modalState.pet), {}, {
			showModal: false,
			edit: false
		}),
		service: require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, modalState.service), {}, {
			value: serviceValue,
			resources: [],
			addModal: false,
			editModal: false,
			edit: false
		}),
		notes: require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, modalState.notes), {}, {
			orderNote: note,
			edit: false
		}),
		disabledEdit,
		channelDisabledEdit: false,
		appointment_status: bookings === null || bookings === void 0 ? void 0 : bookings.appointment_status
	});
};
//#endregion
exports.formatBookingDetail = formatBookingDetail;
