require("../../../_virtual/_rolldown/runtime.js");
const require_objectSpread2 = require("../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js");
const require_utils = require("../../../components/booking/utils.js");
const require_index = require("../../../utils/index.js");
const require_serve = require("../../../components/booking/info/pet/serve.js");
const require_serve$1 = require("../../../components/ticketBooking/serve.js");
let react = require("react");
let ahooks = require("ahooks");
//#region src/plus/salesSdk/bookingEditService/useBookingEditPetState.ts
function buildHolderFormDataConfig(holder) {
	var _holder$form, _holder$form2;
	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: []
	});
}
function getMinorLinkedTableId(formDataConfig) {
	var _formDataConfig$form_, _detail$setting$linke, _detail$setting;
	if ((formDataConfig === null || formDataConfig === void 0 ? void 0 : formDataConfig.form_type) !== "minors") return null;
	const detail = formDataConfig === null || formDataConfig === void 0 || (_formDataConfig$form_ = formDataConfig.form_field) === null || _formDataConfig$form_ === void 0 ? void 0 : _formDataConfig$form_.find((item) => item.filed_type === "form_to_form" && item.status === "hide");
	return (_detail$setting$linke = detail === null || detail === void 0 || (_detail$setting = detail.setting) === null || _detail$setting === void 0 ? void 0 : _detail$setting.linkedTable) !== null && _detail$setting$linke !== void 0 ? _detail$setting$linke : null;
}
/**
* SalesSdk 编辑抽屉内 Holder / selectHolderModal 所需的 pet 局部状态（替代 miniRedux setPet）。
*/
function useBookingEditPetState(open, baseBookingState, setRequest) {
	var _baseBookingState$boo3, _baseBookingState$boo4, _baseBookingState$cli2;
	const [pet, setPet] = (0, react.useState)({});
	const apis = (0, react.useMemo)(() => ({ getPetsData: require_serve$1.getPetsData }), []);
	const dispatch = (0, ahooks.useMemoizedFn)((cmd) => {
		if (cmd.type === "setPet" && cmd.payload) setPet((prev) => require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, prev), cmd.payload));
	});
	(0, react.useEffect)(() => {
		var _baseBookingState$boo, _baseBookingState$boo2, _baseBookingState$cli, _holder$form3;
		if (!open) {
			setPet({});
			return;
		}
		const holder = (_baseBookingState$boo = baseBookingState.bookingConfig) === null || _baseBookingState$boo === void 0 ? void 0 : _baseBookingState$boo.holder;
		const formDataConfig = buildHolderFormDataConfig(holder);
		if (!formDataConfig) {
			setPet({});
			return;
		}
		setPet({ formData: [formDataConfig] });
		if (setRequest) {
			require_utils.request.setRequest(setRequest);
			require_index.request.setRequest(setRequest);
		}
		const appointmentSubject = ((_baseBookingState$boo2 = baseBookingState.bookingConfig) === null || _baseBookingState$boo2 === void 0 || (_baseBookingState$boo2 = _baseBookingState$boo2.config) === null || _baseBookingState$boo2 === void 0 || (_baseBookingState$boo2 = _baseBookingState$boo2.basic) === null || _baseBookingState$boo2 === void 0 ? void 0 : _baseBookingState$boo2.appointment_subject) || {};
		const customerId = (_baseBookingState$cli = baseBookingState.client) === null || _baseBookingState$cli === void 0 || (_baseBookingState$cli = _baseBookingState$cli.value) === null || _baseBookingState$cli === void 0 ? void 0 : _baseBookingState$cli.id;
		const isWalkInCustomer = require_utils.isWalkIn(customerId);
		if ((holder === null || holder === void 0 ? void 0 : holder.data_range) === "customer" && isWalkInCustomer) return;
		require_serve$1.getPetsData({
			form_id: appointmentSubject.form_id,
			source_shop_customer: holder === null || holder === void 0 || (_holder$form3 = holder.form) === null || _holder$form3 === void 0 ? void 0 : _holder$form3.is_franchisor,
			customer_id: (holder === null || holder === void 0 ? void 0 : holder.data_range) === "customer" ? customerId : void 0,
			skip: 1,
			num: 100
		}).then((data) => {
			setPet((prev) => require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, prev), {}, { holders: (data === null || data === void 0 ? void 0 : data.list) || [] }));
		}).catch((err) => {
			console.warn("[SalesSdk] load holders failed", err);
		});
		const linkedTableId = getMinorLinkedTableId(formDataConfig);
		if (linkedTableId) require_serve.getFormDetail(linkedTableId).then((res) => {
			setPet((prev) => require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, prev), {}, { waiverForm: res }));
		}).catch((err) => {
			console.warn("[SalesSdk] load waiver form failed", err);
		});
	}, [
		open,
		(_baseBookingState$boo3 = baseBookingState.bookingConfig) === null || _baseBookingState$boo3 === void 0 ? void 0 : _baseBookingState$boo3.holder,
		(_baseBookingState$boo4 = baseBookingState.bookingConfig) === null || _baseBookingState$boo4 === void 0 || (_baseBookingState$boo4 = _baseBookingState$boo4.config) === null || _baseBookingState$boo4 === void 0 || (_baseBookingState$boo4 = _baseBookingState$boo4.basic) === null || _baseBookingState$boo4 === void 0 ? void 0 : _baseBookingState$boo4.appointment_subject,
		(_baseBookingState$cli2 = baseBookingState.client) === null || _baseBookingState$cli2 === void 0 || (_baseBookingState$cli2 = _baseBookingState$cli2.value) === null || _baseBookingState$cli2 === void 0 ? void 0 : _baseBookingState$cli2.id,
		setRequest
	]);
	return {
		pet,
		dispatch,
		apis
	};
}
//#endregion
exports.useBookingEditPetState = useBookingEditPetState;
