import { _asyncToGenerator } from "../../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/asyncToGenerator.js";
import { contextUtils } from "../../hooks.js";
import { useScanOrderContext } from "../../context.js";
import Cart from "../Cart/index.js";
import FulfillmentMode from "../FulfillmentMode/index.js";
import ManualPickupRefModal from "../ManualPickupRefModal/index.js";
import React, { useCallback, useEffect, useMemo, useState } from "react";
import { Input, message } from "antd";
import { locales } from "@pisell/utils";
import "./index.less";
//#region src/components/scanOrder/components/Check/index.tsx
function Check() {
	const { viewModel, runModuleAction, refreshViewModel, tableName, entryMode, clearStepCache } = useScanOrderContext();
	const isAdditionalOrder = entryMode === "additional_order";
	const [submitting, setSubmitting] = useState(false);
	const [note, setNote] = useState((viewModel === null || viewModel === void 0 ? void 0 : viewModel.note) || "");
	const [pickupRefOpen, setPickupRefOpen] = useState(false);
	const utils = contextUtils();
	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();
	}, []);
	const isShowOrderNote = useMemo(() => {
		return dineInConfig === null || dineInConfig === void 0 ? void 0 : dineInConfig["sale.allow_order_notes"];
	}, [dineInConfig]);
	const noteTitle = useMemo(() => {
		return String(locales.getText("pisell2.text.order-notes") || locales.getText("pisell2.scan.order.note.title") || "备注");
	}, []);
	const notePlaceholder = useMemo(() => {
		return String(locales.getText("pisell2.text.add-order-notes") || locales.getText("pisell2.scan.order.note.placeholder") || "请输入备注");
	}, []);
	const currentTableText = useMemo(() => String(locales.getText("pisell2.scan.order.current.table") || "Current table"), []);
	const productsTitle = useMemo(() => String(locales.getText("pisell2.scan.order.products.title") || "Products"), []);
	const showCheckStepEmptyCartTip = () => {
		message.error(locales.getText("pisell2.scan.order.check.cart.empty.tip") || "Please go back to add products");
	};
	const doSubmit = function() {
		var _ref2 = _asyncToGenerator(function* () {
			if (((viewModel === null || viewModel === void 0 ? void 0 : viewModel.cartItems) || []).length === 0) {
				showCheckStepEmptyCartTip();
				setSubmitting(false);
				return;
			}
			try {
				setSubmitting(true);
				yield runModuleAction("updateTempOrderNote", note);
				const res = yield runModuleAction("submitScanOrder");
				console.log("submitScanOrder >>>>> ", res);
				clearStepCache();
				if ((dineInConfig === null || dineInConfig === void 0 ? void 0 : dineInConfig["sale.require_payment_on_submit"]) && !isAdditionalOrder) {
					var _res$data, _res$data2, _viewModel$summary;
					try {
						yield runModuleAction("restoreOrder");
					} catch (error) {
						console.warn("[scanOrder] restoreOrder after payment failed", error);
					}
					utils.action({
						type: "pisell1.payment",
						data: {
							order_id: res === null || res === void 0 || (_res$data = res.data) === null || _res$data === void 0 ? void 0 : _res$data.order_id,
							total_amount: (res === null || res === void 0 || (_res$data2 = res.data) === null || _res$data2 === void 0 ? void 0 : _res$data2.order_wait_pay_amount) || (viewModel === null || viewModel === void 0 || (_viewModel$summary = viewModel.summary) === null || _viewModel$summary === void 0 ? void 0 : _viewModel$summary.total_amount),
							sale_channel: "table-order"
						},
						callback: function() {
							var _ref = _asyncToGenerator(function* (_e) {});
							return function callback(_x) {
								return _ref.apply(this, arguments);
							};
						}()
					});
				} else {
					var _res$data3;
					if (res === null || res === void 0 || (_res$data3 = res.data) === null || _res$data3 === void 0 ? void 0 : _res$data3.order_id) {
						var _res$data4, _utils$getApp, _app$history, _app$history2;
						try {
							yield runModuleAction("restoreOrder");
						} catch (error) {
							console.warn("[scanOrder] restoreOrder before redirect failed", error);
						}
						const link = `/order/details?id=${res === null || res === void 0 || (_res$data4 = res.data) === null || _res$data4 === void 0 ? void 0 : _res$data4.order_id}`;
						const app = utils === null || utils === void 0 || (_utils$getApp = utils.getApp) === null || _utils$getApp === void 0 ? void 0 : _utils$getApp.call(utils);
						if (app === null || app === void 0 || (_app$history = app.history) === null || _app$history === void 0 ? void 0 : _app$history.replace) app.history.replace(link);
						else if (app === null || app === void 0 || (_app$history2 = app.history) === null || _app$history2 === void 0 ? void 0 : _app$history2.push) app.history.push(link);
						else location.replace(link);
					}
				}
				yield refreshViewModel();
			} catch (error) {
				console.error("[scanOrder] submitScanOrder failed", error);
				message.error((error === null || error === void 0 ? void 0 : error.message) || "Submit order failed");
			} finally {
				setSubmitting(false);
			}
		});
		return function doSubmit() {
			return _ref2.apply(this, arguments);
		};
	}();
	const handleSubmit = function() {
		var _ref3 = _asyncToGenerator(function* () {
			if (submitting) return;
			if (((viewModel === null || viewModel === void 0 ? void 0 : viewModel.cartItems) || []).length === 0) {
				showCheckStepEmptyCartTip();
				return;
			}
			if (isAdditionalOrder) {
				yield doSubmit();
				return;
			}
			try {
				setSubmitting(true);
				const manualPickup = yield runModuleAction("checkManualPickupRef");
				if (manualPickup === null || manualPickup === void 0 ? void 0 : manualPickup.enabled) {
					setPickupRefOpen(true);
					setSubmitting(false);
					return;
				}
			} catch (error) {
				console.error("[scanOrder] checkManualPickupRef failed", error);
				message.error((error === null || error === void 0 ? void 0 : error.message) || "Submit order failed");
				setSubmitting(false);
				return;
			}
			yield doSubmit();
		});
		return function handleSubmit() {
			return _ref3.apply(this, arguments);
		};
	}();
	const handlePickupRefConfirm = function() {
		var _ref4 = _asyncToGenerator(function* (value) {
			try {
				setSubmitting(true);
				yield runModuleAction("setPickupRef", value);
			} catch (error) {
				console.error("[scanOrder] setPickupRef failed", error);
				message.error((error === null || error === void 0 ? void 0 : error.message) || "Submit order failed");
				setSubmitting(false);
				return;
			}
			setPickupRefOpen(false);
			yield doSubmit();
		});
		return function handlePickupRefConfirm(_x2) {
			return _ref4.apply(this, arguments);
		};
	}();
	const handlePickupRefCancel = () => {
		if (submitting) return;
		setPickupRefOpen(false);
	};
	const noteContent = /* @__PURE__ */ React.createElement("div", { className: "pisell-scan-order-check-note" }, /* @__PURE__ */ React.createElement("div", { className: "pisell-scan-order-check-note-title" }, noteTitle), /* @__PURE__ */ React.createElement(Input.TextArea, {
		value: note,
		placeholder: notePlaceholder,
		autoSize: { minRows: 4 },
		allowClear: true,
		onChange: (event) => {
			var _event$target;
			return setNote((event === null || event === void 0 || (_event$target = event.target) === null || _event$target === void 0 ? void 0 : _event$target.value) || "");
		}
	}));
	return /* @__PURE__ */ React.createElement("div", { className: "pisell-scan-order-check" }, tableName ? /* @__PURE__ */ React.createElement("div", { className: "pisell-scan-order-check-current-table" }, /* @__PURE__ */ React.createElement("span", { className: "pisell-scan-order-check-current-table-label" }, currentTableText), /* @__PURE__ */ React.createElement("span", { className: "pisell-scan-order-check-current-table-value" }, tableName)) : null, !isAdditionalOrder ? /* @__PURE__ */ React.createElement(FulfillmentMode, null) : null, /* @__PURE__ */ React.createElement("div", { className: "pisell-scan-order-check-products-title" }, productsTitle), /* @__PURE__ */ React.createElement(Cart, {
		embedded: true,
		open: true,
		onCancel: () => {},
		onNext: handleSubmit,
		items: (viewModel === null || viewModel === void 0 ? void 0 : viewModel.cartItems) || [],
		summary: (viewModel === null || viewModel === void 0 ? void 0 : viewModel.summary) || {},
		confirmLoading: submitting,
		extraContent: !isAdditionalOrder && isShowOrderNote ? noteContent : void 0
	}), /* @__PURE__ */ React.createElement(ManualPickupRefModal, {
		open: pickupRefOpen,
		confirmLoading: submitting,
		onCancel: handlePickupRefCancel,
		onConfirm: handlePickupRefConfirm
	}));
}
//#endregion
export { Check as default };
