var _excluded = ["dataSource", "itemType", "type", "rightActions", "escapeDom", "isShowImage", "isShowDelete", "isShowNote", "isShowAmountFooter", "disabledClick", "disabledEdit", "statusOptions", "statusLoading", "statusDisabled", "isShowRelatedProduct", "onBookingStatusChange", "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);
};
var convertBundleOption = function convertBundleOption(option, translationOriginal) {
  return _objectSpread(_objectSpread({}, option), {}, {
    id: option.option_group_item_id,
    name: translateText(option.name, translationOriginal)
  });
};
var convertBundle = function convertBundle(bundle, translationOriginal) {
  var title = translateText(bundle.bundle_title, translationOriginal);
  var price = bundle.bundle_sum_type === 'markdown' ? "-".concat(bundle.bundle_sum_price) : bundle.bundle_sum_price;
  var options = (bundle.option || []).map(function (option) {
    return convertBundleOption(option, translationOriginal);
  });
  return _objectSpread(_objectSpread({}, bundle), {}, {
    id: bundle.bundle_id,
    product_id: bundle.bundle_product_id,
    _bundle_product_id: bundle.bundle_product_id,
    product_variant_id: bundle.bundle_variant_id,
    title: title,
    name: title,
    bundle_title: title,
    num: bundle.num,
    quantity: bundle.num,
    price: price,
    total: bundle.bundle_payment_price,
    origin_total: bundle.bundle_selling_price,
    original_total: bundle.bundle_selling_price,
    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
  });
};
export var convertProductToLineItemProduct = function convertProductToLineItemProduct(product, translationOriginal, symbol) {
  var _product$metadata, _ref, _product$num, _ref2, _product$num2, _product$metadata$sou, _product$metadata2, _product$payment_pric, _product$metadata3, _product$product_sku, _ref3, _product$num3;
  var title = translateText(product.product_title || product.title, translationOriginal);
  var bundle = (product.product_bundle || []).map(function (bundleItem) {
    return convertBundle(bundleItem, translationOriginal);
  });
  return {
    _id: ((_product$metadata = product.metadata) === null || _product$metadata === void 0 ? void 0 : _product$metadata.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: (_ref = (_product$num = product.num) !== null && _product$num !== void 0 ? _product$num : product.product_quantity) !== null && _ref !== void 0 ? _ref : 1,
    quantity: (_ref2 = (_product$num2 = product.num) !== null && _product$num2 !== void 0 ? _product$num2 : product.product_quantity) !== null && _ref2 !== void 0 ? _ref2 : 1,
    price: (_product$metadata$sou = (_product$metadata2 = product.metadata) === null || _product$metadata2 === void 0 ? void 0 : _product$metadata2.source_product_price) !== null && _product$metadata$sou !== void 0 ? _product$metadata$sou : product.selling_price,
    total: (_product$payment_pric = product.payment_price) !== null && _product$payment_pric !== void 0 ? _product$payment_pric : product.selling_price,
    origin_total: product.original_price,
    original_total: product.original_price,
    source_product_price: (_product$metadata3 = product.metadata) === null || _product$metadata3 === void 0 ? void 0 : _product$metadata3.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_sku ? translateText(product.product_sku.subtitle, translationOriginal) : '',
    discount_list: product.discount_list,
    promotions: product.promotions,
    isGift: Boolean(product.gift_card),
    giftData: product.giftData,
    _origin: product,
    _extend: {
      other: {
        product_id: product.product_id,
        option: (_product$product_sku = product.product_sku) === null || _product$product_sku === void 0 ? void 0 : _product$product_sku.option,
        bundle: product.product_bundle,
        product_variant_id: product.product_variant_id,
        quantity: (_ref3 = (_product$num3 = product.num) !== null && _product$num3 !== void 0 ? _product$num3 : product.product_quantity) !== null && _ref3 !== void 0 ? _ref3 : 1
      },
      note: product.note,
      total: product.payment_price,
      origin_total: product.original_price,
      payment_price: product.payment_price
    }
  };
};
var LineItem = function LineItem(props) {
  var _context$appHelper$ut, _context$appHelper$ut2, _context$appHelper$ut3, _context$appHelper$ut4, _dataSource$_extend2;
  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,
    _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,
    onBookingStatusChange = props.onBookingStatusChange,
    onAction = props.onAction,
    onDelete = props.onDelete,
    onNote = props.onNote,
    onCard = props.onCard,
    onPromotion = props.onPromotion,
    onGift = props.onGift,
    other = _objectWithoutProperties(props, _excluded);
  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]);
  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,
    relatedProductDataSource: relatedProductDataSource,
    rightActions: rightActions,
    escapeDom: escapeDom,
    isShowImage: isShowImage,
    isShowDelete: isShowDelete,
    isShowNote: isShowNote,
    isShowAmountFooter: isShowAmountFooter,
    disabledClick: disabledClick,
    disabledEdit: disabledEdit,
    statusOptions: statusOptions,
    statusLoading: statusLoading,
    statusDisabled: statusDisabled,
    isShowRelatedProduct: isShowRelatedProduct,
    onBookingStatusChange: onBookingStatusChange,
    onAction: onAction,
    onDelete: onDelete,
    onNote: onNote,
    onCard: onCard,
    onPromotion: onPromotion,
    onGift: onGift
  }));
};
export default LineItem;