const require_asyncToGenerator = require("../../_virtual/_@oxc-project_runtime@0.122.0/helpers/asyncToGenerator.js");
const require_useEngineContext = require("../../hooks/useEngineContext.js");
const require_toShoppingCartProducts = require("./utils/toShoppingCartProducts.js");
require("react");
//#region src/components/scanOrder/hooks.ts
const SCAN_ORDER_STEPS = [
	"init",
	"product",
	"check",
	"error"
];
const useScanOrderModule = () => {
	var _pisellOS$getModule;
	const pisellOS = require_useEngineContext.usePisellOS();
	return pisellOS === null || pisellOS === void 0 || (_pisellOS$getModule = pisellOS.getModule) === null || _pisellOS$getModule === void 0 ? void 0 : _pisellOS$getModule.call(pisellOS, "scanOrder");
};
const contextUtils = () => {
	var _context$appHelper;
	const context = require_useEngineContext.default();
	return (context === null || context === void 0 || (_context$appHelper = context.appHelper) === null || _context$appHelper === void 0 ? void 0 : _context$appHelper.utils) || {};
};
function normalizeEntryContext(props) {
	return {
		biz: (props === null || props === void 0 ? void 0 : props.biz) || "",
		resource_id: props === null || props === void 0 ? void 0 : props.resource_id,
		order_id: props === null || props === void 0 ? void 0 : props.order_id
	};
}
function callModuleGetter(module, methodName, defaultValue) {
	if (typeof (module === null || module === void 0 ? void 0 : module[methodName]) !== "function") return defaultValue;
	const result = module[methodName]();
	if (result && typeof (result === null || result === void 0 ? void 0 : result.then) === "function") return defaultValue;
	return result !== null && result !== void 0 ? result : defaultValue;
}
function safeRunModuleAction(_x, _x2) {
	return _safeRunModuleAction.apply(this, arguments);
}
function _safeRunModuleAction() {
	_safeRunModuleAction = require_asyncToGenerator._asyncToGenerator(function* (module, actionName, ...args) {
		if (!module) throw new Error("scanOrder module not found");
		if (typeof (module === null || module === void 0 ? void 0 : module[actionName]) !== "function") throw new Error(`scanOrder action "${actionName}" not found`);
		return module[actionName](...args);
	});
	return _safeRunModuleAction.apply(this, arguments);
}
function createScanOrderViewModel(module) {
	const menuItems = callModuleGetter(module, "getMenu", []);
	const orderProducts = callModuleGetter(module, "getOrderProducts", []);
	const summaryByGetter = callModuleGetter(module, "getSummary", null);
	const tempOrder = callModuleGetter(module, "getTempOrder", null);
	const amountSymbol = (tempOrder === null || tempOrder === void 0 ? void 0 : tempOrder.currency_symbol) || "$";
	const cartItems = require_toShoppingCartProducts.toShoppingCartProducts({
		orderProducts: Array.isArray(orderProducts) ? orderProducts : [],
		amountSymbol
	});
	const summary = (summaryByGetter && typeof summaryByGetter === "object" ? summaryByGetter : null) || (tempOrder === null || tempOrder === void 0 ? void 0 : tempOrder.summary) || {};
	return {
		menuItems: Array.isArray(menuItems) ? menuItems : [],
		cartItems: Array.isArray(cartItems) ? cartItems : [],
		summary: summary && typeof summary === "object" ? summary : {},
		note: String((tempOrder === null || tempOrder === void 0 ? void 0 : tempOrder.note) || "")
	};
}
const useScanOrderLogin = () => {
	var _utils$token, _utils$token$get;
	const utils = contextUtils();
	const isLogin = !!(utils === null || utils === void 0 || (_utils$token = utils.token) === null || _utils$token === void 0 || (_utils$token$get = _utils$token.get) === null || _utils$token$get === void 0 ? void 0 : _utils$token$get.call(_utils$token));
	const openLoginModal = (params) => {
		const { callback } = params || {};
		utils.action({
			type: "pisell1.login",
			data: {
				notReload: true,
				type: "plugin"
			},
			callback
		});
	};
	return {
		isLogin,
		openLoginModal
	};
};
//#endregion
exports.SCAN_ORDER_STEPS = SCAN_ORDER_STEPS;
exports.contextUtils = contextUtils;
exports.createScanOrderViewModel = createScanOrderViewModel;
exports.normalizeEntryContext = normalizeEntryContext;
exports.safeRunModuleAction = safeRunModuleAction;
exports.useScanOrderLogin = useScanOrderLogin;
exports.useScanOrderModule = useScanOrderModule;
