const require_runtime = require("../../../../_virtual/_rolldown/runtime.js");
const require_objectSpread2 = require("../../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js");
const require_asyncToGenerator = require("../../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/asyncToGenerator.js");
const require_useEngineContext = require("../../../../hooks/useEngineContext.js");
const require_index = require("../../../../plus/productSelect/index.js");
const require_utils = require("../../utils.js");
const require_context = require("../../context.js");
const require_hooks = require("../../hooks.js");
let dayjs = require("dayjs");
dayjs = require_runtime.__toESM(dayjs);
let react = require("react");
react = require_runtime.__toESM(react);
let _pisell_utils = require("@pisell/utils");
require("./index.less");
//#region src/components/appointmentBooking/components/Services/index.tsx
const Services = (0, react.forwardRef)((props, ref) => {
	const context = require_useEngineContext.default();
	const { utils } = context.appHelper || {};
	const appointmentBooking = require_hooks.useAppointmentBooking();
	const { isDateSelectedFirst, isRetailTemplate } = require_hooks.useAppointmentBookingSetting();
	const { getProducts, checkProductType } = require_hooks.useAppointmentBookingProduct();
	const { removeStep, nextStep } = require_hooks.useAppointmentBookingStep();
	const { currentStep, setLoading, carts } = require_context.useAppointmentBookingContext();
	const [products, setProducts] = (0, react.useState)(null);
	const [productLoading, setProductLoading] = (0, react.useState)(false);
	const { isMobile } = require_hooks.useConfig();
	(0, react.useImperativeHandle)(ref, () => ({ next: () => {
		setLoading(true);
		setTimeout(() => {
			nextStep();
			setLoading(false);
		}, 500);
	} }));
	(0, react.useEffect)(() => {
		setProductLoading(true);
		getProducts().then((res) => {
			setProducts(res || []);
			setProductLoading(false);
		}).catch(() => {
			setProducts([]);
			setProductLoading(false);
		});
	}, [isDateSelectedFirst]);
	/**
	* @description: 是否显示商品详情页面的步进器
	* @param {*} useMemo
	* @return {*}
	*/
	const isShowProductDetailStepper = (0, react.useMemo)(() => {
		const { productModuleParams } = (appointmentBooking === null || appointmentBooking === void 0 ? void 0 : appointmentBooking.otherParams) || {};
		return productModuleParams === null || productModuleParams === void 0 ? void 0 : productModuleParams.isShowProductDetailStepper;
	}, [appointmentBooking === null || appointmentBooking === void 0 ? void 0 : appointmentBooking.otherParams]);
	const handleSelectProduct = function() {
		var _ref2 = require_asyncToGenerator._asyncToGenerator(function* (item, clickType) {
			let type = "";
			if (!isRetailTemplate) {
				type = yield checkProductType(item === null || item === void 0 ? void 0 : item.id);
				appointmentBooking.openProductDetail(item.id);
			}
			utils === null || utils === void 0 || utils.action({
				type: "pisell1.productDetail",
				data: {
					detail: require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, item), {}, { open_deposit: 1 }),
					setting: {
						can_input: true,
						item_max_length: isShowProductDetailStepper,
						is_detail_open: clickType === "cover"
					}
				},
				callback: function() {
					var _ref = require_asyncToGenerator._asyncToGenerator(function* (data) {
						if (data.isCancel) {
							appointmentBooking.closeProductDetail();
							return;
						}
						const product = require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, data), {}, { options: (data === null || data === void 0 ? void 0 : data.option) || [] });
						delete product.option;
						if (isRetailTemplate) {
							if (data === null || data === void 0 ? void 0 : data.isCancel) return;
							const res = appointmentBooking.storeProduct(product);
							if (!res.success && res.errorCode === "not_enough_stock") return require_utils.Toast(_pisell_utils.locales.getText("pisell2.appointment.unuse.stock"));
							return;
						}
						const session = data === null || data === void 0 ? void 0 : data.session;
						if (session) {
							const startDate = (0, dayjs.default)(session === null || session === void 0 ? void 0 : session.start_at).format("YYYY-MM-DD");
							const endDate = (0, dayjs.default)(session === null || session === void 0 ? void 0 : session.end_at).format("YYYY-MM-DD");
							const res = appointmentBooking.addProductToCart({
								product: require_objectSpread2._objectSpread2({}, product),
								date: {
									startTime: `${startDate} ${session === null || session === void 0 ? void 0 : session.start_time}`,
									endTime: `${endDate} ${session === null || session === void 0 ? void 0 : session.end_time}`
								}
							});
							if (!res.success && res.errorCode === "not_enough_stock") return require_utils.Toast(_pisell_utils.locales.getText("pisell2.appointment.unuse.stock"));
							appointmentBooking.closeProductDetail();
							yield appointmentBooking.getAvailableDate({
								startDate,
								endDate
							});
						} else {
							var _data$origin;
							if (!((data === null || data === void 0 || (_data$origin = data.origin) === null || _data$origin === void 0 || (_data$origin = _data$origin.product_resource) === null || _data$origin === void 0 ? void 0 : _data$origin.resources) || []).find((item) => item.status === 1) && !appointmentBooking.isTargetNormalProduct(data === null || data === void 0 ? void 0 : data.origin)) return require_utils.Toast(_pisell_utils.locales.getText("pisell2.appointment.no.available.service"));
							const res = appointmentBooking.storeProduct(product);
							if (!res.success && res.errorCode === "not_enough_stock") return require_utils.Toast(_pisell_utils.locales.getText("pisell2.appointment.unuse.stock"));
							if (type === "duration") yield appointmentBooking.getAvailableDate();
						}
						appointmentBooking.closeProductDetail();
						if (isDateSelectedFirst) {
							const index = (currentStep === null || currentStep === void 0 ? void 0 : currentStep.index) + 2;
							if (type === "duration" || appointmentBooking.isCartHasDurationProduct()) {
								const stepList = appointmentBooking.getStepList();
								if (stepList === null || stepList === void 0 ? void 0 : stepList.find((item) => (item === null || item === void 0 ? void 0 : item.key) === "select-resource")) {
									const description = _pisell_utils.locales.getText("pisell2.appointment.select.time");
									appointmentBooking.addStep({
										index,
										key: "select-time",
										description
									}, "select-resource");
								}
							} else removeStep("select-time");
						} else if (type === "session") {
							removeStep("select-date");
							removeStep("select-time");
						}
					});
					return function callback(_x) {
						return _ref.apply(this, arguments);
					};
				}()
			});
		});
		return function handleSelectProduct(_x2, _x3) {
			return _ref2.apply(this, arguments);
		};
	}();
	const productListSetting = (0, react.useMemo)(() => {
		var _context$engine;
		const { customPageData } = ((_context$engine = context.engine) === null || _context$engine === void 0 ? void 0 : _context$engine.props) || {};
		let productListBlock;
		if (Array.isArray(customPageData === null || customPageData === void 0 ? void 0 : customPageData.block)) productListBlock = customPageData.block.find((b) => "product_list" === b.type);
		return (productListBlock === null || productListBlock === void 0 ? void 0 : productListBlock.setting) || {};
	}, [context]);
	const _isMobile = (0, react.useMemo)(() => {
		if (typeof isMobile === "function") return isMobile();
		return isMobile;
	}, [isMobile]);
	return /* @__PURE__ */ react.default.createElement("div", { className: "appointment-service-wrap" }, /* @__PURE__ */ react.default.createElement(require_index.default, {
		dataSource: products,
		onChange: handleSelectProduct,
		scrollDom: _isMobile ? ".parallel-content-wrap" : ".parallel-booking-wrap",
		offsetDom: _isMobile ? "" : ".pisell-section-headers",
		loading: productLoading,
		emptyDescription: _pisell_utils.locales.getText("pisell2.appointment.booking.empty.service"),
		token: {
			colorPrimary: "var(--theme-color)",
			containerBg: "#FFFFFF",
			tabItemBg: "#F2F4F7",
			tabItemHoverBg: "#F2F4F7",
			tabItemActiveBg: "var(--theme-color)",
			categoryTextColor: "#1B1B1B",
			cardSelectedBg: "var(--theme-color)"
		},
		productListSetting,
		selectedItems: carts,
		hideCategoryWhenOne: true
	}));
});
//#endregion
exports.default = Services;
