var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
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 __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// src/plus/salesSdk/utils/createGlobalScanHostBridge.ts
var createGlobalScanHostBridge_exports = {};
__export(createGlobalScanHostBridge_exports, {
  SCAN_RESULT_EVENT: () => SCAN_RESULT_EVENT,
  createGlobalScanHostBridge: () => createGlobalScanHostBridge
});
module.exports = __toCommonJS(createGlobalScanHostBridge_exports);
var SCAN_RESULT_EVENT = "nativeScanResult";
function createGlobalScanHostBridge(deps) {
  const {
    uiHosts,
    action,
    cart,
    businessCode,
    refreshRootSnapshot,
    getCustomerId,
    toast,
    getLocaleText
  } = deps;
  const resolveText = (key, fallback) => {
    if (!key) return fallback || "";
    try {
      return (getLocaleText == null ? void 0 : getLocaleText(key)) || fallback || key;
    } catch {
      return fallback || key;
    }
  };
  const applyPromotionCode = async (code, customerId) => {
    var _a, _b;
    const trimmed = code.trim();
    if (!trimmed) {
      return { isAvailable: false, type: "client" };
    }
    if (window.isPaymentModalPluginOpen) {
      return { isAvailable: false, type: "client" };
    }
    const resolvedCustomerId = customerId ?? (getCustomerId == null ? void 0 : getCustomerId());
    try {
      const result = await cart.scanCode(trimmed, resolvedCustomerId);
      if (result == null ? void 0 : result.isAvailable) {
        refreshRootSnapshot();
        return result;
      }
      const unavailableText = {
        time_limit: resolveText("pisell2.text.goodpass.code-invalid-expired")
      };
      const message = (result == null ? void 0 : result.type) === "server" ? resolveText("pisell2.text.goodpass.code-invalid") : unavailableText[(result == null ? void 0 : result.unavailableReason) || ""] || resolveText("pisell2.text.goodpass.code-invalid-cart");
      (_a = toast == null ? void 0 : toast.fail) == null ? void 0 : _a.call(toast, message);
      return result;
    } catch (error) {
      console.error("[SalesSdk] applyPromotionCode failed", error);
      (_b = toast == null ? void 0 : toast.fail) == null ? void 0 : _b.call(toast, resolveText("pisell2.text.goodpass.code-invalid"));
      return { isAvailable: false, type: "server" };
    }
  };
  return {
    openProductDetail: uiHosts.openProductDetail ? (payload) => uiHosts.openProductDetail(payload) : void 0,
    openBookingEdit: uiHosts.openBookingEdit ? (payload) => uiHosts.openBookingEdit(payload) : void 0,
    confirmDetail: (input) => cart.confirmDetail(input),
    confirmBookingEdit: (input) => cart.confirmBookingEdit(input),
    openOrder: ({ order_id, business_code }) => {
      if (!action) {
        console.warn("[SalesSdk] openOrder: action 未配置");
        return;
      }
      if (window.isPaymentModalPluginOpen) {
        return;
      }
      action({
        type: "pisell1.handleOpenSaleDetail",
        data: {
          osKey: "big-sale-detail",
          osProps: { orderId: order_id }
        }
      });
    },
    applyPromotionCode,
    onNotify: (type, messageKey) => {
      var _a, _b, _c;
      const text = resolveText(messageKey);
      if (type === "fail") {
        (_a = toast == null ? void 0 : toast.fail) == null ? void 0 : _a.call(toast, text || resolveText("pisell2.text.scan-global-failed"));
        return;
      }
      if (type === "success") {
        if (messageKey) {
          (_b = toast == null ? void 0 : toast.success) == null ? void 0 : _b.call(toast, text);
        }
        return;
      }
      if (type === "info" && messageKey) {
        (_c = toast == null ? void 0 : toast.info) == null ? void 0 : _c.call(toast, text);
      }
    },
    refresh: refreshRootSnapshot
  };
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  SCAN_RESULT_EVENT,
  createGlobalScanHostBridge
});
