import { _objectSpread2 } from "../../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js";
import { _asyncToGenerator } from "../../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/asyncToGenerator.js";
import useEngineContext from "../../../../hooks/useEngineContext.js";
import useComponents from "../../../../hooks/useComponents.js";
import { formatCustomer } from "../../utils.js";
import { useAppointmentBookingContext } from "../../context.js";
import { scrollToFirstErrorCard } from "../Resource/utils.js";
import { contextUtils, useAppointmentBookingAccount, useAppointmentBookingCart, useAppointmentBookingLogin, useAppointmentBookingSetting, useAppointmentBookingStep, useConfig, useShopDiscount } from "../../hooks.js";
import DepositModal from "../../deposit/components/PolicyModal/index.js";
import CartItem from "../Cart/Item/index.js";
import Header from "../Header/index.js";
import Footer from "../Footer/index.js";
import Services from "../Services/index.js";
import Date from "../Date/index.js";
import Resource from "../Resource/index.js";
import TimeSlicing from "../TimeSlicing/index.js";
import Guest from "../Guest/index.js";
import Form from "../Form/index.js";
import ConfirmInformation from "../ConfirmInformation/index.js";
import React, { useEffect, useMemo, useRef, useState } from "react";
import { locales } from "@pisell/utils";
import "./index.less";
//#region src/components/appointmentBooking/components/Content/index.tsx
const Layout = () => {
	var _useEngineContext$eng, _currentStep$step2, _currentStep$step4, _currentStep$step6, _currentStep$step8;
	const stepRef = useRef();
	const { currentStep, setResourceOtherParams, resourceOtherParams, setIsAgreeDepositPolicy, appointmentBooking, setLoading, policyParams, closePolicy, currentStepParams, setIsErrorHolder } = useAppointmentBookingContext();
	const { isDateSelectedFirst, isFormSubject, isRetailTemplate } = useAppointmentBookingSetting();
	const { isLogin } = useAppointmentBookingLogin();
	const { getHolders } = useAppointmentBookingAccount();
	const { removeStep } = useAppointmentBookingStep();
	const { shopDiscount } = useShopDiscount();
	const { isMobile } = useConfig();
	const { customPageData } = (_useEngineContext$eng = useEngineContext().engine) === null || _useEngineContext$eng === void 0 ? void 0 : _useEngineContext$eng.props;
	const [CustomComponent] = useComponents({ components: ["CustomComponent"] });
	console.log("CustomComponent:", CustomComponent, typeof CustomComponent, CustomComponent === null || CustomComponent === void 0 ? void 0 : CustomComponent.$$typeof);
	useAppointmentBookingCart();
	useAppointmentBookingStep();
	const [params, setParams] = useState({});
	const utils = contextUtils();
	const { pubsub } = useEngineContext().appHelper.utils;
	useEffect(() => {
		const handleLoginSuccess = function() {
			var _ref = _asyncToGenerator(function* (res) {
				if (!isFormSubject) {
					var _utils$storage;
					const currentUser = formatCustomer(JSON.parse((utils === null || utils === void 0 || (_utils$storage = utils.storage) === null || _utils$storage === void 0 ? void 0 : _utils$storage.get("customer")) || "{}"));
					const accountLists = yield appointmentBooking.getAccounts();
					const meAccount = accountLists === null || accountLists === void 0 ? void 0 : accountLists.find((d) => !(d === null || d === void 0 ? void 0 : d.isGuest));
					appointmentBooking.setLoginAccount(meAccount === null || meAccount === void 0 ? void 0 : meAccount.id, currentUser);
				} else if (!isRetailTemplate) getHolders();
			});
			return function handleLoginSuccess(_x) {
				return _ref.apply(this, arguments);
			};
		}();
		pubsub.subscribe("pisell1.login.success", handleLoginSuccess);
		return () => {
			pubsub.unsubscribe("pisell1.login.success", handleLoginSuccess);
		};
	}, []);
	useEffect(() => {
		appointmentBooking.removeStep("fill-form");
		return () => {
			var _shopDiscount$destroy;
			setIsAgreeDepositPolicy(false);
			appointmentBooking.destroy();
			shopDiscount === null || shopDiscount === void 0 || (_shopDiscount$destroy = shopDiscount.destroy) === null || _shopDiscount$destroy === void 0 || _shopDiscount$destroy.call(shopDiscount);
		};
	}, []);
	useEffect(() => {
		var _currentStepParams$st;
		if ((currentStepParams === null || currentStepParams === void 0 ? void 0 : currentStepParams.index) < 0) return;
		const resourceStepIndex = currentStepParams === null || currentStepParams === void 0 || (_currentStepParams$st = currentStepParams.stepLists) === null || _currentStepParams$st === void 0 ? void 0 : _currentStepParams$st.findIndex((item) => item.key === "select-resource");
		if ((currentStepParams === null || currentStepParams === void 0 ? void 0 : currentStepParams.index) < resourceStepIndex) appointmentBooking.setOtherData("current_selected_resources", null);
	}, [currentStepParams]);
	useEffect(() => {
		try {
			var _appointmentBooking$o;
			if (!(appointmentBooking === null || appointmentBooking === void 0 ? void 0 : appointmentBooking.otherParams) || isRetailTemplate) return;
			const showAddGuest = appointmentBooking === null || appointmentBooking === void 0 || (_appointmentBooking$o = appointmentBooking.otherParams) === null || _appointmentBooking$o === void 0 || (_appointmentBooking$o = _appointmentBooking$o.productModuleParams) === null || _appointmentBooking$o === void 0 ? void 0 : _appointmentBooking$o.showAddGuest;
			if (isFormSubject || !showAddGuest) removeStep("select-guest");
			else appointmentBooking.addStep({
				key: "select-guest",
				description: locales.getText("pisell2.appointment.add-guests")
			}, "select-service");
		} catch (error) {}
	}, [isFormSubject, isRetailTemplate]);
	useEffect(() => {
		if (isFormSubject && isLogin && !isRetailTemplate) getHolders();
	}, [
		isFormSubject,
		isLogin,
		isRetailTemplate
	]);
	useEffect(() => {
		var _currentStep$step;
		if (!(currentStep === null || currentStep === void 0 ? void 0 : currentStep.step)) return;
		setLoading(false);
		setResourceOtherParams(_objectSpread2(_objectSpread2({}, resourceOtherParams), {}, { errorList: null }));
		setIsErrorHolder(false);
		window.scrollTo({
			top: 0,
			behavior: "smooth"
		});
		if ((currentStep === null || currentStep === void 0 || (_currentStep$step = currentStep.step) === null || _currentStep$step === void 0 ? void 0 : _currentStep$step.key) === "confirm-information") scrollToFirstErrorCard(".pisell-lowcode-product-card-error");
	}, [currentStep === null || currentStep === void 0 || (_currentStep$step2 = currentStep.step) === null || _currentStep$step2 === void 0 ? void 0 : _currentStep$step2.key]);
	const components = {
		"select-service": Services,
		"select-guest": Guest,
		"select-date": Date,
		"select-time": TimeSlicing,
		"confirm-information": ConfirmInformation
	};
	const Com = useMemo(() => {
		var _currentStep$step3;
		const key = currentStep === null || currentStep === void 0 || (_currentStep$step3 = currentStep.step) === null || _currentStep$step3 === void 0 ? void 0 : _currentStep$step3.key;
		if (!key) return null;
		let Com;
		if (key.startsWith("select-resource")) Com = Resource;
		else if (key.startsWith("fill-form")) Com = Form;
		else Com = components[key];
		if (key === "select-service") return /* @__PURE__ */ React.createElement(CustomComponent, {
			block: customPageData === null || customPageData === void 0 ? void 0 : customPageData.block,
			customComponents: { product_list: /* @__PURE__ */ React.createElement(Com, {
				onChange: setParams,
				params,
				ref: stepRef
			}) }
		});
		else return /* @__PURE__ */ React.createElement(Com, {
			onChange: setParams,
			params,
			ref: stepRef
		});
	}, [currentStep === null || currentStep === void 0 || (_currentStep$step4 = currentStep.step) === null || _currentStep$step4 === void 0 ? void 0 : _currentStep$step4.key]);
	const isShowCartCom = useMemo(() => {
		var _currentStep$step5;
		const key = currentStep === null || currentStep === void 0 || (_currentStep$step5 = currentStep.step) === null || _currentStep$step5 === void 0 ? void 0 : _currentStep$step5.key;
		if (isMobile) return null;
		if (isDateSelectedFirst && key === "select-date") return null;
		return /* @__PURE__ */ React.createElement("div", { className: "parallel-content-right" }, /* @__PURE__ */ React.createElement(CartItem, { onNext: () => {
			var _stepRef$current, _stepRef$current$next;
			return stepRef === null || stepRef === void 0 || (_stepRef$current = stepRef.current) === null || _stepRef$current === void 0 || (_stepRef$current$next = _stepRef$current.next) === null || _stepRef$current$next === void 0 ? void 0 : _stepRef$current$next.call(_stepRef$current);
		} }));
	}, [isDateSelectedFirst, currentStep === null || currentStep === void 0 || (_currentStep$step6 = currentStep.step) === null || _currentStep$step6 === void 0 ? void 0 : _currentStep$step6.key]);
	/**
	* @title: 是否显示底部
	* @description:
	* @param {*} useMemo
	* @return {*}
	* @Author: WangHan
	* @Date: 2025-05-10 17:42
	*/
	const isFooter = useMemo(() => {
		var _currentStep$step7;
		if (!isMobile) return false;
		const key = currentStep === null || currentStep === void 0 || (_currentStep$step7 = currentStep.step) === null || _currentStep$step7 === void 0 ? void 0 : _currentStep$step7.key;
		if (isDateSelectedFirst && key === "select-date") return false;
		return key !== "confirm-information";
	}, [
		isDateSelectedFirst,
		currentStep === null || currentStep === void 0 || (_currentStep$step8 = currentStep.step) === null || _currentStep$step8 === void 0 ? void 0 : _currentStep$step8.key,
		isMobile
	]);
	/**
	* @title: 获取协议方法需要在组件中定义才能传递给子组件，子组件无法获取到context中的方法
	* @description:
	* @param {string} id
	* @return {*}
	* @Author: WangHan
	* @Date: 2025-07-07 10:06
	*/
	const depositProtocolRequest = (id) => {
		var _appointmentBooking$g;
		return appointmentBooking === null || appointmentBooking === void 0 || (_appointmentBooking$g = appointmentBooking.getProtocol) === null || _appointmentBooking$g === void 0 ? void 0 : _appointmentBooking$g.call(appointmentBooking, id);
	};
	return /* @__PURE__ */ React.createElement(React.Fragment, null, !isRetailTemplate ? /* @__PURE__ */ React.createElement(Header, null) : null, /* @__PURE__ */ React.createElement("div", { className: `parallel-content-wrap ${isMobile ? "parallel-content-mobile-wrap" : ""} ${isRetailTemplate ? "parallel-content-retail-wrap" : ""}` }, /* @__PURE__ */ React.createElement("div", { className: "parallel-content-left" }, Com), isShowCartCom), isFooter ? /* @__PURE__ */ React.createElement(Footer, { onNext: () => {
		var _stepRef$current2, _stepRef$current2$nex;
		return stepRef === null || stepRef === void 0 || (_stepRef$current2 = stepRef.current) === null || _stepRef$current2 === void 0 || (_stepRef$current2$nex = _stepRef$current2.next) === null || _stepRef$current2$nex === void 0 ? void 0 : _stepRef$current2$nex.call(_stepRef$current2);
	} }) : null, (policyParams === null || policyParams === void 0 ? void 0 : policyParams.open) ? /* @__PURE__ */ React.createElement(DepositModal, {
		key: policyParams === null || policyParams === void 0 ? void 0 : policyParams._ts,
		open: policyParams === null || policyParams === void 0 ? void 0 : policyParams.open,
		onCancel: () => closePolicy(false),
		onConfirm: () => closePolicy(true),
		item: policyParams === null || policyParams === void 0 ? void 0 : policyParams.content,
		onChange: () => setLoading(false),
		isHideCheckout: false,
		depositProtocolRequest
	}) : null);
};
//#endregion
export { Layout as default };
