import { _objectSpread2 } from "../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js";
import { useSales } from "../../../businessModel/SalesModel/Sales/index.js";
import { useCart } from "../Cart/index.js";
import { getEventListApi } from "./serve.js";
import React, { createContext, useMemo } from "react";
import { useRequest } from "ahooks";
//#region src/components/Sales/Event/index.tsx
const defaultState = {
	currentEventItem: null,
	eventList: []
};
const EventContext = createContext(void 0);
const EventProvider = ({ children }) => {
	const { data, setData, getCurrentData } = useSales();
	const { updateCurrentCartItem, currentCartItem, createCartItemFromEvent } = useCart();
	const eventData = useMemo(() => {
		return data.eventData || {};
	}, [data.eventData]);
	const setEventData = (newEventData) => {
		setData({ eventData: _objectSpread2(_objectSpread2({}, getCurrentData().eventData), newEventData) });
	};
	const getEventList = useRequest(getEventListApi, {
		manual: true,
		onSuccess: (res) => {
			var _res$data;
			const eventList = [];
			if (res === null || res === void 0 || (_res$data = res.data) === null || _res$data === void 0 || (_res$data = _res$data.list) === null || _res$data === void 0 ? void 0 : _res$data.length) res.data.list.forEach((item) => {
				var _item$relation_produc;
				if (item === null || item === void 0 || (_item$relation_produc = item.relation_product_ids) === null || _item$relation_produc === void 0 ? void 0 : _item$relation_produc.length) {
					var _item$relation_produc2;
					eventList.push({
						id: item.id,
						title: item.title,
						price: item.price,
						image: item.cover || "",
						isMultiPrice: !!((item === null || item === void 0 || (_item$relation_produc2 = item.relation_product_ids) === null || _item$relation_produc2 === void 0 ? void 0 : _item$relation_produc2.length) > 1),
						_originData: item
					});
				}
			});
			setEventData({ eventList });
		}
	});
	const getEventListByTitle = (title) => {
		var _eventData$eventList;
		if (!title) return eventData.eventList || [];
		const newEventList = [];
		if (eventData === null || eventData === void 0 || (_eventData$eventList = eventData.eventList) === null || _eventData$eventList === void 0 ? void 0 : _eventData$eventList.length) eventData.eventList.forEach((item) => {
			var _item$title;
			if ((_item$title = item.title) === null || _item$title === void 0 ? void 0 : _item$title.includes(title || "")) newEventList.push(item);
		});
		return newEventList;
	};
	const selectEvent = (eventItem) => {
		setEventData({ currentEventItem: eventItem });
		createCartItemFromEvent(eventItem);
	};
	const contextValue = {
		eventData,
		getEventList,
		getEventListByTitle,
		selectEvent
	};
	return /* @__PURE__ */ React.createElement(EventContext.Provider, { value: contextValue }, children);
};
/**
* 使用步骤管理的Hook
* @throws {Error} 如果在StepProvider外部使用会抛出错误
* @returns {StepContextType} 步骤管理上下文
*/
const useEvent = () => {
	const context = React.useContext(EventContext);
	if (!context) throw new Error("useEvent must be used within a EventProvider");
	return context;
};
var Event_default = { defaultState };
//#endregion
export { EventProvider, Event_default as default, useEvent };
