import { _objectSpread2 } from "../../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js";
import { defaultData } from "./status.js";
import { formatActiveKey, formatProducts, isEventBooking, updateServices } from "./utils.js";
import { Context } from "../model.js";
import useSelectPet from "./hooks/useSelectPet.js";
import { CartContext } from "../../../shoppingCart/Provider.js";
import shoppingCart from "../../../shoppingCart/index.js";
import React, { useContext, useEffect, useMemo, useState } from "react";
//#region src/components/booking/info/service2/Lists.tsx
const Lists = ({ onEdit, onChange }) => {
	var _state$service;
	const { state } = useContext(Context);
	const [params, setParams] = useState(defaultData);
	const { holderLists } = useSelectPet(state);
	useEffect(() => {
		const keys = formatActiveKey(holderLists, params === null || params === void 0 ? void 0 : params.platform, !!state.bookingId);
		onChange({ activeCartId: keys }, "setActiveCartId");
		setParams(_objectSpread2(_objectSpread2({}, params), {}, { activeKey: keys }));
	}, [holderLists, params === null || params === void 0 ? void 0 : params.platform]);
	/**
	* @title: 更新商品数据
	* @description:
	* @param {any} newState
	* @return {*}
	* @Author: WangHan
	* @Date: 2024-11-29 16:35
	*/
	const handleChange = (currentItem, type, other) => {
		if (state.disabledEdit || state.disableEventBookingEdit) return;
		let products = updateServices(state, currentItem, type, other);
		if (type === "view") {
			var _window$openBookingEd, _window;
			let product = currentItem._origin;
			(_window$openBookingEd = (_window = window).openBookingEditModal) === null || _window$openBookingEd === void 0 || _window$openBookingEd.call(_window);
			onChange({
				editModal: true,
				cacheItem: _objectSpread2(_objectSpread2({}, product), {}, { new: 0 })
			});
			return onEdit === null || onEdit === void 0 ? void 0 : onEdit(_objectSpread2(_objectSpread2({}, product), {}, { new: 0 }));
		}
		onChange({
			editModal: false,
			value: products,
			edit: true,
			cacheItem: null
		});
	};
	/**
	* @title: 打开哪个折叠面板的keys
	* @description:
	* @param {any} key
	* @return {*}
	* @Author: WangHan
	* @Date: 2024-12-09 16:06
	*/
	const handleActiveKey = (key) => {
		onChange({ activeCartId: key }, "setActiveCartId");
		setParams(_objectSpread2(_objectSpread2({}, params), {}, { activeKey: key }));
	};
	/**
	* @title: 折叠面板参数
	* @description:
	* @param {*} useMemo
	* @return {*}
	* @Author: WangHan
	* @Date: 2024-12-05 15:22
	*/
	const CollapseParams = useMemo(() => {
		return {
			activeKey: params === null || params === void 0 ? void 0 : params.activeKey,
			bordered: false
		};
	}, [params === null || params === void 0 ? void 0 : params.activeKey]);
	/**
	* @title: 商品信息
	* @description:
	* @param {*} useMemo
	* @return {*}
	* @Author: WangHan
	* @Date: 2024-12-09 16:07
	*/
	const productParams = useMemo(() => {
		return {
			hideAddBtn: state.isTerminalDayBookingPage,
			isShowNote: isEventBooking(state),
			isShowPackageNote: false,
			holders: holderLists,
			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
		};
	}, [holderLists, state === null || state === void 0 ? void 0 : state.bookingId]);
	return /* @__PURE__ */ React.createElement(CartContext.Provider, { value: { state } }, /* @__PURE__ */ React.createElement(shoppingCart, {
		onChange: handleChange,
		onAddProduct: () => onChange({ addModal: true }),
		onActiveKey: handleActiveKey,
		collapseParams: CollapseParams,
		productParams: _objectSpread2(_objectSpread2({}, productParams), {}, { products: formatProducts(state === null || state === void 0 || (_state$service = state.service) === null || _state$service === void 0 ? void 0 : _state$service.value, state) })
	}));
};
//#endregion
export { Lists as default };
