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/modules/BookingContext/utils/buildNormalProductCacheItemFromOrderLine.ts
var buildNormalProductCacheItemFromOrderLine_exports = {};
__export(buildNormalProductCacheItemFromOrderLine_exports, {
  buildNormalProductCacheItemFromOrderLine: () => buildNormalProductCacheItemFromOrderLine
});
module.exports = __toCommonJS(buildNormalProductCacheItemFromOrderLine_exports);
var import_utils = require("../../Order/utils");
var import_orderLineDisplay = require("./orderLineDisplay");
function resolveSourceProductPrice(product, sourceProduct) {
  var _a, _b, _c;
  const metadata = product.metadata || {};
  if (metadata.source_product_price !== void 0 && metadata.source_product_price !== null) {
    const parsed = Number(metadata.source_product_price);
    if (Number.isFinite(parsed))
      return parsed;
  }
  const variantId = Number(product.product_variant_id || 0);
  if (variantId > 0) {
    const variantList = ((_a = metadata.origin) == null ? void 0 : _a.variant) ?? (sourceProduct == null ? void 0 : sourceProduct.variant);
    if (Array.isArray(variantList)) {
      const matched = variantList.find((v) => Number(v == null ? void 0 : v.id) === variantId);
      const variantPrice = (matched == null ? void 0 : matched.price) ?? (matched == null ? void 0 : matched.base_price);
      if (variantPrice !== void 0 && variantPrice !== null) {
        const parsed = Number(variantPrice);
        if (Number.isFinite(parsed))
          return parsed;
      }
    }
  }
  const catalogPrice = (sourceProduct == null ? void 0 : sourceProduct.price) ?? (sourceProduct == null ? void 0 : sourceProduct.base_price);
  if (catalogPrice !== void 0 && catalogPrice !== null) {
    const parsed = Number(catalogPrice);
    if (Number.isFinite(parsed))
      return parsed;
  }
  const originPrice = ((_b = metadata.origin) == null ? void 0 : _b.price) ?? ((_c = metadata.origin) == null ? void 0 : _c.base_price);
  if (originPrice !== void 0 && originPrice !== null) {
    const parsed = Number(originPrice);
    if (Number.isFinite(parsed))
      return parsed;
  }
  return Number(product.selling_price ?? 0);
}
function mapOrderBundleToOtherBundle(bundle) {
  return bundle.map((item) => {
    const catalogOriginal = item.original_price ?? item.product_price ?? item.price ?? item.bundle_selling_price;
    const selling = item.bundle_selling_price ?? item.price ?? catalogOriginal;
    const { bundle_selling_price: _omitBundleSelling, ...rest } = item;
    const isMarkupOrMarkdown = (item.price_type === "markdown" || item.price_type === "markup") && (item.price_type_ext === "" || item.price_type_ext === void 0 || item.price_type_ext === null);
    if (isMarkupOrMarkdown)
      delete rest.original_total;
    return {
      ...rest,
      price: selling,
      original_price: catalogOriginal,
      ...isMarkupOrMarkdown ? {} : { original_total: item.original_total ?? catalogOriginal },
      num: item.num ?? item.quantity ?? 1,
      quantity: item.quantity ?? item.num ?? 1
    };
  });
}
function buildNormalProductCacheItemFromOrderLine(input) {
  var _a, _b, _c, _d;
  const { product, sourceProduct } = input;
  const resolvedId = product.product_id ?? (sourceProduct == null ? void 0 : sourceProduct.id);
  const sourcePrice = resolveSourceProductPrice(product, sourceProduct);
  const selling = Number(product.selling_price ?? 0);
  const original = Number(product.original_price ?? selling);
  const uid = (_a = product.metadata) == null ? void 0 : _a.unique_identification_number;
  const option = (0, import_utils.getProductSkuOptions)(product);
  const bundle = mapOrderBundleToOtherBundle(
    Array.isArray(product.product_bundle) ? product.product_bundle : []
  );
  const originExtend = (_c = (_b = product.metadata) == null ? void 0 : _b.origin) == null ? void 0 : _c._extend;
  const productOptionString = (0, import_orderLineDisplay.buildProductOptionStringFromOrderLine)(product);
  const discountList = product.discount_list || [];
  const other = {
    product_id: resolvedId,
    product_variant_id: product.product_variant_id ?? 0,
    option,
    bundle: ((_d = originExtend == null ? void 0 : originExtend.other) == null ? void 0 : _d.bundle) ? JSON.parse(JSON.stringify(originExtend.other.bundle)) : bundle,
    quantity: product.num ?? 1
  };
  const skuValue = {
    product_id: resolvedId,
    product_variant_id: product.product_variant_id ?? 0,
    option: other.option,
    bundle: other.bundle
  };
  const extend = {
    quantity: product.num ?? 1,
    product_name: (sourceProduct == null ? void 0 : sourceProduct.title) ?? "",
    note: product.note ?? "",
    // v2：_extend.price 为主商品 source 价；行 composite 用 total / origin_total 展示
    price: (originExtend == null ? void 0 : originExtend.price) ?? sourcePrice,
    total: (originExtend == null ? void 0 : originExtend.total) ?? selling,
    origin_total: (originExtend == null ? void 0 : originExtend.origin_total) ?? original,
    other,
    skuValue: JSON.parse(JSON.stringify(skuValue)),
    _skuValue: JSON.parse(JSON.stringify(skuValue)),
    isNormalProduct: true,
    ...productOptionString ? { product_option_string: productOptionString } : {},
    ...(originExtend == null ? void 0 : originExtend.priceOverride) !== void 0 ? { priceOverride: originExtend.priceOverride } : {},
    ...(originExtend == null ? void 0 : originExtend.bundle_edit) !== void 0 ? { bundle_edit: originExtend.bundle_edit } : {}
  };
  if ((originExtend == null ? void 0 : originExtend.skuValue) || (originExtend == null ? void 0 : originExtend._skuValue)) {
    extend.skuValue = JSON.parse(
      JSON.stringify(originExtend.skuValue || originExtend._skuValue)
    );
    extend._skuValue = JSON.parse(JSON.stringify(extend.skuValue));
  }
  return {
    ...sourceProduct || {},
    id: resolvedId,
    product_id: resolvedId,
    _id: uid,
    title: sourceProduct == null ? void 0 : sourceProduct.title,
    price: (sourceProduct == null ? void 0 : sourceProduct.price) ?? sourcePrice,
    selling_price: product.selling_price,
    original_price: product.original_price,
    ...productOptionString ? { product_option_string: productOptionString } : {},
    note: product.note,
    order_detail_id: product.order_detail_id ?? null,
    discount_list: discountList,
    new: 0,
    autoClose: true,
    _extend: extend
  };
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  buildNormalProductCacheItemFromOrderLine
});
