import { _objectSpread2 } from "../../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js";
import { getProductExtend } from "../../info2/service/addService/utils.js";
import { isNormalProductByDurationSchedule } from "../service2/utils.js";
import { isOpenAddTime } from "../utils.js";
import useHolderConfig from "./useHolderConfig.js";
import useGetHolderList from "./useGetHolderList.js";
import { getOpenNormalServiceHandler } from "../../info2/service/openNormalService.js";
import { useEffect, useMemo, useRef } from "react";
import { useMemoizedFn } from "ahooks";
//#region src/components/booking/info/hooks/useInfoHolder.ts
const useInfoHolder = (state, dispatch) => {
	var _state$client, _state$bookingConfig2;
	const detailRef = useRef();
	const editNormalServiceRef = useRef(null);
	const { holder, isFormSubject, appointment_subject } = useHolderConfig(state);
	const getHolders = useGetHolderList(state, dispatch);
	useEffect(() => {
		if (isFormSubject) getHolders();
	}, [
		appointment_subject,
		holder,
		(_state$client = state.client) === null || _state$client === void 0 || (_state$client = _state$client.value) === null || _state$client === void 0 ? void 0 : _state$client.id
	]);
	const showAddTimeModal = useMemo(() => {
		return isOpenAddTime(state);
	}, [state, useMemo(() => {
		var _state$bookingConfig;
		return (state === null || state === void 0 || (_state$bookingConfig = state.bookingConfig) === null || _state$bookingConfig === void 0 || (_state$bookingConfig = _state$bookingConfig.config) === null || _state$bookingConfig === void 0 || (_state$bookingConfig = _state$bookingConfig.information_tab) === null || _state$bookingConfig === void 0 ? void 0 : _state$bookingConfig.appointment_card) || { quick_time: 1 };
	}, [state === null || state === void 0 || (_state$bookingConfig2 = state.bookingConfig) === null || _state$bookingConfig2 === void 0 || (_state$bookingConfig2 = _state$bookingConfig2.config) === null || _state$bookingConfig2 === void 0 ? void 0 : _state$bookingConfig2.information_tab]).quick_time]);
	const handleEdit = useMemoizedFn((cacheItem, type) => {
		if (isNormalProductByDurationSchedule(cacheItem)) {
			const openNormalService = getOpenNormalServiceHandler();
			if (openNormalService) openNormalService({
				mode: "edit",
				cacheItem,
				callback: () => {}
			});
		} else {
			var _detailRef$current, _detailRef$current$in;
			(_detailRef$current = detailRef.current) === null || _detailRef$current === void 0 || (_detailRef$current$in = _detailRef$current.init) === null || _detailRef$current$in === void 0 || _detailRef$current$in.call(_detailRef$current, cacheItem, false, type === "edit_product");
		}
	});
	const dispatchService = (type, payload) => {
		dispatch({
			type,
			payload: _objectSpread2({}, payload)
		});
	};
	return {
		detailRef,
		handleEdit,
		dispatchService,
		handleAction: useMemoizedFn((dataSource) => {
			dispatch({
				type: "setService",
				payload: {
					addTimeModal: true,
					cacheItem: getProductExtend({
						cacheItem: _objectSpread2(_objectSpread2({}, dataSource._origin), {}, { new: 0 }),
						state
					})
				}
			});
		}),
		showAddTimeModal,
		editNormalServiceRef
	};
};
//#endregion
export { useInfoHolder as default };
