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/solution/BookingTicket/utils/scan/applyGlobalScan.ts
var applyGlobalScan_exports = {};
__export(applyGlobalScan_exports, {
  applyGlobalScan: () => applyGlobalScan
});
module.exports = __toCommonJS(applyGlobalScan_exports);
var import_addProductDecision = require("../addProductDecision");
var import_cartView = require("../cartView");
var import_scanProductValues = require("./scanProductValues");
async function processScannedProductItem(host, item, code, bridge) {
  const ctx = host.buildAddProductCtx();
  if ((0, import_scanProductValues.isScanProductBundle)(item) || (0, import_scanProductValues.isScanProductOption)(item)) {
    return runRequiresDetailFlow(host, item, ctx, bridge);
  }
  if ((0, import_scanProductValues.countScanVariantMatches)(item, code) >= 2) {
    return runRequiresDetailFlow(host, item, ctx, bridge);
  }
  const values = (0, import_scanProductValues.genScanProductValues)(item, code);
  const itemWithSelection = { ...item, cartDetailValue: values };
  const decision = host.decideAddProduct(itemWithSelection, ctx);
  return runAddProductDecision(host, item, decision, ctx, bridge);
}
async function runRequiresDetailFlow(host, item, ctx, bridge) {
  if (!(bridge == null ? void 0 : bridge.openProductDetail)) {
    return "pending";
  }
  const payload = (0, import_addProductDecision.buildRequiresDetailPayload)(item, ctx);
  const detailResult = await bridge.openProductDetail(payload);
  if (!detailResult) {
    return "cancelled";
  }
  if (!bridge.confirmDetail) {
    return "pending";
  }
  await bridge.confirmDetail({
    item: payload.productData ?? payload.item ?? item,
    detailResult
  });
  return "added";
}
async function runAddProductDecision(host, catalogItem, decision, ctx, bridge) {
  if (decision.action === "reloadCatalog") {
    return "cancelled";
  }
  if (decision.action === "add") {
    const transformed = host.transformDetailToProductAndBooking({
      item: catalogItem,
      cacheItem: decision.cacheItem,
      options: ctx
    });
    await host.addProductToOrder(transformed.product, transformed.booking);
    return "added";
  }
  if (decision.action === "requiresDetail") {
    return runRequiresDetailFlow(host, catalogItem, ctx, bridge);
  }
  if (decision.action === "requiresBookingEdit") {
    if (!(bridge == null ? void 0 : bridge.openBookingEdit)) {
      return "pending";
    }
    const edited = await bridge.openBookingEdit(decision.payload);
    if (!edited) {
      return "cancelled";
    }
    if (!bridge.confirmBookingEdit) {
      return "pending";
    }
    await bridge.confirmBookingEdit({ cacheItem: edited });
    return "added";
  }
  return "pending";
}
async function applyGlobalScan(host, formatted, bridge) {
  var _a, _b, _c, _d, _e, _f, _g;
  const { searchType, data, scanCode } = formatted;
  if (searchType === "wallet") {
    if (!data) {
      (_a = bridge == null ? void 0 : bridge.onNotify) == null ? void 0 : _a.call(bridge, "fail", "pisell2.text.scan-global-failed");
      return { status: "failed", reason: "invalid_data" };
    }
    host.setOrderCustomer(data);
    (_b = bridge == null ? void 0 : bridge.onNotify) == null ? void 0 : _b.call(bridge, "success", "pisell2.text.add-client-success");
    return { status: "success", kind: "customer" };
  }
  if (searchType === "walletPass") {
    if (!data) {
      (_c = bridge == null ? void 0 : bridge.onNotify) == null ? void 0 : _c.call(bridge, "fail", "pisell2.text.scan-global-failed");
      return { status: "failed", reason: "invalid_data" };
    }
    const snapshot = host.getOrderCustomerSnapshot();
    if ((0, import_cartView.isWalkInCustomer)(snapshot)) {
      if (!(bridge == null ? void 0 : bridge.applyPromotionCode)) {
        return { status: "failed", reason: "no_bridge" };
      }
      const result = await bridge.applyPromotionCode(scanCode);
      if (result == null ? void 0 : result.isAvailable) {
        return { status: "success", kind: "promotion" };
      }
      return { status: "failed", reason: "invalid_data" };
    }
    host.setOrderCustomer(data);
    (_d = bridge == null ? void 0 : bridge.onNotify) == null ? void 0 : _d.call(bridge, "success", "pisell2.text.add-client-success");
    return { status: "success", kind: "customer" };
  }
  if (searchType === "product" || searchType === "local_product") {
    const list = data == null ? void 0 : data.list;
    if (!Array.isArray(list) || list.length === 0) {
      (_e = bridge == null ? void 0 : bridge.onNotify) == null ? void 0 : _e.call(bridge, "fail", "pisell2.text.scan-global-failed");
      return { status: "failed", reason: "invalid_data" };
    }
    let addedCount = 0;
    for (const item of list) {
      const outcome = await processScannedProductItem(host, item, scanCode, bridge);
      if (outcome === "added") {
        addedCount += 1;
      }
      if (outcome === "cancelled") {
        return { status: "cancelled" };
      }
      if (outcome === "pending") {
        return { status: "pending", kind: "requiresDetail" };
      }
    }
    if (addedCount > 0) {
      (_f = bridge == null ? void 0 : bridge.onNotify) == null ? void 0 : _f.call(bridge, "success");
      return { status: "success", kind: "products", meta: { count: addedCount } };
    }
    return { status: "failed", reason: "no_bridge" };
  }
  if (searchType === "order") {
    const orderId = (data == null ? void 0 : data.order_id) ?? (data == null ? void 0 : data.id);
    if (!orderId) {
      (_g = bridge == null ? void 0 : bridge.onNotify) == null ? void 0 : _g.call(bridge, "fail", "pisell2.text.scan-global-failed");
      return { status: "failed", reason: "invalid_data" };
    }
    if (!(bridge == null ? void 0 : bridge.openOrder)) {
      return { status: "pending", kind: "openOrder" };
    }
    bridge.openOrder({
      order_id: Number(orderId),
      business_code: (data == null ? void 0 : data.business_code) ?? host.getBusinessCode()
    });
    return { status: "pending", kind: "openOrder" };
  }
  return { status: "failed", reason: "invalid_data" };
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  applyGlobalScan
});
