var _excluded = ["dataSource", "itemType", "type", "isShowBookingHeader", "rightActions", "escapeDom", "isShowImage", "isShowDelete", "isShowNote", "isShowAmountFooter", "disabledClick", "disabledEdit", "statusOptions", "statusLoading", "statusDisabled", "isShowRelatedProduct", "holderOptions", "onBookingStatusChange", "onAddTimeProductCard", "onAction", "onDelete", "onNote", "onCard", "onPromotion", "onGift"];
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
function _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); }
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
import React, { useMemo } from 'react';
import { locales } from '@pisell/utils';
import cartSkuCardLocaleTexts from "../cartSkuCard/locales";
import localeTexts from "./locales";
import CartSkuCard from "../cartSkuCard";
import useEngineContext from "../../../hooks/useEngineContext";
import { defaultValue } from "../status";
import BookingLineItem from "./BookingLineItem";
import "./index.less";
export var PREFIX = 'pisell-line-item';
var isBookingItem = function isBookingItem(dataSource) {
  var _dataSource$_extend;
  return Boolean((dataSource === null || dataSource === void 0 || (_dataSource$_extend = dataSource._extend) === null || _dataSource$_extend === void 0 ? void 0 : _dataSource$_extend.product) || (dataSource === null || dataSource === void 0 ? void 0 : dataSource.booking_id) || (dataSource === null || dataSource === void 0 ? void 0 : dataSource.appointment_status) || (dataSource === null || dataSource === void 0 ? void 0 : dataSource.start_date));
};
var translateText = function translateText(text, translationOriginal) {
  if (typeof text === 'string') return text;
  if (!text) return '';
  return translationOriginal(text);
};

/**
 * 解析行级划线原价（composite 单价）。
 * SalesSdk/OS 行数据用 original_price；legacy cacheItem 用 original_total / _extend.origin_total。
 *
 * @example
 * resolveLineOriginTotal({ original_price: '8.00', selling_price: '5.00' }); // '8.00'
 */
var resolveLineOriginTotal = function resolveLineOriginTotal(product) {
  var _product$_extend, _product$metadata;
  var candidates = [product === null || product === void 0 ? void 0 : product.original_total, product === null || product === void 0 ? void 0 : product.original_price, product === null || product === void 0 || (_product$_extend = product._extend) === null || _product$_extend === void 0 ? void 0 : _product$_extend.origin_total, product === null || product === void 0 || (_product$metadata = product.metadata) === null || _product$metadata === void 0 || (_product$metadata = _product$metadata.origin) === null || _product$metadata === void 0 || (_product$metadata = _product$metadata._extend) === null || _product$metadata === void 0 ? void 0 : _product$metadata.origin_total];
  for (var _i = 0, _candidates = candidates; _i < _candidates.length; _i++) {
    var candidate = _candidates[_i];
    if (candidate !== undefined && candidate !== null && candidate !== '') {
      return candidate;
    }
  }
  return undefined;
};
var convertBundleOption = function convertBundleOption(option, translationOriginal) {
  var _option$add_price, _ref, _option$num;
  return _objectSpread(_objectSpread({}, option), {}, {
    id: option.option_group_item_id,
    name: translateText(option.name, translationOriginal),
    price: (_option$add_price = option.add_price) !== null && _option$add_price !== void 0 ? _option$add_price : option.price,
    num: (_ref = (_option$num = option.num) !== null && _option$num !== void 0 ? _option$num : option.quantity) !== null && _ref !== void 0 ? _ref : 1
  });
};
var convertBundle = function convertBundle(bundle, translationOriginal) {
  var options = (bundle.option || []).map(function (option) {
    return convertBundleOption(option, translationOriginal);
  });
  return _objectSpread(_objectSpread({}, 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: options,
    option: options,
    _origin: bundle
  });
};

/**
 * 读取行级规格：优先 product_sku.option，回落 legacy product_option_item。
 *
 * @example
 * resolveLineItemOptions({ product_sku: { option: [{ name: 'Large' }] } });
 */
var resolveLineItemOptions = function resolveLineItemOptions(product) {
  var _product$product_sku;
  var skuOptions = product === null || product === void 0 || (_product$product_sku = product.product_sku) === null || _product$product_sku === void 0 ? void 0 : _product$product_sku.option;
  if (Array.isArray(skuOptions) && skuOptions.length > 0) {
    return skuOptions;
  }
  if (Array.isArray(product === null || product === void 0 ? void 0 : product.product_option_item)) {
    return product.product_option_item;
  }
  return [];
};

/**
 * 将 OS 规格项映射为 CartSkuCard 可直接渲染的 options。
 *
 * @example
 * mapLineItemOptions([{ name: 'Large', add_price: 1, num: 2 }], translateText);
 */
var mapLineItemOptions = function mapLineItemOptions(options, translationOriginal) {
  return options.map(function (option) {
    var _option$name, _option$add_price2, _ref2, _option$num2;
    return _objectSpread(_objectSpread({}, option), {}, {
      name: translateText((_option$name = option.name) !== null && _option$name !== void 0 ? _option$name : option.title, translationOriginal),
      price: (_option$add_price2 = option.add_price) !== null && _option$add_price2 !== void 0 ? _option$add_price2 : option.price,
      num: (_ref2 = (_option$num2 = option.num) !== null && _option$num2 !== void 0 ? _option$num2 : option.quantity) !== null && _ref2 !== void 0 ? _ref2 : 1
    });
  });
};
export var convertProductToLineItemProduct = function convertProductToLineItemProduct(product, translationOriginal, symbol) {
  var _product$metadata2, _ref3, _product$num, _ref4, _product$num2, _product$metadata3, _product$metadata4, _product$discount_lis, _product$discount_lis2, _product$metadata5, _product$metadata6, _ref5, _product$num3;
  var title = translateText(product.product_title || product.title, translationOriginal);
  var bundle = (product.product_bundle || []).map(function (bundleItem) {
    return convertBundle(bundleItem, translationOriginal);
  });
  var lineOriginTotal = resolveLineOriginTotal(product);
  var lineOptions = mapLineItemOptions(resolveLineItemOptions(product), translationOriginal);
  var mapped = {
    _id: ((_product$metadata2 = product.metadata) === null || _product$metadata2 === void 0 ? void 0 : _product$metadata2.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: title,
    product_title: title,
    name: title,
    cover: product.cover,
    image: product.cover,
    num: (_ref3 = (_product$num = product.num) !== null && _product$num !== void 0 ? _product$num : product.product_quantity) !== null && _ref3 !== void 0 ? _ref3 : 1,
    quantity: (_ref4 = (_product$num2 = product.num) !== null && _product$num2 !== void 0 ? _product$num2 : product.product_quantity) !== null && _ref4 !== void 0 ? _ref4 : 1,
    price: (_product$metadata3 = product.metadata) === null || _product$metadata3 === void 0 ? void 0 : _product$metadata3.source_product_price,
    total: product.selling_price,
    origin_total: lineOriginTotal,
    original_total: lineOriginTotal,
    source_product_price: (_product$metadata4 = product.metadata) === null || _product$metadata4 === void 0 ? void 0 : _product$metadata4.source_product_price,
    selling_price: product.selling_price,
    payment_price: product.payment_price,
    tax_fee: product.tax_fee,
    symbol: symbol,
    note: product.note,
    product_sku: product.product_sku,
    product_bundle: bundle,
    bundle: bundle,
    product_option_string: product.product_option_string,
    options: lineOptions,
    discount_list: product.discount_list,
    promotions: product.promotions,
    discount_reason: (_product$discount_lis = (_product$discount_lis2 = product.discount_list) === null || _product$discount_lis2 === void 0 || (_product$discount_lis2 = _product$discount_lis2.find(function (d) {
      return (d === null || d === void 0 ? void 0 : d.type) === 'product';
    })) === null || _product$discount_lis2 === void 0 || (_product$discount_lis2 = _product$discount_lis2.discount) === null || _product$discount_lis2 === void 0 ? void 0 : _product$discount_lis2.message) !== null && _product$discount_lis !== void 0 ? _product$discount_lis : (_product$metadata5 = product.metadata) === null || _product$metadata5 === void 0 ? void 0 : _product$metadata5.product_discount_reason,
    // 促销赠品走 metadata._giftInfo；礼品卡走 gift_card（对齐 legacy formatProducts isGift）
    isGift: Boolean(product.gift_card || product.isGift || product._isGiftPromotion || ((_product$metadata6 = product.metadata) === null || _product$metadata6 === void 0 ? void 0 : _product$metadata6._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: (_ref5 = (_product$num3 = product.num) !== null && _product$num3 !== void 0 ? _product$num3 : product.product_quantity) !== null && _ref5 !== void 0 ? _ref5 : 1
      },
      note: product.note,
      total: product.selling_price,
      origin_total: lineOriginTotal,
      payment_price: product.payment_price
    }
  };
  return mapped;
};
var LineItem = function LineItem(props) {
  var _context$appHelper$ut, _context$appHelper$ut2, _context$appHelper$ut3, _context$appHelper$ut4, _dataSource$_extend2, _dataSource$_extend4;
  var _props$dataSource = props.dataSource,
    dataSource = _props$dataSource === void 0 ? defaultValue.dataSource : _props$dataSource,
    itemType = props.itemType,
    _props$type = props.type,
    type = _props$type === void 0 ? 'a9' : _props$type,
    isShowBookingHeader = props.isShowBookingHeader,
    _props$rightActions = props.rightActions,
    rightActions = _props$rightActions === void 0 ? [] : _props$rightActions,
    escapeDom = props.escapeDom,
    _props$isShowImage = props.isShowImage,
    isShowImage = _props$isShowImage === void 0 ? defaultValue.isShowImage : _props$isShowImage,
    _props$isShowDelete = props.isShowDelete,
    isShowDelete = _props$isShowDelete === void 0 ? defaultValue.isShowDelete : _props$isShowDelete,
    _props$isShowNote = props.isShowNote,
    isShowNote = _props$isShowNote === void 0 ? defaultValue.isShowNote : _props$isShowNote,
    _props$isShowAmountFo = props.isShowAmountFooter,
    isShowAmountFooter = _props$isShowAmountFo === void 0 ? defaultValue.isShowAmountFooter : _props$isShowAmountFo,
    _props$disabledClick = props.disabledClick,
    disabledClick = _props$disabledClick === void 0 ? defaultValue.disabledClick : _props$disabledClick,
    _props$disabledEdit = props.disabledEdit,
    disabledEdit = _props$disabledEdit === void 0 ? defaultValue.disabledEdit : _props$disabledEdit,
    _props$statusOptions = props.statusOptions,
    statusOptions = _props$statusOptions === void 0 ? [] : _props$statusOptions,
    _props$statusLoading = props.statusLoading,
    statusLoading = _props$statusLoading === void 0 ? false : _props$statusLoading,
    _props$statusDisabled = props.statusDisabled,
    statusDisabled = _props$statusDisabled === void 0 ? false : _props$statusDisabled,
    _props$isShowRelatedP = props.isShowRelatedProduct,
    isShowRelatedProduct = _props$isShowRelatedP === void 0 ? true : _props$isShowRelatedP,
    holderOptions = props.holderOptions,
    onBookingStatusChange = props.onBookingStatusChange,
    onAddTimeProductCard = props.onAddTimeProductCard,
    onAction = props.onAction,
    onDelete = props.onDelete,
    onNote = props.onNote,
    onCard = props.onCard,
    onPromotion = props.onPromotion,
    onGift = props.onGift,
    other = _objectWithoutProperties(props, _excluded);
  console.log('LineItem xxxxxxxxx', props);
  var context = useEngineContext();
  var translationOriginal = context.appHelper.utils.translationOriginal;
  var symbol = context.appHelper.utils.getSymbolic();
  var locale = ((_context$appHelper$ut = context.appHelper.utils) === null || _context$appHelper$ut === void 0 || (_context$appHelper$ut2 = _context$appHelper$ut.getApp) === null || _context$appHelper$ut2 === void 0 || (_context$appHelper$ut2 = _context$appHelper$ut2.call(_context$appHelper$ut)) === null || _context$appHelper$ut2 === void 0 || (_context$appHelper$ut2 = _context$appHelper$ut2.locales) === null || _context$appHelper$ut2 === void 0 || (_context$appHelper$ut3 = _context$appHelper$ut2.getLocale) === null || _context$appHelper$ut3 === void 0 ? void 0 : _context$appHelper$ut3.call(_context$appHelper$ut2)) || ((_context$appHelper$ut4 = context.appHelper.utils) === null || _context$appHelper$ut4 === void 0 || (_context$appHelper$ut4 = _context$appHelper$ut4.storage) === null || _context$appHelper$ut4 === void 0 ? void 0 : _context$appHelper$ut4.get('umi_locale')) || 'en-US';
  locales.init(localeTexts, locale);
  locales.init(cartSkuCardLocaleTexts, locale);
  var itemKind = itemType || (isBookingItem(dataSource) ? 'booking' : 'product');
  var productDataSource = useMemo(function () {
    return convertProductToLineItemProduct(dataSource, translationOriginal, symbol);
  }, [dataSource, symbol, translationOriginal]);
  var relatedProduct = dataSource === null || dataSource === void 0 || (_dataSource$_extend2 = dataSource._extend) === null || _dataSource$_extend2 === void 0 ? void 0 : _dataSource$_extend2.product;
  var relatedProductDataSource = useMemo(function () {
    if (!relatedProduct) return null;
    return convertProductToLineItemProduct(relatedProduct, translationOriginal, symbol);
  }, [relatedProduct, symbol, translationOriginal]);
  var addTimeProductDataSources = useMemo(function () {
    var _dataSource$_extend3;
    var addTimeProducts = Array.isArray(dataSource === null || dataSource === void 0 || (_dataSource$_extend3 = dataSource._extend) === null || _dataSource$_extend3 === void 0 ? void 0 : _dataSource$_extend3.addTimeProduct) ? dataSource._extend.addTimeProduct : [];
    return addTimeProducts.map(function (item) {
      return convertProductToLineItemProduct(item, translationOriginal, symbol);
    });
  }, [dataSource === null || dataSource === void 0 || (_dataSource$_extend4 = dataSource._extend) === null || _dataSource$_extend4 === void 0 ? void 0 : _dataSource$_extend4.addTimeProduct, symbol, translationOriginal]);
  if (itemKind === 'product') {
    return /*#__PURE__*/React.createElement(CartSkuCard, _extends({}, other, {
      dataSource: productDataSource,
      type: type,
      rightActions: rightActions,
      escapeDom: escapeDom,
      isShowImage: isShowImage,
      isShowDelete: isShowDelete,
      isShowNote: isShowNote,
      isShowAmountFooter: isShowAmountFooter,
      disabledClick: disabledClick,
      disabledEdit: disabledEdit,
      onAction: onAction,
      onDelete: onDelete,
      onNote: onNote,
      onCard: onCard,
      onPromotion: onPromotion,
      onGift: onGift
    }));
  }
  return /*#__PURE__*/React.createElement(BookingLineItem, _extends({}, other, {
    dataSource: dataSource,
    type: type,
    cartSkuType: type,
    isShowBookingHeader: isShowBookingHeader,
    relatedProductDataSource: relatedProductDataSource,
    addTimeProductDataSources: addTimeProductDataSources,
    rightActions: rightActions,
    escapeDom: escapeDom,
    isShowImage: isShowImage,
    isShowDelete: isShowDelete,
    isShowNote: isShowNote,
    isShowAmountFooter: isShowAmountFooter,
    disabledClick: disabledClick,
    disabledEdit: disabledEdit,
    statusOptions: statusOptions,
    statusLoading: statusLoading,
    statusDisabled: statusDisabled,
    isShowRelatedProduct: isShowRelatedProduct,
    holderOptions: holderOptions,
    onBookingStatusChange: onBookingStatusChange,
    onAddTimeProductCard: onAddTimeProductCard,
    onAction: onAction,
    onDelete: onDelete,
    onNote: onNote,
    onCard: onCard,
    onPromotion: onPromotion,
    onGift: onGift
  }));
};
export default LineItem;