require("../../../../_virtual/_rolldown/runtime.js");
const require_objectSpread2 = require("../../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js");
const require_asyncToGenerator = require("../../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/asyncToGenerator.js");
const require_utils = require("../service2/utils.js");
const require_utils$1 = require("../../utils.js");
const require_useGetHolderList = require("./useGetHolderList.js");
const require_serve = require("../pet/serve.js");
let react = require("react");
//#region src/components/booking/info/hooks/useAddHolder.ts
const useFormDataConfig = (state) => {
	var _state$bookingConfig2;
	const formDataConfig = (0, react.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 require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2(require_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: (0, react.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] = (0, react.useState)(null);
	const getHolders = require_useGetHolderList.default(state, dispatch);
	const { formDataConfig, isMinorAndWaiver } = useFormDataConfig(state);
	(0, react.useEffect)(() => {
		if (isMinorAndWaiver) require_serve.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 ? require_objectSpread2._objectSpread2(require_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
		}) : require_objectSpread2._objectSpread2({}, formDataConfig) };
		const isWalkInBol = require_utils$1.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 = require_asyncToGenerator._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: require_utils.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
exports.default = useAddHolder;
exports.useFormDataConfig = useFormDataConfig;
