const require_runtime = require("../../../_virtual/_rolldown/runtime.js");
const require_objectSpread2 = require("../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js");
const require_useLoginUser = require("../../../hooks/useLoginUser.js");
const require_serve = require("./serve.js");
const require_index = require("./index.js");
const require_index$1 = require("../utils/index.js");
let react = require("react");
react = require_runtime.__toESM(react);
let ahooks = require("ahooks");
//#region src/components/Sales/Event/Detail.tsx
const EventDetailContext = (0, react.createContext)(void 0);
const EventDetailProvider = ({ children }) => {
	const eventRef = (0, react.useRef)({});
	const detailRef = (0, react.useRef)({});
	const [detail, setDetail] = (0, react.useState)(null);
	const [open, setOpen] = (0, react.useState)(false);
	const [currentBookingOption, setCurrentBookingOption] = (0, react.useState)(null);
	const { selectEvent } = require_index.useEvent();
	const customer = require_useLoginUser.default();
	const currentBookingOptionRef = (0, react.useRef)(null);
	const _setDetail = (detailData) => {
		detailRef.current = require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, detailRef.current || {}), detailData || {});
		setDetail(require_objectSpread2._objectSpread2({}, detailRef.current));
	};
	const _setCurrentBookingOption = (id) => {
		currentBookingOptionRef.current = id;
		setCurrentBookingOption(id);
	};
	const onOpenEventDetail = (eventItem) => {
		var _eventItem$_originDat;
		setOpen(true);
		const introduction = {
			title: eventItem.title,
			price: eventItem.price,
			images: eventItem.image ? [eventItem.image] : []
		};
		eventRef.current = eventItem;
		_setDetail({ introduction });
		getEventDetailIntroduction.run(eventItem.id);
		getEventDetailResource.run({ event_item_id: eventItem.id });
		getEventDetailBookingOptions.run({
			ids: ((_eventItem$_originDat = eventItem._originData) === null || _eventItem$_originDat === void 0 ? void 0 : _eventItem$_originDat.relation_product_ids) || [],
			num: 100,
			skip: 1,
			customer_id: customer === null || customer === void 0 ? void 0 : customer.id,
			open_quotation: 1,
			is_eject: 1
		});
		getEventDetailSelectDate.run(eventItem.id, { days: 366 });
	};
	const onCloseEventDetail = () => {
		setOpen(false);
		_setDetail(null);
		detailRef.current = null;
		setCurrentBookingOption(null);
		currentBookingOptionRef.current = null;
	};
	/**
	* @title:选择活动详情
	* @param productDetail 商品详情
	*/
	const onSelectEventDetail = () => {
		var _detail$bookingOption, _detailRef$current;
		selectEvent(require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, eventRef.current), {}, { eventDetail: require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, detailRef.current || {}), {}, {
			bookingOptionItem: (_detail$bookingOption = detail.bookingOptions) === null || _detail$bookingOption === void 0 || (_detail$bookingOption = _detail$bookingOption.filter((v) => v.value === currentBookingOptionRef.current)) === null || _detail$bookingOption === void 0 ? void 0 : _detail$bookingOption[0],
			productDetail: (_detailRef$current = detailRef.current) === null || _detailRef$current === void 0 ? void 0 : _detailRef$current.productDetail
		}) }));
		onCloseEventDetail();
	};
	/**
	* Introduction数据获取
	*/
	const getEventDetailIntroduction = (0, ahooks.useRequest)(require_serve.getEventDetailIntroductionApi, {
		manual: true,
		onSuccess: (res) => {
			const data = res.data;
			_setDetail({ introduction: require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, detail.introduction || {}), {}, {
				title: data.title,
				subtitle: data.subtitle,
				description: data.description,
				images: require_index$1.getIamgeFromMedia(data.media),
				_originData: res.data
			}) });
		}
	});
	/**
	* Schedule Date数据获取
	*/
	const getEventDetailSelectDate = (0, ahooks.useRequest)(require_serve.getEventDetailSelectDateApi, {
		manual: true,
		onSuccess: (res) => {
			var _detailRef$current2, _detailRef$current3;
			const data = res.data;
			const dates = [...data.dates || []];
			const startDateInfo = (_detailRef$current2 = detailRef.current) === null || _detailRef$current2 === void 0 || (_detailRef$current2 = _detailRef$current2.selectDate) === null || _detailRef$current2 === void 0 ? void 0 : _detailRef$current2.startDateInfo;
			const endDateInfo = (_detailRef$current3 = detailRef.current) === null || _detailRef$current3 === void 0 || (_detailRef$current3 = _detailRef$current3.selectDate) === null || _detailRef$current3 === void 0 ? void 0 : _detailRef$current3.endDateInfo;
			const selectDate = {
				dates,
				_originData: {
					dates,
					default_data: data.default_data,
					shop_time: data.shop_time
				},
				startDateInfo,
				endDateInfo
			};
			if (!startDateInfo) selectDate.startDateInfo = dates === null || dates === void 0 ? void 0 : dates[0];
			if (!endDateInfo && (dates === null || dates === void 0 ? void 0 : dates.length)) selectDate.endDateInfo = dates === null || dates === void 0 ? void 0 : dates[dates.length - 1];
			_setDetail({ selectDate });
		}
	});
	/**
	* 资源获取
	*/
	const getEventDetailResource = (0, ahooks.useRequest)(require_serve.getEventDetailTeacherApi, {
		manual: true,
		onSuccess: (res) => {
			const data = res.data || [];
			const resource = [];
			data.forEach((item) => {
				var _item$resource;
				resource.push({
					title: item.form_name,
					children: item === null || item === void 0 || (_item$resource = item.resource) === null || _item$resource === void 0 ? void 0 : _item$resource.map((v) => {
						var _v$form_data;
						return {
							id: v.id,
							title: v.resource_name,
							image: require_index$1.getIamgeFromMedia(v === null || v === void 0 || (_v$form_data = v.form_data) === null || _v$form_data === void 0 ? void 0 : _v$form_data.media, "findOne"),
							_originData: v
						};
					})
				});
			});
			_setDetail({ resource });
		}
	});
	/**
	* BookingOptions数据获取
	*/
	const getEventDetailBookingOptions = (0, ahooks.useRequest)(require_serve.getEventDetailBookingOptionsApi, {
		manual: true,
		onSuccess: (res) => {
			var _res$data;
			const list = ((_res$data = res.data) === null || _res$data === void 0 ? void 0 : _res$data.list) || [];
			let current = null;
			_setDetail({ bookingOptions: list.map((v) => {
				const option = {
					value: v.id,
					label: v.title,
					price: v.price,
					_originData: v
				};
				if (!current || Number((current === null || current === void 0 ? void 0 : current.price) || 0) > Number(v.price || 0)) current = require_objectSpread2._objectSpread2({}, option);
				return option;
			}) });
			selectEventDetailBookingOptions(current.value);
		}
	});
	const getProductDetailRequest = (0, ahooks.useRequest)(require_serve.getProductDetailApi, {
		manual: true,
		loadingDelay: 200
	});
	/**
	* @title: 选择活动关联的商品
	* @param id 活动关联的商品id
	*/
	const selectEventDetailBookingOptions = (id) => {
		var _bookingOptions$filte, _option$_originData;
		const { introduction, bookingOptions } = detailRef.current || {};
		const option = bookingOptions === null || bookingOptions === void 0 || (_bookingOptions$filte = bookingOptions.filter((v) => v.value === id)) === null || _bookingOptions$filte === void 0 ? void 0 : _bookingOptions$filte[0];
		const setSelectedData = (data) => {
			var _detailRef$current4;
			_setDetail({
				introduction: require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, (detailRef === null || detailRef === void 0 || (_detailRef$current4 = detailRef.current) === null || _detailRef$current4 === void 0 ? void 0 : _detailRef$current4.introduction) || {}), {}, {
					price: data === null || data === void 0 ? void 0 : data.price,
					origin_price: data === null || data === void 0 ? void 0 : data.original_price
				}),
				selectedProduct: data
			});
			_setCurrentBookingOption(id);
		};
		if ((option === null || option === void 0 || (_option$_originData = option._originData) === null || _option$_originData === void 0 ? void 0 : _option$_originData.is_eject) === 0) {
			var _eventRef$current;
			getProductDetailRequest.runAsync(id, {
				open_deposit: 1,
				front_end_cache_id: (eventRef === null || eventRef === void 0 || (_eventRef$current = eventRef.current) === null || _eventRef$current === void 0 ? void 0 : _eventRef$current.id) + "_" + id
			}).then((res) => {
				var _res$data2;
				const custom_deposit_data = res === null || res === void 0 || (_res$data2 = res.data) === null || _res$data2 === void 0 ? void 0 : _res$data2.custom_deposit_data;
				const newOption = require_objectSpread2._objectSpread2({}, option);
				if (custom_deposit_data && newOption._originData) newOption._originData.custom_deposit_data = custom_deposit_data;
				setSelectedData(newOption);
			}).catch(() => {
				setSelectedData(option);
			});
		} else setSelectedData(option);
	};
	/**
	* @title: 选择商品详情
	* @param {any} productDetail 商品详情
	*/
	const selectEventDetailProductDetail = (productDetail) => {
		_setDetail({ productDetail });
	};
	/**
	* @title: 校验is_eject为1的商品是否有拉起商品详情弹窗选中过
	* @returns boolean
	*/
	const checkBeforeSelectEventDetail = () => {
		var _selectedProduct$_ori;
		const { selectedProduct, productDetail } = detailRef.current || {};
		console.log("selectedProduct", selectedProduct, productDetail);
		if ((selectedProduct === null || selectedProduct === void 0 || (_selectedProduct$_ori = selectedProduct._originData) === null || _selectedProduct$_ori === void 0 ? void 0 : _selectedProduct$_ori.is_eject) === 1 && !productDetail) return false;
		return true;
	};
	const contextValue = {
		detail,
		open,
		onOpenEventDetail,
		onCloseEventDetail,
		onSelectEventDetail,
		selectEventDetailBookingOptions,
		getEventDetailSelectDate,
		currentBookingOption,
		selectEventDetailProductDetail,
		checkBeforeSelectEventDetail,
		getProductDetailRequest
	};
	return /* @__PURE__ */ react.default.createElement(EventDetailContext.Provider, { value: contextValue }, children);
};
/**
* 使用步骤管理的Hook
* @throws {Error} 如果在StepProvider外部使用会抛出错误
* @returns {StepContextType} 步骤管理上下文
*/
const useEventDetail = () => {
	const context = react.default.useContext(EventDetailContext);
	if (!context) throw new Error("useEventDetail must be used within a EventDetailProvider");
	return context;
};
//#endregion
exports.EventDetailProvider = EventDetailProvider;
exports.useEventDetail = useEventDetail;
