import useEngineContext from "../../hooks/useEngineContext.js";
import { getCartCurrentResourceData } from "./utils.js";
import { TasksManager } from "../../utils/tasks/index.js";
import { useAppointmentBooking } from "./hooks.js";
import React, { createContext, useContext, useEffect, useMemo, useRef, useState } from "react";
//#region src/components/appointmentBooking/context.tsx
const AppointmentBookingContext = createContext(void 0);
const AppointmentBookingProvider = ({ children }) => {
	var _appHelper$utils, _appointmentBooking$s2, _currentStep$step2, _appointmentBooking$s3, _currentStep$step4;
	const policyIdsRef = useRef([]);
	const appointmentBooking = useAppointmentBooking();
	const [summary, setSummary] = useState({});
	const [times, setTimes] = useState([]);
	const [resources, setResources] = useState([]);
	const [currentStep, setCurrentStep] = useState();
	const [carts, setCarts] = useState([]);
	const [resourceOtherParams, setResourceOtherParams] = useState({});
	const [selectedTime, setSelectedTime] = useState(null);
	const [isAgreeDepositPolicy, setIsAgreeDepositPolicy] = useState(false);
	const [currentResource, setCurrentResource] = useState(null);
	const [loading, setLoading] = useState(false);
	const [isErrorHolder, setIsErrorHolder] = useState(false);
	const [tasksManager] = useState(() => new TasksManager());
	const [policyParams, setPolicyParams] = useState(null);
	const [currentSelectedResources, setCurrentSelectedResources] = useState();
	const { appHelper } = useEngineContext();
	const symbolic = appHelper === null || appHelper === void 0 || (_appHelper$utils = appHelper.utils) === null || _appHelper$utils === void 0 ? void 0 : _appHelper$utils.getSymbolic();
	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 = 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 = 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 = 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]);
	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: 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
	]);
	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.createElement(AppointmentBookingContext.Provider, { value }, children);
};
const useAppointmentBookingContext = () => {
	try {
		const context = 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
export { AppointmentBookingProvider, useAppointmentBookingContext };
