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/scanOrder/utils/toShoppingCartProducts.ts
var toShoppingCartProducts_exports = {};
__export(toShoppingCartProducts_exports, {
  toShoppingCartProducts: () => toShoppingCartProducts
});
module.exports = __toCommonJS(toShoppingCartProducts_exports);
function toNumber(value, fallback = 0) {
  const parsedValue = Number(value);
  if (Number.isNaN(parsedValue)) return fallback;
  return parsedValue;
}
function toSafeNum(value) {
  const parsedValue = Math.floor(Number(value));
  if (Number.isNaN(parsedValue) || parsedValue < 1) return 1;
  return parsedValue;
}
function getDisplayName(product) {
  var _a, _b, _c;
  const origin = (product == null ? void 0 : product._origin) || {};
  const callbackData = (origin == null ? void 0 : origin.callbackData) || {};
  const callbackOrigin = (callbackData == null ? void 0 : callbackData.origin) || {};
  const sourceItem = (origin == null ? void 0 : origin.sourceItem) || {};
  return (callbackData == null ? void 0 : callbackData.name) || (callbackData == null ? void 0 : callbackData.title) || (callbackOrigin == null ? void 0 : callbackOrigin.name) || (callbackOrigin == null ? void 0 : callbackOrigin.title) || (sourceItem == null ? void 0 : sourceItem.name) || (sourceItem == null ? void 0 : sourceItem.title) || (origin == null ? void 0 : origin.name) || (origin == null ? void 0 : origin.title) || ((_b = (_a = origin == null ? void 0 : origin.resources) == null ? void 0 : _a[0]) == null ? void 0 : _b.main_field) || ((_c = product == null ? void 0 : product.metadata) == null ? void 0 : _c.resource_name) || `Product #${product.product_id ?? "unknown"}`;
}
function getDisplayImage(product) {
  const origin = (product == null ? void 0 : product._origin) || {};
  const callbackData = (origin == null ? void 0 : origin.callbackData) || {};
  const callbackOrigin = (callbackData == null ? void 0 : callbackData.origin) || {};
  const sourceItem = (origin == null ? void 0 : origin.sourceItem) || {};
  return (callbackData == null ? void 0 : callbackData.image) || (callbackData == null ? void 0 : callbackData.cover) || (callbackOrigin == null ? void 0 : callbackOrigin.image) || (callbackOrigin == null ? void 0 : callbackOrigin.cover) || (sourceItem == null ? void 0 : sourceItem.image) || (sourceItem == null ? void 0 : sourceItem.cover) || (origin == null ? void 0 : origin.image) || (origin == null ? void 0 : origin.cover) || "";
}
function getUnitPrice(product) {
  var _a, _b;
  const callbackData = ((_a = product == null ? void 0 : product._origin) == null ? void 0 : _a.callbackData) || {};
  const callbackOrigin = (callbackData == null ? void 0 : callbackData.origin) || {};
  const sourceItem = ((_b = product == null ? void 0 : product._origin) == null ? void 0 : _b.sourceItem) || {};
  return toNumber(product.selling_price, NaN) || toNumber(product.original_price, NaN) || toNumber(callbackData == null ? void 0 : callbackData.price, NaN) || toNumber(callbackOrigin == null ? void 0 : callbackOrigin.price, NaN) || toNumber(sourceItem == null ? void 0 : sourceItem.price, 0);
}
function normalizeOptions(product) {
  var _a;
  const callbackData = ((_a = product == null ? void 0 : product._origin) == null ? void 0 : _a.callbackData) || {};
  const sourceOptions = Array.isArray(callbackData == null ? void 0 : callbackData.option) ? callbackData.option : (product == null ? void 0 : product.product_option_item) || [];
  return sourceOptions.map((optionItem) => {
    const optionPrice = toNumber(optionItem == null ? void 0 : optionItem.price, 0);
    const optionNum = toSafeNum(optionItem == null ? void 0 : optionItem.num);
    return {
      id: toNumber(
        (optionItem == null ? void 0 : optionItem.product_option_item_id) ?? (optionItem == null ? void 0 : optionItem.option_group_item_id)
      ),
      name: (optionItem == null ? void 0 : optionItem.name) || (optionItem == null ? void 0 : optionItem.title) || (optionItem == null ? void 0 : optionItem.label) || (optionItem == null ? void 0 : optionItem.option_group_item_name) || "",
      num: optionNum,
      price: optionPrice,
      total: optionPrice * optionNum,
      origin_total: optionPrice * optionNum
    };
  });
}
function getProductOriginRich(product) {
  var _a, _b, _c;
  return ((_a = product == null ? void 0 : product.metadata) == null ? void 0 : _a.origin) || ((_c = (_b = product == null ? void 0 : product._origin) == null ? void 0 : _b.callbackData) == null ? void 0 : _c.origin) || {};
}
function findVariantGroupById(groups, groupId) {
  return groups.find((g) => Number(g == null ? void 0 : g.id) === Number(groupId));
}
function findVariantItemDisplayName(variantGroup, variantItemId) {
  const items = Array.isArray(variantGroup == null ? void 0 : variantGroup.variant_item) ? variantGroup.variant_item : [];
  const found = items.find(
    (it) => Number(it == null ? void 0 : it.id) === Number(variantItemId)
  );
  const raw = (found == null ? void 0 : found.name) ?? (typeof (found == null ? void 0 : found.title) === "string" ? found.title : found == null ? void 0 : found.title) ?? "";
  return String(raw ?? "").trim();
}
function buildVariantStringFromSkuIds(origin, skuIdsRaw) {
  const groups = Array.isArray(origin == null ? void 0 : origin.variant_group) ? origin.variant_group : [];
  const parts = [];
  const segments = String(skuIdsRaw || "").split("_").map((s) => s.trim()).filter(Boolean);
  for (const seg of segments) {
    const colonIdx = seg.indexOf(":");
    if (colonIdx <= 0) continue;
    const groupId = Number(seg.slice(0, colonIdx));
    const itemId = Number(seg.slice(colonIdx + 1));
    if (Number.isNaN(groupId) || Number.isNaN(itemId)) continue;
    const vg = findVariantGroupById(groups, groupId);
    if (!vg) continue;
    const groupName = String((vg == null ? void 0 : vg.name) ?? "").trim();
    const itemName = findVariantItemDisplayName(vg, itemId);
    if (groupName && itemName) parts.push(`${groupName}: ${itemName}`);
    else if (itemName) parts.push(itemName);
    else if (groupName) parts.push(groupName);
  }
  return parts.join(", ");
}
function fallbackVariantLabelFromTitles(origin, variantRow) {
  const groups = Array.isArray(origin == null ? void 0 : origin.variant_group) ? origin.variant_group : [];
  const nameObj = (variantRow == null ? void 0 : variantRow.name) || (variantRow == null ? void 0 : variantRow.title);
  if (!nameObj || typeof nameObj !== "object") return "";
  const parts = [];
  for (const g of groups) {
    const pid = g == null ? void 0 : g.position_id;
    if (pid == null) continue;
    const val = nameObj[String(pid)] ?? nameObj[pid];
    if (val == null || val === "") continue;
    const groupName = String((g == null ? void 0 : g.name) ?? "").trim();
    const itemLabel = String(val).trim();
    if (groupName && itemLabel) parts.push(`${groupName}: ${itemLabel}`);
    else if (itemLabel) parts.push(itemLabel);
  }
  if (parts.length) return parts.join(", ");
  const vals = Object.values(nameObj).filter(
    (v) => v != null && String(v).trim() !== ""
  );
  return vals.map((v) => String(v).trim()).join(" / ");
}
function getVariantProductOptionString(product) {
  const variantId = product == null ? void 0 : product.product_variant_id;
  if (variantId == null || variantId === "" || Number(variantId) <= 0) return "";
  const origin = getProductOriginRich(product);
  const variants = Array.isArray(origin == null ? void 0 : origin.variant) ? origin.variant : [];
  const matched = variants.find((v) => Number(v == null ? void 0 : v.id) === Number(variantId));
  if (!matched) return "";
  const skuIds = matched == null ? void 0 : matched.sku_ids;
  if (skuIds != null && String(skuIds).trim() !== "") {
    const fromSku = buildVariantStringFromSkuIds(origin, String(skuIds));
    if (fromSku) return fromSku;
  }
  return fallbackVariantLabelFromTitles(origin, matched);
}
function normalizeBundles(product) {
  var _a;
  const callbackData = ((_a = product == null ? void 0 : product._origin) == null ? void 0 : _a.callbackData) || {};
  const sourceBundles = Array.isArray(callbackData == null ? void 0 : callbackData.bundle) ? callbackData.bundle : (product == null ? void 0 : product.product_bundle) || [];
  return sourceBundles.map((bundleItem, bundleIndex) => {
    const bundlePrice = toNumber(
      (bundleItem == null ? void 0 : bundleItem.price) ?? (bundleItem == null ? void 0 : bundleItem.product_price) ?? (bundleItem == null ? void 0 : bundleItem.custom_price),
      0
    );
    const bundleNum = toSafeNum(bundleItem == null ? void 0 : bundleItem.num);
    const bundleOptions = Array.isArray(bundleItem == null ? void 0 : bundleItem.option) ? bundleItem.option : [];
    return {
      id: toNumber((bundleItem == null ? void 0 : bundleItem.id) ?? (bundleItem == null ? void 0 : bundleItem.bundle_product_id), bundleIndex),
      name: (bundleItem == null ? void 0 : bundleItem.name) || (bundleItem == null ? void 0 : bundleItem.title) || "",
      num: bundleNum,
      price: bundlePrice,
      total: bundlePrice * bundleNum,
      origin_total: bundlePrice * bundleNum,
      options: bundleOptions.map((optionItem) => {
        const optionPrice = toNumber(optionItem == null ? void 0 : optionItem.price, 0);
        const optionNum = toSafeNum(optionItem == null ? void 0 : optionItem.num);
        return {
          id: toNumber(
            (optionItem == null ? void 0 : optionItem.product_option_item_id) ?? (optionItem == null ? void 0 : optionItem.option_group_item_id)
          ),
          name: (optionItem == null ? void 0 : optionItem.name) || (optionItem == null ? void 0 : optionItem.title) || (optionItem == null ? void 0 : optionItem.label) || (optionItem == null ? void 0 : optionItem.option_group_item_name) || "",
          num: optionNum,
          price: optionPrice,
          total: optionPrice * optionNum,
          origin_total: optionPrice * optionNum
        };
      })
    };
  });
}
function toShoppingCartProducts(params) {
  const { orderProducts, amountSymbol = "$" } = params;
  if (!Array.isArray(orderProducts)) return [];
  return orderProducts.map((productItem, index) => {
    var _a, _b, _c, _d;
    const productNum = toSafeNum(productItem == null ? void 0 : productItem.num);
    const unitPrice = getUnitPrice(productItem);
    const callbackData = ((_a = productItem == null ? void 0 : productItem._origin) == null ? void 0 : _a.callbackData) || {};
    const identityKey = typeof (productItem == null ? void 0 : productItem.identity_key) === "string" && productItem.identity_key.length > 0 ? productItem.identity_key : void 0;
    const rowKey = identityKey ?? `${productItem.product_id ?? "p"}_${productItem.product_variant_id}_${index}`;
    const fromCallbackOptionString = typeof (callbackData == null ? void 0 : callbackData.product_option_string) === "string" ? callbackData.product_option_string.trim() : "";
    const productOptionString = fromCallbackOptionString || getVariantProductOptionString(productItem) || void 0;
    const base = {
      _id: rowKey,
      rowKey,
      identity_key: identityKey,
      id: productItem.product_id,
      product_id: productItem.product_id,
      product_variant_id: productItem.product_variant_id,
      note: (productItem == null ? void 0 : productItem.note) != null ? String(productItem.note) : "",
      name: getDisplayName(productItem),
      image: getDisplayImage(productItem),
      num: productNum,
      price: unitPrice,
      total: unitPrice,
      origin_total: toNumber(productItem.original_price, unitPrice),
      symbol: amountSymbol,
      options: normalizeOptions(productItem),
      bundle: normalizeBundles(productItem),
      _origin: productItem == null ? void 0 : productItem._origin,
      isNormalProduct: true,
      ...productOptionString ? { product_option_string: productOptionString } : {}
    };
    if (Array.isArray(productItem.product_option_item) && productItem.product_option_item.length) {
      base.product_option_item = productItem.product_option_item;
    }
    if (Array.isArray(productItem.product_bundle) && productItem.product_bundle.length) {
      base.product_bundle = productItem.product_bundle;
    }
    if ((_b = productItem == null ? void 0 : productItem.metadata) == null ? void 0 : _b.venue_booking) {
      const origin = productItem._origin || {};
      const meta = productItem.metadata || {};
      const startDate = origin.start_date;
      const startTime = origin.start_time;
      const endDate = origin.end_date;
      const endTime = origin.end_time;
      base.start_date = startDate && startTime ? `${startDate} ${startTime}` : meta.start_time || "";
      base.end_date = endDate && endTime ? `${endDate} ${endTime}` : meta.end_time || "";
      base.start_time = startTime || "";
      base.end_time = endTime || "";
      base.duration_type = origin.sub_type || "minutes";
      base.resource_id = meta.resource_id;
      base.relation_form_name = meta.resource_name || ((_d = (_c = origin.resources) == null ? void 0 : _c[0]) == null ? void 0 : _d.main_field) || "";
      if (Array.isArray(origin.resources)) base.resources = origin.resources;
    }
    return base;
  });
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  toShoppingCartProducts
});
