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("../../../components/booking/info/service2/utils.js");
const require_SalesSdkUIHostsContext = require("../context/SalesSdkUIHostsContext.js");
const require_normalProductDetailHostController = require("../normalProductDetail/normalProductDetailHostController.js");
const require_applyProductDetailEditInDrawer = require("./applyProductDetailEditInDrawer.js");
const require_buildEditProductDetailPayload = require("./buildEditProductDetailPayload.js");
const require_canShowBookingEditProductButton = require("./canShowBookingEditProductButton.js");
let react = require("react");
let ahooks = require("ahooks");
//#region src/plus/salesSdk/bookingEditService/useBookingEditProduct.ts
/**
* 是否走 SkuDetailModal（普通 duration 商品且非预约 extension_type）。
*
* @example
* shouldOpenNormalProductDetail({ extension_type: 'appointment_ticket' }); // false
*/
function shouldOpenNormalProductDetail(cacheItem) {
	if (require_utils.isBookingProduct(cacheItem === null || cacheItem === void 0 ? void 0 : cacheItem.extension_type)) return false;
	return require_utils.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 = require_SalesSdkUIHostsContext.useSalesSdkUIHosts();
	const [editingProduct, setEditingProduct] = (0, react.useState)(false);
	const canEditProduct = (0, react.useMemo)(() => require_canShowBookingEditProductButton.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 = (0, ahooks.useMemoizedFn)((result) => {
		onCacheItemChange(result.cacheItem);
		form.setFieldsValue(result.formPatch);
		resetManualPriceChange();
		onAfterProductEdit(result.cacheItem);
	});
	return {
		canEditProduct,
		handleEditProduct: (0, ahooks.useMemoizedFn)(require_asyncToGenerator._asyncToGenerator(function* () {
			if (!cacheItem || !canEditProduct || editingProduct) return;
			const formValues = form.getFieldsValue();
			setEditingProduct(true);
			try {
				if (shouldOpenNormalProductDetail(cacheItem)) {
					const editedResult = yield require_normalProductDetailHostController.requestOpenNormalProductDetail({
						mode: "edit",
						cacheItem: require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, cacheItem), {}, { _extend: require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, cacheItem._extend), formValues) }),
						skuDetailConfig: cacheItem === null || cacheItem === void 0 ? void 0 : cacheItem.skuDetailConfig
					});
					if (!editedResult || editedResult.mode !== "edit") return;
					applyEditResult(require_applyProductDetailEditInDrawer.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(require_buildEditProductDetailPayload.buildEditProductDetailPayload({
					cacheItem,
					bookingState,
					formValues
				}));
				if (!detailResult) return;
				applyEditResult(require_applyProductDetailEditInDrawer.applyProductDetailEditInDrawer({
					cacheItem,
					formValues,
					detailResult
				}));
			} catch (err) {
				console.warn("[SalesSdk] handleEditProduct 失败", err);
			} finally {
				setEditingProduct(false);
			}
		})),
		editingProduct
	};
}
//#endregion
exports.useBookingEditProduct = useBookingEditProduct;
