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/solution/BookingTicket/utils/scan/handleScan.ts
var handleScan_exports = {};
__export(handleScan_exports, {
  handleCustomerScan: () => handleCustomerScan,
  handleGlobalScan: () => handleGlobalScan,
  handleUniversalScan: () => handleUniversalScan
});
module.exports = __toCommonJS(handleScan_exports);
var import_cloudSearch = require("./cloudSearch");
var import_scanCache = __toESM(require("./scanCache"));
var promiseAny = async (promises) => {
  if (promises.length === 0) {
    throw new Error("No promises provided");
  }
  const errors = [];
  let completedCount = 0;
  return new Promise((resolve, reject) => {
    promises.forEach((promise, index) => {
      promise.then((result) => {
        resolve(result);
      }).catch((error) => {
        errors[index] = error;
        completedCount++;
        if (completedCount === promises.length) {
          const errorMessage = `All promises were rejected. Errors: ${errors.map((e) => e.message).join(", ")}`;
          const combinedError = new Error(errorMessage);
          combinedError.errors = errors;
          reject(combinedError);
        }
      });
    });
  });
};
var handleScanFn = (getRequestList, localSearch) => {
  return async (scanResult) => {
    const { value } = scanResult || {};
    if (localSearch) {
      try {
        const localResult = localSearch == null ? void 0 : localSearch(value);
        if ((localResult == null ? void 0 : localResult.length) > 0) {
          console.log("本地搜索到数据>>>>>>>", localResult);
          return {
            searchType: "local_product",
            response: { data: { list: localResult } }
          };
        } else {
          console.log("本地搜索无数据>>>>>>>");
        }
      } catch (error) {
        console.error("本地搜索到数据失败>>>>>>>", error);
      }
    }
    try {
      if (import_scanCache.default.has(value)) {
        const result = import_scanCache.default.get(value);
        console.log("缓存中搜索到数据>>>>>>>", result);
        return result;
      } else {
        console.log("缓存中无数据>>>>>>>");
      }
    } catch (error) {
      console.error("缓存中搜索数据失败>>>>>>>", error);
    }
    const requestList = (getRequestList == null ? void 0 : getRequestList(value)) || [];
    if (requestList.length === 0) {
      throw new Error("requestList is empty");
    }
    try {
      const result = await promiseAny(requestList);
      import_scanCache.default.set(value, result);
      return result;
    } catch (error) {
      throw error;
    }
  };
};
var handleGlobalScan = (request, localSearch) => {
  const getRequestList = (value) => {
    let requestList = [];
    if (value.startsWith("WL")) {
      requestList = [(0, import_cloudSearch.searchWallet)(request, value)];
    } else {
      requestList = [
        (0, import_cloudSearch.searchOrder)(request, value),
        (0, import_cloudSearch.searchWalletPass)(request, value),
        (0, import_cloudSearch.searchWallet)(request, value),
        (0, import_cloudSearch.searchProduct)(request, value)
      ];
    }
    return requestList;
  };
  return handleScanFn(getRequestList, localSearch);
};
var handleCustomerScan = () => {
  return () => Promise.resolve({});
};
var handleUniversalScan = () => {
  return () => Promise.resolve({});
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  handleCustomerScan,
  handleGlobalScan,
  handleUniversalScan
});
