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 { handleChangeHolder } from "../service2/utils.js";
import { isWalkIn } from "../../utils.js";
import useGetHolderList from "./useGetHolderList.js";
import { getFormDetail } from "../pet/serve.js";
import { useEffect, useMemo, useState } from "react";
//#region src/components/booking/info/hooks/useAddHolder.ts
const useFormDataConfig = (state) => {
	var _state$bookingConfig2;
	const formDataConfig = useMemo(() => {
		var _state$bookingConfig, _holder$form, _holder$form2;
		let _holder = state === null || state === void 0 || (_state$bookingConfig = state.bookingConfig) === null || _state$bookingConfig === void 0 ? void 0 : _state$bookingConfig.holder;
		if (!_holder) return null;
		return _objectSpread2(_objectSpread2(_objectSpread2({}, _holder), _holder === null || _holder === void 0 ? void 0 : _holder.form), {}, {
			options: [],
			form_id: _holder === null || _holder === void 0 || (_holder$form = _holder.form) === null || _holder$form === void 0 ? void 0 : _holder$form.id,
			field: _holder === null || _holder === void 0 || (_holder$form2 = _holder.form) === null || _holder$form2 === void 0 ? void 0 : _holder$form2.form_field,
			relation_type: "appointment_booking",
			form_record: []
		});
	}, [state === null || state === void 0 || (_state$bookingConfig2 = state.bookingConfig) === null || _state$bookingConfig2 === void 0 ? void 0 : _state$bookingConfig2.holder]);
	return {
		formDataConfig,
		isMinorAndWaiver: useMemo(() => {
			if ((formDataConfig === null || formDataConfig === void 0 ? void 0 : formDataConfig.form_type) === "minors") {
				const detail = formDataConfig === null || formDataConfig === void 0 ? void 0 : formDataConfig.form_field.find((item) => item.filed_type === "form_to_form" && item.status === "hide");
				if (detail) {
					var _detail$setting;
					return detail === null || detail === void 0 || (_detail$setting = detail.setting) === null || _detail$setting === void 0 ? void 0 : _detail$setting.linkedTable;
				}
				return false;
			}
			return false;
		}, [formDataConfig])
	};
};
const useAddHolder = (state, dispatch) => {
	const [waiverForm, setWaiverForm] = useState(null);
	const getHolders = useGetHolderList(state, dispatch);
	const { formDataConfig, isMinorAndWaiver } = useFormDataConfig(state);
	useEffect(() => {
		if (isMinorAndWaiver) getFormDetail(isMinorAndWaiver).then((res) => {
			setWaiverForm(res);
			dispatch({
				type: "setPet",
				payload: { waiverForm: res }
			});
		});
	}, [isMinorAndWaiver]);
	const handleAddHolder = (products, currentItem) => {
		var _state$client, _state$client2;
		let _data = { formDataConfig: isMinorAndWaiver ? _objectSpread2(_objectSpread2({}, waiverForm), {}, {
			customer_id: (_state$client = state.client) === null || _state$client === void 0 || (_state$client = _state$client.value) === null || _state$client === void 0 ? void 0 : _state$client.id,
			is_franchisor: formDataConfig.is_franchisor
		}) : _objectSpread2({}, formDataConfig) };
		const isWalkInBol = isWalkIn((_state$client2 = state.client) === null || _state$client2 === void 0 || (_state$client2 = _state$client2.value) === null || _state$client2 === void 0 ? void 0 : _state$client2.id);
		if (formDataConfig.data_range === "customer" && !isWalkInBol) {
			var _state$client3;
			_data.customerId = (_state$client3 = state.client) === null || _state$client3 === void 0 ? void 0 : _state$client3.value.id;
		}
		state.action({
			type: "pisell1.goCustomFormAddData",
			data: _data,
			callback: function() {
				var _ref = _asyncToGenerator(function* (res) {
					let obj = {};
					for (let item of res) obj[item.key] = item.value;
					if (isMinorAndWaiver) {
						var _state$pet, _find;
						const oldList = (state === null || state === void 0 || (_state$pet = state.pet) === null || _state$pet === void 0 ? void 0 : _state$pet.holders) || [];
						const formValue = ((_find = (obj["form_to_form_value"] || []).find((item) => item.form_id === formDataConfig.form_id)) === null || _find === void 0 ? void 0 : _find.form_data_value) || [];
						const selectId = Math.max(...formValue);
						const oldDetail = oldList.find((item) => item.form_record_id === selectId);
						yield getHolders();
						if (formValue.length && !oldDetail) dispatch({
							type: "setService",
							payload: { value: handleChangeHolder(products, currentItem, { id: selectId }) }
						});
					} else dispatch({
						type: "setPet",
						payload: { holders: !isWalkInBol ? [obj, ...state.pet.holders] : state.pet.holders }
					});
				});
				return function callback(_x) {
					return _ref.apply(this, arguments);
				};
			}()
		});
	};
	return {
		handleAddHolder,
		waiverForm
	};
};
//#endregion
export { useAddHolder as default, useFormDataConfig };
