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/components/checkout/utils.ts
var utils_exports = {};
__export(utils_exports, {
  getShopWalletPassId: () => getShopWalletPassId,
  reorderPaymentMethodList: () => reorderPaymentMethodList
});
module.exports = __toCommonJS(utils_exports);
var getShopWalletPassId = async (interaction) => {
  var _a, _b;
  try {
    const shop_wallet_pass_id = await ((_b = (_a = interaction == null ? void 0 : interaction.utils) == null ? void 0 : _a.asyncDataManager) == null ? void 0 : _b.call(_a, {
      module: "get",
      key: "till_id"
    }));
    return shop_wallet_pass_id;
  } catch (error) {
    console.warn("Failed to get till_id from asyncDataManager:", error);
    return null;
  }
};
var convertPaymentMethodId = (id) => {
  if (id === void 0 || id === null) {
    return null;
  }
  const numericId = Number(id);
  return isNaN(numericId) ? null : numericId;
};
var isValidTerminalPaymentMethodList = (terminalList) => {
  return !!(terminalList && Array.isArray(terminalList) && terminalList.length > 0);
};
var reorderPaymentMethodList = async (paymentMethodList, interaction) => {
  var _a, _b;
  try {
    const terminalPaymentMethodList = await ((_b = (_a = interaction == null ? void 0 : interaction.utils) == null ? void 0 : _a.asyncDataManager) == null ? void 0 : _b.call(_a, {
      module: "get",
      key: "payment_method_list"
    }));
    console.log(terminalPaymentMethodList, "terminalPaymentMethodList");
    if (!isValidTerminalPaymentMethodList(terminalPaymentMethodList)) {
      console.info(
        "No terminal payment method list found, using original order"
      );
      return paymentMethodList;
    }
    const paymentMethodMap = /* @__PURE__ */ new Map();
    const invalidIdMethods = [];
    paymentMethodList.forEach((method) => {
      const numericId = convertPaymentMethodId(method == null ? void 0 : method.id);
      if (numericId !== null) {
        paymentMethodMap.set(numericId, method);
      } else {
        invalidIdMethods.push(method);
        console.warn("Payment method has invalid or missing id", {
          id: method == null ? void 0 : method.id,
          method
        });
      }
    });
    const orderedPaymentMethods = [];
    const usedPaymentMethodIds = /* @__PURE__ */ new Set();
    const hiddenPaymentMethodIds = /* @__PURE__ */ new Set();
    terminalPaymentMethodList.forEach((terminalMethod) => {
      if (terminalMethod.hidden === true) {
        hiddenPaymentMethodIds.add(Number(terminalMethod.id));
      }
    });
    terminalPaymentMethodList.filter((terminalMethod) => terminalMethod.hidden !== true).forEach((terminalMethod) => {
      const paymentMethod = paymentMethodMap.get(Number(terminalMethod.id));
      if (paymentMethod) {
        orderedPaymentMethods.push(paymentMethod);
        usedPaymentMethodIds.add(Number(terminalMethod.id));
      }
    });
    paymentMethodMap.forEach((method, numericId) => {
      if (!usedPaymentMethodIds.has(numericId) && !hiddenPaymentMethodIds.has(numericId)) {
        orderedPaymentMethods.push(method);
      }
    });
    orderedPaymentMethods.push(...invalidIdMethods);
    return orderedPaymentMethods;
  } catch (error) {
    console.error("Failed to reorder payment method list:", error);
    return paymentMethodList || [];
  }
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  getShopWalletPassId,
  reorderPaymentMethodList
});
