const require_runtime = require("../../_virtual/_rolldown/runtime.js");
const require_useEngineContext = require("../../hooks/useEngineContext.js");
const require_utils = require("./utils.js");
const require_index = require("../../utils/tasks/index.js");
const require_hooks = require("./hooks.js");
let react = require("react");
react = require_runtime.__toESM(react);
//#region src/components/appointmentBooking/context.tsx
const AppointmentBookingContext = (0, react.createContext)(void 0);
const AppointmentBookingProvider = ({ children }) => {
	var _appHelper$utils, _appointmentBooking$s2, _currentStep$step2, _appointmentBooking$s3, _currentStep$step4;
	const policyIdsRef = (0, react.useRef)([]);
	const appointmentBooking = require_hooks.useAppointmentBooking();
	const [summary, setSummary] = (0, react.useState)({});
	const [times, setTimes] = (0, react.useState)([]);
	const [resources, setResources] = (0, react.useState)([]);
	const [currentStep, setCurrentStep] = (0, react.useState)();
	const [carts, setCarts] = (0, react.useState)([]);
	const [resourceOtherParams, setResourceOtherParams] = (0, react.useState)({});
	const [selectedTime, setSelectedTime] = (0, react.useState)(null);
	const [isAgreeDepositPolicy, setIsAgreeDepositPolicy] = (0, react.useState)(false);
	const [currentResource, setCurrentResource] = (0, react.useState)(null);
	const [loading, setLoading] = (0, react.useState)(false);
	const [isErrorHolder, setIsErrorHolder] = (0, react.useState)(false);
	const [tasksManager] = (0, react.useState)(() => new require_index.TasksManager());
	const [policyParams, setPolicyParams] = (0, react.useState)(null);
	const [currentSelectedResources, setCurrentSelectedResources] = (0, react.useState)();
	const { appHelper } = require_useEngineContext.default();
	const symbolic = appHelper === null || appHelper === void 0 || (_appHelper$utils = appHelper.utils) === null || _appHelper$utils === void 0 ? void 0 : _appHelper$utils.getSymbolic();
	(0, react.useEffect)(() => {
		setCurrentStep(appointmentBooking === null || appointmentBooking === void 0 ? void 0 : appointmentBooking.getCurrentStep());
	}, []);
	/**
	* @title: 设置协议数据
	* @description:
	* @param {any} ids
	* @return {*}
	* @Author: WangHan
	* @Date: 2025-05-12 21:55
	*/
	const setPolicyIds = (ids) => {
		policyIdsRef.current = ids;
	};
	/**
	* @title: 打开协议
	* @description:
	* @param {any} content
	* @param {any} callback
	* @return {*}
	* @Author: WangHan
	* @Date: 2025-05-12 17:56
	*/
	const openPolicy = (content, callback) => {
		setPolicyParams({
			open: true,
			content,
			callback,
			_ts: Date.now()
		});
	};
	/**
	* @title: 关闭协议
	* @description:
	* @return {*}
	* @Author: WangHan
	* @Date: 2025-05-12 17:57
	*/
	const closePolicy = (isCallback) => {
		setPolicyParams({
			open: false,
			content: {},
			callback: null
		});
		if ((policyParams === null || policyParams === void 0 ? void 0 : policyParams.callback) && isCallback) policyParams.callback();
	};
	const stepLists = (0, react.useMemo)(() => {
		var _appointmentBooking$s;
		return (appointmentBooking === null || appointmentBooking === void 0 || (_appointmentBooking$s = appointmentBooking.store) === null || _appointmentBooking$s === void 0 || (_appointmentBooking$s = _appointmentBooking$s.step) === null || _appointmentBooking$s === void 0 || (_appointmentBooking$s = _appointmentBooking$s.store) === null || _appointmentBooking$s === void 0 ? void 0 : _appointmentBooking$s.stepList) || [];
	}, [appointmentBooking === null || appointmentBooking === void 0 || (_appointmentBooking$s2 = appointmentBooking.store) === null || _appointmentBooking$s2 === void 0 || (_appointmentBooking$s2 = _appointmentBooking$s2.step) === null || _appointmentBooking$s2 === void 0 || (_appointmentBooking$s2 = _appointmentBooking$s2.store) === null || _appointmentBooking$s2 === void 0 ? void 0 : _appointmentBooking$s2.stepList]);
	/**
	* @title: 是否是最后一步
	* @description:
	* @param {*} useMemo
	* @return {*}
	* @Author: WangHan
	* @Date: 2025-05-10 15:18
	*/
	const isLastStep = (0, react.useMemo)(() => {
		var _currentStep$step;
		return (currentStep === null || currentStep === void 0 || (_currentStep$step = currentStep.step) === null || _currentStep$step === void 0 ? void 0 : _currentStep$step.key) === "confirm-information";
	}, [currentStep === null || currentStep === void 0 || (_currentStep$step2 = currentStep.step) === null || _currentStep$step2 === void 0 ? void 0 : _currentStep$step2.key, appointmentBooking === null || appointmentBooking === void 0 || (_appointmentBooking$s3 = appointmentBooking.store) === null || _appointmentBooking$s3 === void 0 || (_appointmentBooking$s3 = _appointmentBooking$s3.step) === null || _appointmentBooking$s3 === void 0 || (_appointmentBooking$s3 = _appointmentBooking$s3.store) === null || _appointmentBooking$s3 === void 0 ? void 0 : _appointmentBooking$s3.stepList]);
	const currentStepParams = (0, react.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;
		return {
			key,
			index: stepLists.findIndex((item) => item.key === key),
			stepLists
		};
	}, [currentStep === null || currentStep === void 0 || (_currentStep$step4 = currentStep.step) === null || _currentStep$step4 === void 0 ? void 0 : _currentStep$step4.key, stepLists]);
	(0, react.useEffect)(() => {
		var _currentStepParams$ke, _currentStepParams$ke2;
		if ((currentStepParams === null || currentStepParams === void 0 || (_currentStepParams$ke = currentStepParams.key) === null || _currentStepParams$ke === void 0 ? void 0 : _currentStepParams$ke.startsWith("select-resource")) || (currentStepParams === null || currentStepParams === void 0 || (_currentStepParams$ke2 = currentStepParams.key) === null || _currentStepParams$ke2 === void 0 ? void 0 : _currentStepParams$ke2.startsWith("fill-form"))) {
			if (!(carts === null || carts === void 0 ? void 0 : carts.length) || !(resources === null || resources === void 0 ? void 0 : resources.length)) return setCurrentSelectedResources(null);
			const currentSelectedResources = [];
			carts.forEach((item) => {
				if (appointmentBooking.isTargetCartIdNormalProduct(item === null || item === void 0 ? void 0 : item._id)) return;
				currentSelectedResources.push({
					_id: item === null || item === void 0 ? void 0 : item._id,
					currentSelectedResource: require_utils.getCartCurrentResourceData(item, currentStepParams === null || currentStepParams === void 0 ? void 0 : currentStepParams.key, resources)
				});
			});
			setCurrentSelectedResources(currentSelectedResources);
			appointmentBooking.setOtherData("current_selected_resources", currentSelectedResources);
		}
	}, [
		currentStepParams === null || currentStepParams === void 0 ? void 0 : currentStepParams.key,
		carts,
		resources
	]);
	(0, react.useEffect)(() => {
		setCurrentSelectedResources(appointmentBooking.getOtherData("current_selected_resources") || []);
	}, []);
	const value = {
		appointmentBooking,
		times,
		resources,
		summary,
		currentStep,
		carts,
		setTimes,
		setResources,
		setSummary,
		setCurrentStep,
		setCarts,
		appHelper,
		symbolic,
		resourceOtherParams,
		setResourceOtherParams,
		selectedTime,
		setSelectedTime,
		isAgreeDepositPolicy,
		setIsAgreeDepositPolicy,
		currentResource,
		setCurrentResource,
		loading,
		setLoading,
		isErrorHolder,
		setIsErrorHolder,
		isLastStep,
		tasksManager,
		policyParams,
		setPolicyParams,
		policyIdsRef,
		setPolicyIds,
		openPolicy,
		closePolicy,
		currentStepParams,
		currentSelectedResources,
		setCurrentSelectedResources
	};
	return /* @__PURE__ */ react.default.createElement(AppointmentBookingContext.Provider, { value }, children);
};
const useAppointmentBookingContext = () => {
	try {
		const context = (0, react.useContext)(AppointmentBookingContext);
		if (context === void 0) throw new Error("useAppointmentBookingContext must be used within a AppointmentBookingProvider");
		return context;
	} catch (error) {
		console.log(error, "useAppointmentBookingContext");
	}
};
//#endregion
exports.AppointmentBookingProvider = AppointmentBookingProvider;
exports.useAppointmentBookingContext = useAppointmentBookingContext;
