import { _objectSpread2 } from "../../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js";
import { getProductExtend } from "../../info2/service/addService/utils.js";
import { formatProducts, isEventBooking, isNormalProductByDurationSchedule, updateServices } from "../service2/utils.js";
import { Context } from "../model.js";
import useHolderConfig from "../hooks/useHolderConfig.js";
import { CartContext } from "../../../shoppingCart/Provider.js";
import shoppingCart from "../../../shoppingCart/index.js";
import useAddHolder from "../hooks/useAddHolder.js";
import React, { useContext, useMemo } from "react";
import { useMemoizedFn } from "ahooks";
//#region src/components/booking/info/service/Lists.tsx
const Lists = ({ onEdit, onChange, onAction, isParallelResource, isShowDelete = true, isShowAmountFooter = true }) => {
	var _state$service2;
	const { state, dispatch } = useContext(Context);
	const { isShowChangeHolder } = useHolderConfig(state);
	const { handleAddHolder } = useAddHolder(state, dispatch);
	/**
	* @title: 更新商品数据
	* @description:
	* @param {any} newState
	* @return {*}
	* @Author: WangHan
	* @Date: 2024-11-29 16:35
	*/
	const handleChange = useMemoizedFn((currentItem, type, other) => {
		var _window$closeBookingE, _window2;
		console.time("handleChange");
		console.log(window.pp1 = performance.now(), "打开商品");
		if (state.disabledEdit || state.disableEventBookingEdit) return;
		let products = updateServices(state, currentItem, type, other, { handleAddHolder });
		if (type === "view" || type === "edit_product") {
			let product = currentItem._origin;
			let cacheItem = _objectSpread2(_objectSpread2({}, product), {}, { new: 0 });
			if (isParallelResource && !isNormalProductByDurationSchedule(product)) cacheItem = getProductExtend({
				cacheItem,
				state
			});
			if (!isNormalProductByDurationSchedule(cacheItem)) {
				var _window$openBookingEd, _window;
				(_window$openBookingEd = (_window = window).openBookingEditModal) === null || _window$openBookingEd === void 0 || _window$openBookingEd.call(_window);
				onChange({
					editModal: true,
					cacheItem
				});
			}
			return onEdit === null || onEdit === void 0 ? void 0 : onEdit(cacheItem, type);
		}
		(_window$closeBookingE = (_window2 = window).closeBookingEditModal) === null || _window$closeBookingE === void 0 || _window$closeBookingE.call(_window2);
		onChange({
			editModal: false,
			value: products,
			edit: true,
			cacheItem: null
		});
	});
	/**
	* @title: 商品信息
	* @description:
	* @param {*} useMemo
	* @return {*}
	* @Author: WangHan
	* @Date: 2024-12-09 16:07
	*/
	const productParams = useMemo(() => {
		var _state$bookingConfig;
		return {
			hideAddBtn: state.isTerminalDayBookingPage || state.isBookingCreatePage,
			isShowNote: state.isBookingTicket2 ? true : isEventBooking(state),
			isShowDelete,
			isShowAmountFooter,
			isShowPackageNote: false,
			accounts: [],
			isEdit: !!(state === null || state === void 0 ? void 0 : state.bookingId),
			disabledEdit: state === null || state === void 0 ? void 0 : state.disabledEdit,
			maxLength: isEventBooking(state) ? 1 : 999,
			isShowChangeHolder,
			isShowChangeResource: true,
			isShowAddHolderButton: true,
			isShowEditProduct: !state.isBookingCreatePage,
			productCardStyleConfig: (_state$bookingConfig = state.bookingConfig) === null || _state$bookingConfig === void 0 || (_state$bookingConfig = _state$bookingConfig.config) === null || _state$bookingConfig === void 0 ? void 0 : _state$bookingConfig.cart_settings_tab
		};
	}, [
		state === null || state === void 0 ? void 0 : state.bookingId,
		state === null || state === void 0 ? void 0 : state.disabledEdit,
		isShowChangeHolder,
		state.isBookingCreatePage,
		state.bookingConfig
	]);
	const products = useMemo(() => {
		var _state$service;
		console.time("products");
		const p = formatProducts((state === null || state === void 0 || (_state$service = state.service) === null || _state$service === void 0 ? void 0 : _state$service.value) || [], state, true, true, isParallelResource);
		console.timeEnd("products");
		return p;
	}, [state === null || state === void 0 || (_state$service2 = state.service) === null || _state$service2 === void 0 ? void 0 : _state$service2.value, state]);
	return useMemo(() => {
		console.log("render List");
		return /* @__PURE__ */ React.createElement(CartContext.Provider, { value: { state } }, /* @__PURE__ */ React.createElement(shoppingCart, {
			scenario: state.isBookingTicket2 ? "booking2" : void 0,
			isParallelResource,
			onAction,
			onChange: handleChange,
			isOpenGroup: false,
			isHideClearCart: true,
			onAddProduct: () => onChange({ addModal: true }),
			productParams: _objectSpread2(_objectSpread2({}, productParams), {}, { products })
		}));
	}, [
		products,
		state,
		productParams
	]);
};
//#endregion
export { Lists as default };
