import { _objectSpread2 } from "../../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js";
import useComponents from "../../../../hooks/useComponents.js";
import { currentAppointmentBoard, updateBookingFormStorage } from "../../utils.js";
import { useAppointmentBookingContext } from "../../context.js";
import { useAppointmentBookingAccount, useAppointmentBookingLogin, useAppointmentBookingResource, useAppointmentBookingSetting, useAppointmentBookingStep } from "../../hooks.js";
import { updateRelationForms } from "./utils.js";
import React, { forwardRef, useImperativeHandle, useMemo, useRef } from "react";
import "./index.less";
//#region src/components/appointmentBooking/components/Form/index.tsx
const Form = forwardRef((props, ref) => {
	const manualRef = useRef();
	const { appointmentBooking, setLoading, currentStepParams, carts } = useAppointmentBookingContext();
	const { getResourceList } = useAppointmentBookingResource();
	const { setOtherData, getOtherData, isFormSubject } = useAppointmentBookingSetting();
	const { nextStep } = useAppointmentBookingStep();
	const { isLogin } = useAppointmentBookingLogin();
	const { checkAccount } = useAppointmentBookingAccount();
	useImperativeHandle(ref, () => ({ next: () => {
		var _stepLists, _manualRef$current, _manualRef$current$su;
		(_stepLists = ((currentStepParams === null || currentStepParams === void 0 ? void 0 : currentStepParams.stepLists) || [])[(currentStepParams === null || currentStepParams === void 0 ? void 0 : currentStepParams.index) + 1]) === null || _stepLists === void 0 || _stepLists.key;
		setLoading(true);
		(_manualRef$current = manualRef.current) === null || _manualRef$current === void 0 || (_manualRef$current$su = _manualRef$current.submit) === null || _manualRef$current$su === void 0 || _manualRef$current$su.call(_manualRef$current);
	} }));
	const [FormCard] = useComponents({ components: ["FormCard"] });
	const key = "booking_form";
	const formStorage = getOtherData("booking_form") || {};
	const appointmentBoard = useMemo(() => {
		const { resources } = getResourceList();
		return currentAppointmentBoard(currentStepParams.key, resources, appointmentBooking);
	}, [currentStepParams.key]);
	const formId = useMemo(() => {
		return (appointmentBoard === null || appointmentBoard === void 0 ? void 0 : appointmentBoard.resource_form_id) || 0;
	}, [appointmentBoard === null || appointmentBoard === void 0 ? void 0 : appointmentBoard.resource_form_id]);
	/**
	* @title: 更新表单状态
	* @description:
	* @param {string} status
	* @return {*}
	* @Author: WangHan
	* @Date: 2025-04-26 18:39
	*/
	const updateFormStatus = (status) => {
		if (status === "error") setLoading(false);
	};
	/**
	* @title: 设置表单存储
	* @description:
	* @param {any} values
	* @return {*}
	* @Author: WangHan
	* @Date: 2025-04-28 11:32
	*/
	const setFormStorage = (values) => {
		setOtherData(key, _objectSpread2({}, updateBookingFormStorage(getOtherData(key), currentStepParams === null || currentStepParams === void 0 ? void 0 : currentStepParams.key, values)));
		appointmentBooking.updateCart({
			_id: appointmentBoard === null || appointmentBoard === void 0 ? void 0 : appointmentBoard.cart_id,
			relationForms: updateRelationForms(carts, appointmentBoard, values, currentStepParams === null || currentStepParams === void 0 ? void 0 : currentStepParams.key)
		});
	};
	/**
	* @title: 表单设置成功之后的提交
	* @description:
	* @param {string} status
	* @param {any} values
	* @return {*}
	* @Author: WangHan
	* @Date: 2025-04-28 11:34
	*/
	const handleFormFinish = (status, values) => {
		try {
			updateFormStatus(status);
			if (status === "success") {
				setFormStorage(values);
				nextStep();
			}
		} catch (error) {
			setLoading(false);
		}
	};
	return /* @__PURE__ */ React.createElement(React.Fragment, null, /* @__PURE__ */ React.createElement("div", {
		className: "appointment-form-wrap",
		key: currentStepParams === null || currentStepParams === void 0 ? void 0 : currentStepParams.key
	}, /* @__PURE__ */ React.createElement(FormCard, {
		id: formId,
		ref: manualRef,
		isGetLastData: false,
		setting: { isHidden: 1 },
		formWrapStyle: { padding: "16px" },
		isV2API: true,
		isShowSkeleton: true,
		onSubmit: (status, values) => {
			handleFormFinish(status, values);
		},
		onFinishFailed: (e) => {
			updateFormStatus("error");
		},
		onFormValueChange: (values) => {},
		defaultValues: (formStorage === null || formStorage === void 0 ? void 0 : formStorage[currentStepParams === null || currentStepParams === void 0 ? void 0 : currentStepParams.key]) || {},
		isClearData: false
	})));
});
//#endregion
export { Form as default };
