var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// src/components/scanOrder/hooks.ts
var hooks_exports = {};
__export(hooks_exports, {
  SCAN_ORDER_STEPS: () => SCAN_ORDER_STEPS,
  contextUtils: () => contextUtils,
  createScanOrderViewModel: () => createScanOrderViewModel,
  normalizeEntryContext: () => normalizeEntryContext,
  safeRunModuleAction: () => safeRunModuleAction,
  useScanOrderLogin: () => useScanOrderLogin,
  useScanOrderModule: () => useScanOrderModule,
  useScanOrderViewModel: () => useScanOrderViewModel
});
module.exports = __toCommonJS(hooks_exports);
var import_react = require("react");
var import_useEngineContext = __toESM(require("../../hooks/useEngineContext"));
var import_toShoppingCartProducts = require("./utils/toShoppingCartProducts");
var SCAN_ORDER_STEPS = ["init", "product", "check", "error"];
var useScanOrderModule = () => {
  var _a;
  const pisellOS = (0, import_useEngineContext.usePisellOS)();
  return (_a = pisellOS == null ? void 0 : pisellOS.getModule) == null ? void 0 : _a.call(pisellOS, "scanOrder");
};
var contextUtils = () => {
  var _a;
  const context = (0, import_useEngineContext.default)();
  return ((_a = context == null ? void 0 : context.appHelper) == null ? void 0 : _a.utils) || {};
};
function normalizeEntryContext(props) {
  return {
    biz: (props == null ? void 0 : props.biz) || "",
    resource_id: props == null ? void 0 : props.resource_id,
    order_id: props == null ? void 0 : props.order_id
  };
}
function callModuleGetter(module2, methodName, defaultValue) {
  if (typeof (module2 == null ? void 0 : module2[methodName]) !== "function") return defaultValue;
  const result = module2[methodName]();
  if (result && typeof (result == null ? void 0 : result.then) === "function") return defaultValue;
  return result ?? defaultValue;
}
async function safeRunModuleAction(module2, actionName, ...args) {
  if (!module2) throw new Error("scanOrder module not found");
  if (typeof (module2 == null ? void 0 : module2[actionName]) !== "function") {
    throw new Error(`scanOrder action "${actionName}" not found`);
  }
  return module2[actionName](...args);
}
function createScanOrderViewModel(module2) {
  const menuItems = callModuleGetter(module2, "getMenu", []);
  const orderProducts = callModuleGetter(module2, "getOrderProducts", []);
  const summaryByGetter = callModuleGetter(module2, "getSummary", null);
  const tempOrder = callModuleGetter(module2, "getTempOrder", null);
  const amountSymbol = (tempOrder == null ? void 0 : tempOrder.currency_symbol) || "$";
  const cartItems = (0, import_toShoppingCartProducts.toShoppingCartProducts)({
    orderProducts: Array.isArray(orderProducts) ? orderProducts : [],
    amountSymbol
  });
  const summaryFromGetter = summaryByGetter && typeof summaryByGetter === "object" ? summaryByGetter : null;
  const summary = summaryFromGetter || (tempOrder == null ? 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 ? void 0 : tempOrder.note) || "")
  };
}
function useScanOrderViewModel(module2) {
  return (0, import_react.useMemo)(() => createScanOrderViewModel(module2), [module2]);
}
var useScanOrderLogin = () => {
  var _a, _b;
  const utils = contextUtils();
  const isLogin = !!((_b = (_a = utils == null ? void 0 : utils.token) == null ? void 0 : _a.get) == null ? void 0 : _b.call(_a));
  const openLoginModal = (params) => {
    const { callback } = params || {};
    utils.action({
      type: "pisell1.login",
      data: {
        notReload: true,
        type: "plugin"
      },
      callback
    });
  };
  return { isLogin, openLoginModal };
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  SCAN_ORDER_STEPS,
  contextUtils,
  createScanOrderViewModel,
  normalizeEntryContext,
  safeRunModuleAction,
  useScanOrderLogin,
  useScanOrderModule,
  useScanOrderViewModel
});
