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/components/productCard/lineItem/index.tsx
var lineItem_exports = {};
__export(lineItem_exports, {
  PREFIX: () => PREFIX,
  convertProductToLineItemProduct: () => convertProductToLineItemProduct,
  default: () => lineItem_default
});
module.exports = __toCommonJS(lineItem_exports);
var import_react = __toESM(require("react"));
var import_utils = require("@pisell/utils");
var import_locales = __toESM(require("../cartSkuCard/locales"));
var import_locales2 = __toESM(require("./locales"));
var import_cartSkuCard = __toESM(require("../cartSkuCard"));
var import_useEngineContext = __toESM(require("../../../hooks/useEngineContext"));
var import_status = require("../status");
var import_BookingLineItem = __toESM(require("./BookingLineItem"));
var import_index = require("./index.less");
var PREFIX = "pisell-line-item";
var isBookingItem = (dataSource) => {
  var _a;
  return Boolean(
    ((_a = dataSource == null ? void 0 : dataSource._extend) == null ? void 0 : _a.product) || (dataSource == null ? void 0 : dataSource.booking_id) || (dataSource == null ? void 0 : dataSource.appointment_status) || (dataSource == null ? void 0 : dataSource.start_date)
  );
};
var translateText = (text, translationOriginal) => {
  if (typeof text === "string") return text;
  if (!text) return "";
  return translationOriginal(text);
};
var resolveLineOriginTotal = (product) => {
  var _a, _b, _c, _d;
  const candidates = [
    product == null ? void 0 : product.original_total,
    product == null ? void 0 : product.original_price,
    (_a = product == null ? void 0 : product._extend) == null ? void 0 : _a.origin_total,
    (_d = (_c = (_b = product == null ? void 0 : product.metadata) == null ? void 0 : _b.origin) == null ? void 0 : _c._extend) == null ? void 0 : _d.origin_total
  ];
  for (const candidate of candidates) {
    if (candidate !== void 0 && candidate !== null && candidate !== "") {
      return candidate;
    }
  }
  return void 0;
};
var convertBundleOption = (option, translationOriginal) => ({
  ...option,
  id: option.option_group_item_id,
  name: translateText(option.name, translationOriginal),
  price: option.add_price ?? option.price,
  num: option.num ?? option.quantity ?? 1
});
var convertBundle = (bundle, translationOriginal) => {
  const options = (bundle.option || []).map(
    (option) => convertBundleOption(option, translationOriginal)
  );
  return {
    ...bundle,
    title: bundle.title || bundle.bundle_title,
    id: bundle.bundle_id,
    product_id: bundle.bundle_product_id,
    _bundle_product_id: bundle.bundle_product_id,
    product_variant_id: bundle.bundle_variant_id,
    total: bundle.bundle_selling_price,
    origin_total: bundle.original_total,
    custom_price: bundle.custom_price,
    custom_price_type: bundle.custom_price_type,
    custom_price_type_ext: bundle.price_type_ext,
    options,
    option: options,
    _origin: bundle
  };
};
var resolveLineItemOptions = (product) => {
  var _a;
  const skuOptions = (_a = product == null ? void 0 : product.product_sku) == null ? void 0 : _a.option;
  if (Array.isArray(skuOptions) && skuOptions.length > 0) {
    return skuOptions;
  }
  if (Array.isArray(product == null ? void 0 : product.product_option_item)) {
    return product.product_option_item;
  }
  return [];
};
var mapLineItemOptions = (options, translationOriginal) => options.map((option) => ({
  ...option,
  name: translateText(option.name ?? option.title, translationOriginal),
  price: option.add_price ?? option.price,
  num: option.num ?? option.quantity ?? 1
}));
var convertProductToLineItemProduct = (product, translationOriginal, symbol) => {
  var _a, _b, _c, _d, _e, _f, _g, _h;
  const title = translateText(product.product_title || product.title, translationOriginal);
  const bundle = (product.product_bundle || []).map(
    (bundleItem) => convertBundle(bundleItem, translationOriginal)
  );
  const lineOriginTotal = resolveLineOriginTotal(product);
  const lineOptions = mapLineItemOptions(
    resolveLineItemOptions(product),
    translationOriginal
  );
  const mapped = {
    _id: ((_a = product.metadata) == null ? void 0 : _a.unique_identification_number) || product.order_detail_id || product.product_id,
    id: product.product_id,
    product_id: product.product_id,
    product_variant_id: product.product_variant_id,
    title,
    product_title: title,
    name: title,
    cover: product.cover,
    image: product.cover,
    num: product.num ?? product.product_quantity ?? 1,
    quantity: product.num ?? product.product_quantity ?? 1,
    price: (_b = product.metadata) == null ? void 0 : _b.source_product_price,
    total: product.selling_price,
    origin_total: lineOriginTotal,
    original_total: lineOriginTotal,
    source_product_price: (_c = product.metadata) == null ? void 0 : _c.source_product_price,
    selling_price: product.selling_price,
    payment_price: product.payment_price,
    tax_fee: product.tax_fee,
    symbol,
    note: product.note,
    product_sku: product.product_sku,
    product_bundle: bundle,
    bundle,
    product_option_string: product.product_option_string,
    options: lineOptions,
    discount_list: product.discount_list,
    promotions: product.promotions,
    discount_reason: ((_f = (_e = (_d = product.discount_list) == null ? void 0 : _d.find((d) => (d == null ? void 0 : d.type) === "product")) == null ? void 0 : _e.discount) == null ? void 0 : _f.message) ?? ((_g = product.metadata) == null ? void 0 : _g.product_discount_reason),
    // 促销赠品走 metadata._giftInfo；礼品卡走 gift_card（对齐 legacy formatProducts isGift）
    isGift: Boolean(
      product.gift_card || product.isGift || product._isGiftPromotion || ((_h = product.metadata) == null ? void 0 : _h._giftInfo) || product._giftInfo
    ),
    giftData: product.giftData,
    _origin: product,
    _extend: {
      other: {
        product_id: product.product_id,
        option: lineOptions,
        bundle: product.product_bundle,
        product_variant_id: product.product_variant_id,
        quantity: product.num ?? product.product_quantity ?? 1
      },
      note: product.note,
      total: product.selling_price,
      origin_total: lineOriginTotal,
      payment_price: product.payment_price
    }
  };
  return mapped;
};
var LineItem = (props) => {
  var _a, _b, _c, _d, _e, _f, _g, _h, _i;
  const {
    dataSource = import_status.defaultValue.dataSource,
    itemType,
    type = "a9",
    isShowBookingHeader,
    rightActions = [],
    escapeDom,
    isShowImage = import_status.defaultValue.isShowImage,
    isShowDelete = import_status.defaultValue.isShowDelete,
    isShowNote = import_status.defaultValue.isShowNote,
    isShowAmountFooter = import_status.defaultValue.isShowAmountFooter,
    disabledClick = import_status.defaultValue.disabledClick,
    disabledEdit = import_status.defaultValue.disabledEdit,
    statusOptions = [],
    statusLoading = false,
    statusDisabled = false,
    isShowRelatedProduct = true,
    holderOptions,
    onBookingStatusChange,
    onAddTimeProductCard,
    onAction,
    onDelete,
    onNote,
    onCard,
    onPromotion,
    onGift,
    ...other
  } = props;
  console.log("LineItem xxxxxxxxx", props);
  const context = (0, import_useEngineContext.default)();
  const translationOriginal = context.appHelper.utils.translationOriginal;
  const symbol = context.appHelper.utils.getSymbolic();
  const locale = ((_e = (_d = (_c = (_b = (_a = context.appHelper.utils) == null ? void 0 : _a.getApp) == null ? void 0 : _b.call(_a)) == null ? void 0 : _c.locales) == null ? void 0 : _d.getLocale) == null ? void 0 : _e.call(_d)) || ((_g = (_f = context.appHelper.utils) == null ? void 0 : _f.storage) == null ? void 0 : _g.get("umi_locale")) || "en-US";
  import_utils.locales.init(
    import_locales2.default,
    locale
  );
  import_utils.locales.init(
    import_locales.default,
    locale
  );
  const itemKind = itemType || (isBookingItem(dataSource) ? "booking" : "product");
  const productDataSource = (0, import_react.useMemo)(
    () => convertProductToLineItemProduct(dataSource, translationOriginal, symbol),
    [dataSource, symbol, translationOriginal]
  );
  const relatedProduct = (_h = dataSource == null ? void 0 : dataSource._extend) == null ? void 0 : _h.product;
  const relatedProductDataSource = (0, import_react.useMemo)(() => {
    if (!relatedProduct) return null;
    return convertProductToLineItemProduct(relatedProduct, translationOriginal, symbol);
  }, [relatedProduct, symbol, translationOriginal]);
  const addTimeProductDataSources = (0, import_react.useMemo)(() => {
    var _a2;
    const addTimeProducts = Array.isArray((_a2 = dataSource == null ? void 0 : dataSource._extend) == null ? void 0 : _a2.addTimeProduct) ? dataSource._extend.addTimeProduct : [];
    return addTimeProducts.map(
      (item) => convertProductToLineItemProduct(item, translationOriginal, symbol)
    );
  }, [(_i = dataSource == null ? void 0 : dataSource._extend) == null ? void 0 : _i.addTimeProduct, symbol, translationOriginal]);
  if (itemKind === "product") {
    return /* @__PURE__ */ import_react.default.createElement(
      import_cartSkuCard.default,
      {
        ...other,
        dataSource: productDataSource,
        type,
        rightActions,
        escapeDom,
        isShowImage,
        isShowDelete,
        isShowNote,
        isShowAmountFooter,
        disabledClick,
        disabledEdit,
        onAction,
        onDelete,
        onNote,
        onCard,
        onPromotion,
        onGift
      }
    );
  }
  return /* @__PURE__ */ import_react.default.createElement(
    import_BookingLineItem.default,
    {
      ...other,
      dataSource,
      type,
      cartSkuType: type,
      isShowBookingHeader,
      relatedProductDataSource,
      addTimeProductDataSources,
      rightActions,
      escapeDom,
      isShowImage,
      isShowDelete,
      isShowNote,
      isShowAmountFooter,
      disabledClick,
      disabledEdit,
      statusOptions,
      statusLoading,
      statusDisabled,
      isShowRelatedProduct,
      holderOptions,
      onBookingStatusChange,
      onAddTimeProductCard,
      onAction,
      onDelete,
      onNote,
      onCard,
      onPromotion,
      onGift
    }
  );
};
var lineItem_default = LineItem;
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  PREFIX,
  convertProductToLineItemProduct
});
