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 useEngineContext from "../../../../hooks/useEngineContext.js";
import index from "../../../../plus/productSelect/index.js";
import { useScanOrderContext } from "../../context.js";
import { transformProductDetailToOrderProduct } from "../../utils/transformProductDetail.js";
import React, { useCallback, useEffect, useRef, useState } from "react";
import { isMobile, locales } from "@pisell/utils";
import { PisellScrollView } from "@pisell/materials";
//#region src/components/scanOrder/components/Menu/index.tsx
function Menu(props) {
	var _scrollRef$current;
	const { onCheckout, productList, isProductListLoading, onEnsureProductList } = props;
	const { utils } = useEngineContext().appHelper || {};
	const { runModuleAction, refreshViewModel, viewModel } = useScanOrderContext();
	const [dineInConfig, setDineInConfig] = useState(null);
	const getConfig = useCallback(_asyncToGenerator(function* () {
		var _otherParams$dineInCo;
		const otherParams = yield runModuleAction("getOtherParams");
		console.log("otherParams >>>>> ", otherParams);
		setDineInConfig((_otherParams$dineInCo = otherParams === null || otherParams === void 0 ? void 0 : otherParams.dineInConfig) !== null && _otherParams$dineInCo !== void 0 ? _otherParams$dineInCo : {});
	}), [runModuleAction]);
	useEffect(() => {
		getConfig();
	}, []);
	useEffect(() => {
		onEnsureProductList();
	}, [onEnsureProductList]);
	const scrollRef = useRef(null);
	isMobile();
	const height = window.innerHeight - 100;
	const addOrderProduct = function() {
		var _ref = _asyncToGenerator(function* (params) {
			const { orderProduct, sourceData } = params;
			try {
				yield runModuleAction("addProductToOrder", orderProduct);
				yield refreshViewModel();
			} catch (error) {
				console.error("[scanOrder] addProductToOrder failed", {
					error,
					sourceData,
					mappedData: orderProduct
				});
			}
		});
		return function addOrderProduct(_x) {
			return _ref.apply(this, arguments);
		};
	}();
	const removeOrderProduct = function() {
		var _ref2 = _asyncToGenerator(function* (params) {
			const { product_id, product_variant_id, identity_key, sourceData } = params;
			try {
				yield runModuleAction("removeProductFromOrder", _objectSpread2({
					product_id,
					product_variant_id
				}, identity_key ? { identity_key } : {}));
				yield refreshViewModel();
			} catch (error) {
				console.error("[scanOrder] removeProductFromOrder failed", {
					error,
					sourceData,
					product_id,
					product_variant_id,
					identity_key
				});
			}
		});
		return function removeOrderProduct(_x2) {
			return _ref2.apply(this, arguments);
		};
	}();
	const updateOrderProduct = function() {
		var _ref3 = _asyncToGenerator(function* (params) {
			const { product_id, product_variant_id, identity_key, updates, sourceData } = params;
			try {
				yield runModuleAction("updateProductInOrder", _objectSpread2(_objectSpread2({
					product_id,
					product_variant_id
				}, identity_key ? { identity_key } : {}), {}, { updates }));
				yield refreshViewModel();
			} catch (error) {
				console.error("[scanOrder] updateProductInOrder failed", {
					error,
					sourceData,
					product_id,
					product_variant_id,
					identity_key,
					updates
				});
			}
		});
		return function updateOrderProduct(_x3) {
			return _ref3.apply(this, arguments);
		};
	}();
	const handleSelectProduct = function() {
		var _ref6 = _asyncToGenerator(function* (item, clickType, meta) {
			console.log(item, clickType, meta);
			const sourceItem = (meta === null || meta === void 0 ? void 0 : meta.product) || item;
			if ((meta === null || meta === void 0 ? void 0 : meta.intent) === "quantity") {
				var _item$option, _item$bundle, _item$price, _item$product_id, _ref4, _meta$quantity, _orderProduct$product, _orderProduct$product4;
				const quantityData = _objectSpread2(_objectSpread2({}, item), {}, {
					quantity: item === null || item === void 0 ? void 0 : item.num,
					option: (_item$option = item === null || item === void 0 ? void 0 : item.option) !== null && _item$option !== void 0 ? _item$option : item === null || item === void 0 ? void 0 : item.product_option_item,
					bundle: (_item$bundle = item === null || item === void 0 ? void 0 : item.bundle) !== null && _item$bundle !== void 0 ? _item$bundle : item === null || item === void 0 ? void 0 : item.product_bundle,
					origin: sourceItem,
					price: (_item$price = item === null || item === void 0 ? void 0 : item.price) !== null && _item$price !== void 0 ? _item$price : item === null || item === void 0 ? void 0 : item.selling_price
				});
				const orderProduct = transformProductDetailToOrderProduct({
					payload: quantityData,
					fallbackProductId: (_item$product_id = item === null || item === void 0 ? void 0 : item.product_id) !== null && _item$product_id !== void 0 ? _item$product_id : item === null || item === void 0 ? void 0 : item.id,
					sourceItem
				});
				if (!orderProduct) {
					console.warn("[scanOrder] quantity add missing product_id", quantityData);
					return;
				}
				const nextQuantity = Number((_ref4 = (_meta$quantity = meta === null || meta === void 0 ? void 0 : meta.quantity) !== null && _meta$quantity !== void 0 ? _meta$quantity : quantityData === null || quantityData === void 0 ? void 0 : quantityData.quantity) !== null && _ref4 !== void 0 ? _ref4 : 0);
				const productVariantId = Number((_orderProduct$product = orderProduct === null || orderProduct === void 0 ? void 0 : orderProduct.product_variant_id) !== null && _orderProduct$product !== void 0 ? _orderProduct$product : 0);
				if (Number.isNaN(productVariantId)) {
					console.warn("[scanOrder] quantity action missing product_variant_id", {
						quantityData,
						orderProduct
					});
					return;
				}
				const normalizedQuantity = Number.isNaN(nextQuantity) ? 0 : Math.max(0, Math.floor(nextQuantity));
				const existedCartItem = (Array.isArray(viewModel === null || viewModel === void 0 ? void 0 : viewModel.cartItems) ? viewModel.cartItems : []).find((cartItem) => {
					var _cartItem$product_id, _orderProduct$product2, _cartItem$product_var;
					return String((_cartItem$product_id = cartItem === null || cartItem === void 0 ? void 0 : cartItem.product_id) !== null && _cartItem$product_id !== void 0 ? _cartItem$product_id : null) === String((_orderProduct$product2 = orderProduct === null || orderProduct === void 0 ? void 0 : orderProduct.product_id) !== null && _orderProduct$product2 !== void 0 ? _orderProduct$product2 : null) && Number((_cartItem$product_var = cartItem === null || cartItem === void 0 ? void 0 : cartItem.product_variant_id) !== null && _cartItem$product_var !== void 0 ? _cartItem$product_var : 0) === productVariantId;
				});
				const existedIdentityKey = typeof (existedCartItem === null || existedCartItem === void 0 ? void 0 : existedCartItem.identity_key) === "string" && existedCartItem.identity_key.length > 0 ? existedCartItem.identity_key : void 0;
				if (normalizedQuantity <= 0) {
					var _orderProduct$product3;
					yield removeOrderProduct({
						product_id: (_orderProduct$product3 = orderProduct === null || orderProduct === void 0 ? void 0 : orderProduct.product_id) !== null && _orderProduct$product3 !== void 0 ? _orderProduct$product3 : null,
						product_variant_id: productVariantId,
						identity_key: existedIdentityKey,
						sourceData: quantityData
					});
					return;
				}
				if (!existedCartItem) {
					yield addOrderProduct({
						orderProduct: _objectSpread2(_objectSpread2({}, orderProduct), {}, { num: normalizedQuantity }),
						sourceData: quantityData
					});
					return;
				}
				yield updateOrderProduct({
					product_id: (_orderProduct$product4 = orderProduct === null || orderProduct === void 0 ? void 0 : orderProduct.product_id) !== null && _orderProduct$product4 !== void 0 ? _orderProduct$product4 : null,
					product_variant_id: productVariantId,
					identity_key: existedIdentityKey,
					updates: {
						num: normalizedQuantity,
						_origin: orderProduct === null || orderProduct === void 0 ? void 0 : orderProduct._origin
					},
					sourceData: quantityData
				});
			} else utils === null || utils === void 0 || utils.action({
				type: "pisell1.productDetail",
				data: {
					detail: _objectSpread2(_objectSpread2({}, sourceItem), {}, { open_deposit: 1 }),
					setting: {
						can_input: true,
						item_max_length: 1,
						is_detail_open: false
					}
				},
				callback: function() {
					var _ref5 = _asyncToGenerator(function* (data) {
						var _item$product_id2;
						if (data === null || data === void 0 ? void 0 : data.isCancel) return;
						const orderProduct = transformProductDetailToOrderProduct({
							payload: data,
							fallbackProductId: (_item$product_id2 = item === null || item === void 0 ? void 0 : item.product_id) !== null && _item$product_id2 !== void 0 ? _item$product_id2 : item === null || item === void 0 ? void 0 : item.id,
							sourceItem
						});
						if (!orderProduct) {
							console.warn("[scanOrder] productDetail callback missing product_id", data);
							return;
						}
						yield addOrderProduct({
							orderProduct,
							sourceData: data
						});
					});
					return function callback(_x4) {
						return _ref5.apply(this, arguments);
					};
				}()
			});
		});
		return function handleSelectProduct(_x5, _x6, _x7) {
			return _ref6.apply(this, arguments);
		};
	}();
	const productSelectNode = /* @__PURE__ */ React.createElement(index, {
		dataSource: productList,
		onChange: handleSelectProduct,
		loading: isProductListLoading,
		emptyDescription: locales.getText("pisell2.scan.order.empty.product"),
		token: {
			colorPrimary: "var(--theme-color)",
			containerBg: "#FFFFFF",
			tabItemBg: "#F2F4F7",
			tabItemHoverBg: "#F2F4F7",
			tabItemActiveBg: "var(--theme-color)",
			categoryTextColor: "#1B1B1B",
			cardSelectedBg: "var(--theme-color)"
		},
		customScrollParent: ((_scrollRef$current = scrollRef.current) === null || _scrollRef$current === void 0 ? void 0 : _scrollRef$current.getScrollContainer()) || "",
		hideCategoryWhenOne: true,
		version: "v2",
		value: (viewModel === null || viewModel === void 0 ? void 0 : viewModel.cartItems) || [],
		productListSetting: {
			imgWidth: 80,
			imgHeight: 80
		},
		cartElementId: "#scan-order-v2-cart-btn"
	});
	return /* @__PURE__ */ React.createElement("div", { className: "pisell-scan-menu" }, /* @__PURE__ */ React.createElement(PisellScrollView, {
		height,
		overflow: "y",
		ref: scrollRef
	}, productSelectNode));
}
//#endregion
export { Menu as default };
