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/pisellReservation/components/bookingDetailModal/bookingProductToProductCard.ts
var bookingProductToProductCard_exports = {};
__export(bookingProductToProductCard_exports, {
  bookingProductLineToProductCardData: () => bookingProductLineToProductCardData
});
module.exports = __toCommonJS(bookingProductToProductCard_exports);
function str(v) {
  if (v == null) return "";
  return String(v).trim();
}
function pickLocalizedText(obj) {
  if (obj == null) return "";
  if (typeof obj === "string") return obj.trim();
  if (typeof obj !== "object") return "";
  const o = obj;
  const keys = [
    "auto",
    "zh-CN",
    "zh-HK",
    "en",
    "ja",
    "pt",
    "original",
    "kitchen"
  ];
  for (const k of keys) {
    const v = o[k];
    if (v != null && str(v)) return str(v);
  }
  for (const v of Object.values(o)) {
    if (v != null && typeof v === "string" && str(v)) return str(v);
    if (v != null && typeof v === "object" && v !== null && !Array.isArray(v)) {
      const inner = pickLocalizedText(v);
      if (inner) return inner;
    }
  }
  return "";
}
function parseMoneyNumber(v) {
  if (v == null || v === "") return 0;
  if (typeof v === "number" && Number.isFinite(v)) return v;
  const n = Number(String(v).replace(/,/g, "").trim());
  return Number.isFinite(n) ? n : 0;
}
function stableLineId(line, index) {
  const od = line.order_detail_id;
  if (od != null && String(od) !== "") return od;
  const bu = line.booking_uid;
  if (bu != null && String(bu) !== "") return String(bu);
  const pid = line.product_id;
  if (pid != null && String(pid) !== "") return `${String(pid)}-${index}`;
  return `line-${index}`;
}
function mapSkuOptionRowToOptionType(opt) {
  const idRaw = opt.option_group_item_id ?? opt.id;
  const idNum = typeof idRaw === "number" && Number.isFinite(idRaw) ? idRaw : Number(String(idRaw ?? 0).replace(/\D/g, "")) || 0;
  const namePart = pickLocalizedText(opt.name);
  const groupPart = pickLocalizedText(opt.group_name);
  const label = groupPart && namePart ? `${groupPart}：${namePart}` : namePart || groupPart || "—";
  const num = Math.max(1, Math.round(parseMoneyNumber(opt.num) || 1));
  const price = parseMoneyNumber(opt.add_price);
  return {
    id: idNum,
    name: label,
    num,
    price,
    total: price * num
  };
}
function variantRowsToSubtitle(variant) {
  if (!Array.isArray(variant)) return "";
  const parts = [];
  for (const row of variant) {
    if (!row || typeof row !== "object") continue;
    const r = row;
    const g = pickLocalizedText(r.group);
    const it = pickLocalizedText(r.item);
    if (g && it) parts.push(`${g}：${it}`);
    else if (it) parts.push(it);
    else if (g) parts.push(g);
  }
  return parts.join(" · ");
}
function buildProductOptionString(sku) {
  if (!sku) return void 0;
  const sub = pickLocalizedText(sku.subtitle);
  if (sub) return sub;
  const varStr = variantRowsToSubtitle(sku.variant);
  if (varStr) return varStr;
  const opts = sku.option;
  if (!Array.isArray(opts) || opts.length === 0) return void 0;
  const names = opts.filter((x) => x && typeof x === "object").map((x) => {
    const o = x;
    const gn = pickLocalizedText(o.group_name);
    const nn = pickLocalizedText(o.name);
    if (gn && nn) return `${gn}：${nn}`;
    return nn || gn;
  }).filter(Boolean);
  return names.length ? names.join(" · ") : void 0;
}
function mapProductSkuOptions(sku) {
  if (!sku) return void 0;
  const opts = sku.option;
  if (!Array.isArray(opts) || opts.length === 0) return void 0;
  const out = opts.filter((x) => x && typeof x === "object").map((x) => mapSkuOptionRowToOptionType(x));
  return out.length ? out : void 0;
}
function mapBundleRowToBundleType(b, bi) {
  const bundleId = b.bundle_id;
  const idNum = typeof bundleId === "number" && Number.isFinite(bundleId) ? bundleId : Number(String(bundleId ?? bi).replace(/\D/g, "")) || bi;
  const num = Math.max(1, Math.round(parseMoneyNumber(b.num) || 1));
  const unit = parseMoneyNumber(b.custom_price ?? b.price);
  const payLine = parseMoneyNumber(b.bundle_payment_price);
  const total = payLine > 0 ? payLine : unit * num;
  const optRaw = b.option;
  const nestedOpts = Array.isArray(optRaw) ? optRaw.filter((x) => x && typeof x === "object").map((x) => mapSkuOptionRowToOptionType(x)) : void 0;
  return {
    id: idNum,
    name: pickLocalizedText(b.bundle_title) || "—",
    num,
    price: num > 0 ? total / num : unit,
    total,
    origin_total: void 0,
    price_type: str(b.price_type) || void 0,
    options: nestedOpts && nestedOpts.length ? nestedOpts : void 0
  };
}
function mapProductBundles(line) {
  const raw = line.product_bundle;
  if (!Array.isArray(raw) || raw.length === 0) return void 0;
  const out = raw.filter((x) => x && typeof x === "object").map((x, i) => mapBundleRowToBundleType(x, i));
  return out.length ? out : void 0;
}
function resolveMainLineAmounts(line, qty) {
  const payLine = parseMoneyNumber(line.payment_price);
  const sellUnit = parseMoneyNumber(line.selling_price);
  const origUnit = parseMoneyNumber(line.original_price);
  const meta = line.metadata;
  const metaMainOrig = meta ? parseMoneyNumber(meta.main_product_original_price) : 0;
  let total = payLine > 0 ? payLine : sellUnit * qty;
  if (total <= 0 && origUnit > 0) total = origUnit * qty;
  if (total <= 0 && metaMainOrig > 0) total = metaMainOrig;
  let price = qty > 0 ? total / qty : 0;
  if (price <= 0 && sellUnit > 0) price = sellUnit;
  if (price <= 0 && origUnit > 0) price = origUnit;
  const originLine = origUnit > 0 ? origUnit * qty : metaMainOrig > 0 ? metaMainOrig : 0;
  const origin_total = originLine > 0 && Math.abs(originLine - total) > 1e-4 ? originLine : void 0;
  return { price, total, origin_total };
}
function resolveProductNumericId(line, index) {
  const pid = line.product_id;
  if (typeof pid === "number" && Number.isFinite(pid)) return pid;
  const n = Number(String(pid ?? "").replace(/\D/g, ""));
  return Number.isFinite(n) && n > 0 ? n : index;
}
function bookingProductLineToProductCardData(line, index) {
  const titleObj = line.product_title;
  const name = pickLocalizedText(titleObj) || str(line.name) || str(line.title) || "—";
  const sku = line.product_sku;
  const qty = Math.max(
    1,
    Math.round(parseMoneyNumber(line.product_quantity) || 1)
  );
  const { price, total, origin_total } = resolveMainLineAmounts(line, qty);
  const product_option_string = buildProductOptionString(sku);
  const options = mapProductSkuOptions(sku);
  const bundle = mapProductBundles(line);
  const noteRaw = line.note;
  const note = str(noteRaw) || void 0;
  const likeRaw = line.like_status;
  const like_status = likeRaw != null && str(likeRaw) ? str(likeRaw) : void 0;
  const resId = line.resource_id ?? line.relation_id;
  const resource_id = resId != null && String(resId) !== "" ? resId : void 0;
  const rfn = line.relation_form_name;
  const relation_form_name = rfn != null && str(rfn) ? str(rfn) : void 0;
  const imageRaw = line.image ?? line.product_image ?? (sku == null ? void 0 : sku.image);
  const image = imageRaw != null && str(imageRaw) ? str(imageRaw) : void 0;
  return {
    _id: stableLineId(line, index),
    id: resolveProductNumericId(line, index),
    name,
    price,
    total,
    num: qty,
    origin_total,
    image,
    isShowNote: Boolean(note),
    note,
    like_status,
    bundle,
    options,
    isShowAction: false,
    product_option_string,
    discount_reason: void 0,
    relation_products: void 0,
    resource_id,
    relation_form_name,
    holder_title: void 0,
    start_date: str(line.start_date) || void 0,
    end_date: str(line.end_date) || void 0,
    is_show_duration: void 0,
    is_show_week: void 0
  };
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  bookingProductLineToProductCardData
});
