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 { isBookingProduct, isNormalProductByDurationSchedule } from "../../../components/booking/info/service2/utils.js";
import { useSalesSdkUIHosts } from "../context/SalesSdkUIHostsContext.js";
import { requestOpenNormalProductDetail } from "../normalProductDetail/normalProductDetailHostController.js";
import { applyProductDetailEditInDrawer, mergeNormalProductEditInDrawer } from "./applyProductDetailEditInDrawer.js";
import { buildEditProductDetailPayload } from "./buildEditProductDetailPayload.js";
import { canShowBookingEditProductButton } from "./canShowBookingEditProductButton.js";
import { useMemo, useState } from "react";
import { useMemoizedFn } from "ahooks";
//#region src/plus/salesSdk/bookingEditService/useBookingEditProduct.ts
/**
* 是否走 SkuDetailModal（普通 duration 商品且非预约 extension_type）。
*
* @example
* shouldOpenNormalProductDetail({ extension_type: 'appointment_ticket' }); // false
*/
function shouldOpenNormalProductDetail(cacheItem) {
	if (isBookingProduct(cacheItem === null || cacheItem === void 0 ? void 0 : cacheItem.extension_type)) return false;
	return isNormalProductByDurationSchedule(cacheItem);
}
/**
* 资源编辑抽屉内「编辑商品规格」交互（加车途中 + 已加车均可用）。
*
* @example
* const { canEditProduct, handleEditProduct, editingProduct } = useBookingEditProduct({
*   cacheItem,
*   bookingState,
*   form,
*   onCacheItemChange: setCacheItem,
*   onAfterProductEdit: getResources,
*   resetManualPriceChange: () => { isChangeTotal.current = false; },
* });
*/
function useBookingEditProduct(options) {
	const { cacheItem, bookingState, form, onCacheItemChange, onAfterProductEdit, resetManualPriceChange } = options;
	const uiHosts = useSalesSdkUIHosts();
	const [editingProduct, setEditingProduct] = useState(false);
	const canEditProduct = useMemo(() => canShowBookingEditProductButton({
		cacheItem,
		bookingState,
		hasOpenProductDetail: Boolean(uiHosts === null || uiHosts === void 0 ? void 0 : uiHosts.openProductDetail)
	}), [
		cacheItem,
		bookingState,
		uiHosts === null || uiHosts === void 0 ? void 0 : uiHosts.openProductDetail
	]);
	const applyEditResult = useMemoizedFn((result) => {
		onCacheItemChange(result.cacheItem);
		form.setFieldsValue(result.formPatch);
		resetManualPriceChange();
		onAfterProductEdit(result.cacheItem);
	});
	return {
		canEditProduct,
		handleEditProduct: useMemoizedFn(_asyncToGenerator(function* () {
			if (!cacheItem || !canEditProduct || editingProduct) return;
			const formValues = form.getFieldsValue();
			setEditingProduct(true);
			try {
				if (shouldOpenNormalProductDetail(cacheItem)) {
					const editedResult = yield requestOpenNormalProductDetail({
						mode: "edit",
						cacheItem: _objectSpread2(_objectSpread2({}, cacheItem), {}, { _extend: _objectSpread2(_objectSpread2({}, cacheItem._extend), formValues) }),
						skuDetailConfig: cacheItem === null || cacheItem === void 0 ? void 0 : cacheItem.skuDetailConfig
					});
					if (!editedResult || editedResult.mode !== "edit") return;
					applyEditResult(mergeNormalProductEditInDrawer({
						cacheItem,
						editedCacheItem: editedResult.cacheItem,
						formValues
					}));
					return;
				}
				const openDetail = uiHosts === null || uiHosts === void 0 ? void 0 : uiHosts.openProductDetail;
				if (!openDetail) {
					console.warn("[SalesSdk] openProductDetail 未配置，无法编辑商品规格");
					return;
				}
				const detailResult = yield openDetail(buildEditProductDetailPayload({
					cacheItem,
					bookingState,
					formValues
				}));
				if (!detailResult) return;
				applyEditResult(applyProductDetailEditInDrawer({
					cacheItem,
					formValues,
					detailResult
				}));
			} catch (err) {
				console.warn("[SalesSdk] handleEditProduct 失败", err);
			} finally {
				setEditingProduct(false);
			}
		})),
		editingProduct
	};
}
//#endregion
export { useBookingEditProduct };
